Skip to content

Commit

Permalink
Merge pull request #1691 from sveltejs/gh-1520-alt
Browse files Browse the repository at this point in the history
Alternative approach to #1520
  • Loading branch information
Rich-Harris authored Aug 26, 2018
2 parents 0f171a5 + a18cec0 commit 6327647
Show file tree
Hide file tree
Showing 66 changed files with 572 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/compile/dom/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export default function dom(
? `@proto`
: deindent`
{
${['destroy', 'get', 'fire', 'on', 'set', '_set', '_mount', '_differs']
${['destroy', 'get', 'fire', 'on', 'set', '_set', '_stage', '_mount', '_differs']
.map(n => `${n}: @${n}`)
.join(',\n')}
}`;
Expand Down
10 changes: 10 additions & 0 deletions src/shared/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export function init(component, options) {
component._handlers = blankObject();
component._slots = blankObject();
component._bind = options._bind;
component._staged = {};

component.options = options;
component.root = options.root || component;
Expand Down Expand Up @@ -108,6 +109,9 @@ export function _set(newState) {
changed = {},
dirty = false;

newState = assign(this._staged, newState);
this._staged = {};

for (var key in newState) {
if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true;
}
Expand All @@ -124,6 +128,10 @@ export function _set(newState) {
}
}

export function _stage(newState) {
assign(this._staged, newState);
}

export function setDev(newState) {
if (typeof newState !== 'object') {
throw new Error(
Expand Down Expand Up @@ -159,6 +167,7 @@ export var proto = {
set,
_recompute: noop,
_set,
_stage,
_mount,
_differs
};
Expand All @@ -171,6 +180,7 @@ export var protoDev = {
set: setDev,
_recompute: noop,
_set,
_stage,
_mount,
_differs
};
33 changes: 19 additions & 14 deletions store.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,27 @@ assign(Store.prototype, {
current: this._state
});

const dependents = this._dependents.slice(); // guard against mutations
for (let i = 0; i < dependents.length; i += 1) {
const dependent = dependents[i];
const componentState = {};
let dirty = false;

for (let j = 0; j < dependent.props.length; j += 1) {
const prop = dependent.props[j];
if (prop in changed) {
componentState['$' + prop] = this._state[prop];
dirty = true;
this._dependents
.filter(dependent => {
const componentState = {};
let dirty = false;

for (let j = 0; j < dependent.props.length; j += 1) {
const prop = dependent.props[j];
if (prop in changed) {
componentState['$' + prop] = this._state[prop];
dirty = true;
}
}
}

if (dirty) dependent.component.set(componentState);
}
if (dirty) {
dependent.component._stage(componentState);
return true;
}
})
.forEach(dependent => {
dependent.component.set({});
});

this.fire('update', {
changed,
Expand Down
9 changes: 9 additions & 0 deletions test/cli/samples/amd/expected/Main.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ define("test", function() { "use strict";
on: on,
set: set,
_set: _set,
_stage: _stage,
_mount: _mount,
_differs: _differs
});
Expand All @@ -67,6 +68,7 @@ define("test", function() { "use strict";
component._handlers = blankObject();
component._slots = blankObject();
component._bind = options._bind;
component._staged = {};

component.options = options;
component.root = options.root || component;
Expand Down Expand Up @@ -140,6 +142,9 @@ define("test", function() { "use strict";
changed = {},
dirty = false;

newState = assign(this._staged, newState);
this._staged = {};

for (var key in newState) {
if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true;
}
Expand All @@ -156,6 +161,10 @@ define("test", function() { "use strict";
}
}

function _stage(newState) {
assign(this._staged, newState);
}

function _mount(target, anchor) {
this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null);
}
Expand Down
9 changes: 9 additions & 0 deletions test/cli/samples/basic/expected/Main.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ assign(Main.prototype, {
on: on,
set: set,
_set: _set,
_stage: _stage,
_mount: _mount,
_differs: _differs
});
Expand All @@ -67,6 +68,7 @@ function init(component, options) {
component._handlers = blankObject();
component._slots = blankObject();
component._bind = options._bind;
component._staged = {};

component.options = options;
component.root = options.root || component;
Expand Down Expand Up @@ -140,6 +142,9 @@ function _set(newState) {
changed = {},
dirty = false;

newState = assign(this._staged, newState);
this._staged = {};

for (var key in newState) {
if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true;
}
Expand All @@ -156,6 +161,10 @@ function _set(newState) {
}
}

function _stage(newState) {
assign(this._staged, newState);
}

function _mount(target, anchor) {
this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null);
}
Expand Down
9 changes: 9 additions & 0 deletions test/cli/samples/custom-element/expected/Main.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ assign(Main.prototype, {
on: on,
set: set,
_set: _set,
_stage: _stage,
_mount: _mount,
_differs: _differs
});
Expand Down Expand Up @@ -88,6 +89,7 @@ function init(component, options) {
component._handlers = blankObject();
component._slots = blankObject();
component._bind = options._bind;
component._staged = {};

component.options = options;
component.root = options.root || component;
Expand Down Expand Up @@ -161,6 +163,9 @@ function _set(newState) {
changed = {},
dirty = false;

newState = assign(this._staged, newState);
this._staged = {};

for (var key in newState) {
if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true;
}
Expand All @@ -177,6 +182,10 @@ function _set(newState) {
}
}

function _stage(newState) {
assign(this._staged, newState);
}

function _mount(target, anchor) {
this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null);
}
Expand Down
9 changes: 9 additions & 0 deletions test/cli/samples/dev/expected/Main.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ assign(Main.prototype, {
on: on,
set: setDev,
_set: _set,
_stage: _stage,
_mount: _mount,
_differs: _differs
});
Expand Down Expand Up @@ -91,6 +92,7 @@ function init(component, options) {
component._handlers = blankObject();
component._slots = blankObject();
component._bind = options._bind;
component._staged = {};

component.options = options;
component.root = options.root || component;
Expand Down Expand Up @@ -166,6 +168,9 @@ function _set(newState) {
changed = {},
dirty = false;

newState = assign(this._staged, newState);
this._staged = {};

for (var key in newState) {
if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true;
}
Expand All @@ -182,6 +187,10 @@ function _set(newState) {
}
}

function _stage(newState) {
assign(this._staged, newState);
}

function _mount(target, anchor) {
this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null);
}
Expand Down
9 changes: 9 additions & 0 deletions test/cli/samples/dir-sourcemap/expected/Main.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/cli/samples/dir-sourcemap/expected/Main.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions test/cli/samples/dir-sourcemap/expected/Widget.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/cli/samples/dir-sourcemap/expected/Widget.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions test/cli/samples/dir-subdir/expected/Main.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ assign(Main.prototype, {
on: on,
set: set,
_set: _set,
_stage: _stage,
_mount: _mount,
_differs: _differs
});
Expand All @@ -61,6 +62,7 @@ function init(component, options) {
component._handlers = blankObject();
component._slots = blankObject();
component._bind = options._bind;
component._staged = {};

component.options = options;
component.root = options.root || component;
Expand Down Expand Up @@ -142,6 +144,9 @@ function _set(newState) {
changed = {},
dirty = false;

newState = assign(this._staged, newState);
this._staged = {};

for (var key in newState) {
if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true;
}
Expand All @@ -158,6 +163,10 @@ function _set(newState) {
}
}

function _stage(newState) {
assign(this._staged, newState);
}

function _mount(target, anchor) {
this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null);
}
Expand Down
Loading

0 comments on commit 6327647

Please sign in to comment.