Skip to content

Commit

Permalink
Allows fragment updates to happen after state updates
Browse files Browse the repository at this point in the history
Proposed fix for #1520. Provides a starting point for discussion.
  • Loading branch information
jacwright committed Aug 23, 2018
1 parent f38bdaa commit 2c24c36
Show file tree
Hide file tree
Showing 63 changed files with 943 additions and 122 deletions.
18 changes: 16 additions & 2 deletions src/shared/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,30 @@ export function set(newState) {
flush(this.root);
}

export function _set(newState) {
export function _set(newState, options) {
var oldState = this._state,
changed = {},
dirty = false;

for (var key in newState) {
if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true;
}
if (!dirty) return;
if (!dirty && !this._changed) return false;

this._state = assign(assign({}, oldState), newState);

if (options && options.skipRender) {
if (!this._oldState) this._oldState = oldState;
this._changed = assign(changed, this._changed);
return true;
}

if (this._changed) {
oldState = this._oldState;
changed = assign(changed, this._changed),
this._changed = this._oldState = null;
}

this._recompute(changed, this._state);
if (this._bind) this._bind(changed, this._state);

Expand All @@ -126,6 +139,7 @@ export function _set(newState) {
this._fragment.p(changed, this._state);
this.fire("update", { changed: changed, current: this._state, previous: oldState });
}
return true;
}

export function setDev(newState) {
Expand Down
13 changes: 9 additions & 4 deletions store.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ assign(Store.prototype, {
});

const dependents = this._dependents.slice(); // guard against mutations
for (let i = 0; i < dependents.length; i += 1) {
const dependent = dependents[i];
const options = { skipRender: true };
const empty = {};
const dirtyDependents = dependents.filter(dependent => {
const componentState = {};
let dirty = false;

Expand All @@ -74,8 +75,12 @@ assign(Store.prototype, {
}
}

if (dirty) dependent.component.set(componentState);
}
if (dirty) {
return dependent.component._set(componentState, options);
}
});

dirtyDependents.forEach(dependent => dependent.component.set(empty));

this.fire('update', {
changed,
Expand Down
18 changes: 16 additions & 2 deletions test/cli/samples/amd/expected/Main.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,30 @@ define("test", function() { "use strict";
flush(this.root);
}

function _set(newState) {
function _set(newState, options) {
var oldState = this._state,
changed = {},
dirty = false;

for (var key in newState) {
if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true;
}
if (!dirty) return;
if (!dirty && !this._changed) return false;

this._state = assign(assign({}, oldState), newState);

if (options && options.skipRender) {
if (!this._oldState) this._oldState = oldState;
this._changed = assign(changed, this._changed);
return true;
}

if (this._changed) {
oldState = this._oldState;
changed = assign(changed, this._changed),
this._changed = this._oldState = null;
}

this._recompute(changed, this._state);
if (this._bind) this._bind(changed, this._state);

Expand All @@ -154,6 +167,7 @@ define("test", function() { "use strict";
this._fragment.p(changed, this._state);
this.fire("update", { changed: changed, current: this._state, previous: oldState });
}
return true;
}

function _mount(target, anchor) {
Expand Down
18 changes: 16 additions & 2 deletions test/cli/samples/basic/expected/Main.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,30 @@ function set(newState) {
flush(this.root);
}

function _set(newState) {
function _set(newState, options) {
var oldState = this._state,
changed = {},
dirty = false;

for (var key in newState) {
if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true;
}
if (!dirty) return;
if (!dirty && !this._changed) return false;

this._state = assign(assign({}, oldState), newState);

if (options && options.skipRender) {
if (!this._oldState) this._oldState = oldState;
this._changed = assign(changed, this._changed);
return true;
}

if (this._changed) {
oldState = this._oldState;
changed = assign(changed, this._changed),
this._changed = this._oldState = null;
}

this._recompute(changed, this._state);
if (this._bind) this._bind(changed, this._state);

Expand All @@ -154,6 +167,7 @@ function _set(newState) {
this._fragment.p(changed, this._state);
this.fire("update", { changed: changed, current: this._state, previous: oldState });
}
return true;
}

function _mount(target, anchor) {
Expand Down
18 changes: 16 additions & 2 deletions test/cli/samples/custom-element/expected/Main.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,17 +156,30 @@ function set(newState) {
flush(this.root);
}

function _set(newState) {
function _set(newState, options) {
var oldState = this._state,
changed = {},
dirty = false;

for (var key in newState) {
if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true;
}
if (!dirty) return;
if (!dirty && !this._changed) return false;

this._state = assign(assign({}, oldState), newState);

if (options && options.skipRender) {
if (!this._oldState) this._oldState = oldState;
this._changed = assign(changed, this._changed);
return true;
}

if (this._changed) {
oldState = this._oldState;
changed = assign(changed, this._changed),
this._changed = this._oldState = null;
}

this._recompute(changed, this._state);
if (this._bind) this._bind(changed, this._state);

Expand All @@ -175,6 +188,7 @@ function _set(newState) {
this._fragment.p(changed, this._state);
this.fire("update", { changed: changed, current: this._state, previous: oldState });
}
return true;
}

function _mount(target, anchor) {
Expand Down
18 changes: 16 additions & 2 deletions test/cli/samples/dev/expected/Main.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,17 +161,30 @@ function setDev(newState) {
set.call(this, newState);
}

function _set(newState) {
function _set(newState, options) {
var oldState = this._state,
changed = {},
dirty = false;

for (var key in newState) {
if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true;
}
if (!dirty) return;
if (!dirty && !this._changed) return false;

this._state = assign(assign({}, oldState), newState);

if (options && options.skipRender) {
if (!this._oldState) this._oldState = oldState;
this._changed = assign(changed, this._changed);
return true;
}

if (this._changed) {
oldState = this._oldState;
changed = assign(changed, this._changed),
this._changed = this._oldState = null;
}

this._recompute(changed, this._state);
if (this._bind) this._bind(changed, this._state);

Expand All @@ -180,6 +193,7 @@ function _set(newState) {
this._fragment.p(changed, this._state);
this.fire("update", { changed: changed, current: this._state, previous: oldState });
}
return true;
}

function _mount(target, anchor) {
Expand Down
18 changes: 16 additions & 2 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.

18 changes: 16 additions & 2 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.

18 changes: 16 additions & 2 deletions test/cli/samples/dir-subdir/expected/Main.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,17 +137,30 @@ function set(newState) {
flush(this.root);
}

function _set(newState) {
function _set(newState, options) {
var oldState = this._state,
changed = {},
dirty = false;

for (var key in newState) {
if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true;
}
if (!dirty) return;
if (!dirty && !this._changed) return false;

this._state = assign(assign({}, oldState), newState);

if (options && options.skipRender) {
if (!this._oldState) this._oldState = oldState;
this._changed = assign(changed, this._changed);
return true;
}

if (this._changed) {
oldState = this._oldState;
changed = assign(changed, this._changed),
this._changed = this._oldState = null;
}

this._recompute(changed, this._state);
if (this._bind) this._bind(changed, this._state);

Expand All @@ -156,6 +169,7 @@ function _set(newState) {
this._fragment.p(changed, this._state);
this.fire("update", { changed: changed, current: this._state, previous: oldState });
}
return true;
}

function _mount(target, anchor) {
Expand Down
Loading

0 comments on commit 2c24c36

Please sign in to comment.