Skip to content
This repository has been archived by the owner on Jul 30, 2018. It is now read-only.

Make sure vdom after callbacks run sync when in sync mode #748

Merged
merged 2 commits into from
Nov 6, 2017
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
1 change: 1 addition & 0 deletions src/interfaces.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export interface ProjectionOptions extends ProjectorOptions {
deferredRenderCallbacks: Function [];
afterRenderCallbacks: Function[];
merge: boolean;
sync: boolean;
mergeElement?: Element;
rootNode: Element;
}
Expand Down
2 changes: 2 additions & 0 deletions src/mixins/Projector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,8 @@ export function ProjectorMixin<P, T extends Constructor<WidgetBase<P>>>(Base: T)
}
});

this._projectionOptions = { ...this._projectionOptions, ...{ sync: !this._async } };

switch (type) {
case AttachType.Append:
this._projection = dom.append(this.root, this._boundRender(), this, this._projectionOptions);
Expand Down
46 changes: 31 additions & 15 deletions src/vdom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -790,31 +790,47 @@ function addDeferredProperties(hnode: InternalHNode, projectionOptions: Projecti

function runDeferredRenderCallbacks(projectionOptions: ProjectionOptions) {
if (projectionOptions.deferredRenderCallbacks.length) {
global.requestAnimationFrame(() => {
if (projectionOptions.sync) {
while (projectionOptions.deferredRenderCallbacks.length) {
const callback = projectionOptions.deferredRenderCallbacks.shift();
callback && callback();
}
});
}
else {
global.requestAnimationFrame(() => {
while (projectionOptions.deferredRenderCallbacks.length) {
const callback = projectionOptions.deferredRenderCallbacks.shift();
callback && callback();
}
});
}
}
}

function runAfterRenderCallbacks(projectionOptions: ProjectionOptions) {
if (global.requestIdleCallback) {
global.requestIdleCallback(() => {
while (projectionOptions.afterRenderCallbacks.length) {
const callback = projectionOptions.afterRenderCallbacks.shift();
callback && callback();
}
});
if (projectionOptions.sync) {
while (projectionOptions.afterRenderCallbacks.length) {
const callback = projectionOptions.afterRenderCallbacks.shift();
callback && callback();
}
}
else {
setTimeout(() => {
while (projectionOptions.afterRenderCallbacks.length) {
const callback = projectionOptions.afterRenderCallbacks.shift();
callback && callback();
}
});
if (global.requestIdleCallback) {
global.requestIdleCallback(() => {
while (projectionOptions.afterRenderCallbacks.length) {
const callback = projectionOptions.afterRenderCallbacks.shift();
callback && callback();
}
});
}
else {
setTimeout(() => {
while (projectionOptions.afterRenderCallbacks.length) {
const callback = projectionOptions.afterRenderCallbacks.shift();
callback && callback();
}
});
}
}
}

Expand Down
17 changes: 17 additions & 0 deletions tests/unit/vdom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2278,6 +2278,23 @@ describe('vdom', () => {

});

describe('sync mode', () => {

it('should run afterRenderCallbacks sync', () => {
const projection = dom.create(v('div', { key: '1' }), projectorStub, { sync: true });
assert.isTrue(projectorStub.emit.calledWith({ type: 'element-created', element: (projection.domNode.childNodes[0] as Element), key: '1' }));
});

it('should run defferedRenderCallbacks sync', () => {
let callCount = 0;
dom.create(v('div', () => {
callCount++;
return {};
}), projectorStub, { sync: true });
assert.strictEqual(callCount, 2);
});
});

describe('node callbacks', () => {

it('element-created not emitted for new nodes without a key', () => {
Expand Down