Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure that the latest wrapper is used when processing detached nodes #311

Merged
merged 2 commits into from
Mar 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/widget-core/vdom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1306,18 +1306,19 @@ export function renderer(renderer: () => WNode | VNode): Renderer {
let wrappers = current.childrenWrappers || [];
let wrapper: DNodeWrapper | undefined;
while ((wrapper = wrappers.pop())) {
if (wrapper.childrenWrappers) {
wrappers.push(...wrapper.childrenWrappers);
wrapper.childrenWrappers = undefined;
}
if (isWNodeWrapper(wrapper)) {
wrapper = wrapper.instance ? _instanceToWrapperMap.get(wrapper.instance) || wrapper : wrapper;
if (wrapper.instance) {
_instanceToWrapperMap.delete(wrapper.instance);
const instanceData = widgetInstanceMap.get(wrapper.instance);
instanceData && instanceData.onDetach();
}
wrapper.instance = undefined;
}
if (wrapper.childrenWrappers) {
wrappers.push(...wrapper.childrenWrappers);
wrapper.childrenWrappers = undefined;
}
_wrapperSiblingMap.delete(wrapper);
_parentWrapperMap.delete(wrapper);
wrapper.domNode = undefined;
Expand Down
57 changes: 57 additions & 0 deletions tests/widget-core/unit/vdom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1376,6 +1376,63 @@ jsdomDescribe('vdom', () => {
assert.strictEqual((div.childNodes[0]!.childNodes[0] as Text).data, 'Child One');
});

it('should always use the latest wrapper when processing removed nodes', () => {
let invalidateFoo: any;
let fooRenderCount = 0;
class Foo extends WidgetBase {
constructor() {
super();
invalidateFoo = () => {
this.invalidate();
};
}
render() {
fooRenderCount++;
return 'Foo';
}
}

let switchFoo: any;
class Bar extends WidgetBase {
private _showFoo = false;
constructor() {
super();
switchFoo = () => {
this._showFoo = !this._showFoo;
this.invalidate();
};
}
render() {
return v('div', [v('div', [!this._showFoo ? v('div') : w(Foo, {})])]);
}
}

let showApp: any;
class App extends WidgetBase {
private _showApp = true;
constructor() {
super();
showApp = () => {
this._showApp = !this._showApp;
this.invalidate();
};
}
render() {
return this._showApp ? v('div', [w(Bar, {})]) : null;
}
}

const div = document.createElement('div');
const r = renderer(() => w(App, {}));
r.mount({ domNode: div, sync: true });
assert.strictEqual(fooRenderCount, 0);
switchFoo();
assert.strictEqual(fooRenderCount, 1);
showApp();
invalidateFoo();
assert.strictEqual(fooRenderCount, 1);
});

it('should allow a widget returned from render', () => {
class Bar extends WidgetBase<any> {
render() {
Expand Down