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

Commit 72316bc

Browse files
authored
Only schedule a Projector render when properties or children are set explicitly (#705)
* expose _dirty property with getter and do not reset properties / children when explicitly set by the projector API * do not need to expose dirty using specific flags for resetting properties and children
1 parent 4798947 commit 72316bc

File tree

2 files changed

+25
-18
lines changed

2 files changed

+25
-18
lines changed

src/mixins/Projector.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,10 @@ export function ProjectorMixin<P, T extends Constructor<WidgetBase<P>>>(Base: T)
169169
private _paused: boolean;
170170
private _boundDoRender: () => void;
171171
private _boundRender: Function;
172-
private _projectorChildren: DNode[];
173-
private _projectorProperties: this['properties'];
172+
private _projectorChildren: DNode[] = [];
173+
private _resetChildren = true;
174+
private _projectorProperties: this['properties'] = {} as this['properties'];
175+
private _resetProperties = true;
174176
private _rootTagName: string;
175177
private _attachType: AttachType;
176178

@@ -288,12 +290,13 @@ export function ProjectorMixin<P, T extends Constructor<WidgetBase<P>>>(Base: T)
288290
}
289291

290292
public setChildren(children: DNode[]): void {
293+
this._resetChildren = false;
291294
this._projectorChildren = [ ...children ];
292-
super.__setChildren__(children);
293-
this.emit({ type: 'invalidated' });
295+
super.__setChildren__([ ...children ]);
294296
}
295297

296298
public setProperties(properties: this['properties']): void {
299+
this._resetProperties = false;
297300
if (this._projectorProperties && this._projectorProperties.registry !== properties.registry) {
298301
if (this._projectorProperties.registry) {
299302
this._projectorProperties.registry.destroy();
@@ -305,7 +308,6 @@ export function ProjectorMixin<P, T extends Constructor<WidgetBase<P>>>(Base: T)
305308
this._projectorProperties = assign({}, properties);
306309
super.__setCoreProperties__({ bind: this, baseRegistry: properties.registry });
307310
super.__setProperties__(properties);
308-
this.emit({ type: 'invalidated' });
309311
}
310312

311313
public toHtml(): string {
@@ -346,17 +348,20 @@ export function ProjectorMixin<P, T extends Constructor<WidgetBase<P>>>(Base: T)
346348
}
347349

348350
public __render__(): VNode {
349-
if (this._projectorChildren) {
351+
if (this._resetChildren) {
350352
this.setChildren(this._projectorChildren);
351353
}
352-
if (this._projectorProperties) {
354+
if (this._resetProperties) {
353355
this.setProperties(this._projectorProperties);
354356
}
357+
this._resetChildren = true;
358+
this._resetProperties = true;
355359
return super.__render__() as VNode;
356360
}
357361

358362
public invalidate(): void {
359363
super.invalidate();
364+
this.scheduleRender();
360365
}
361366

362367
private _doRender() {

tests/unit/mixins/Projector.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -761,18 +761,14 @@ registerSuite({
761761
projector.destroy();
762762
assert.strictEqual(registryDestroyedCount, 3);
763763
},
764-
'scheduleRender on setting properties'() {
764+
'scheduleRender on called on invalidate when projector is dirty'() {
765765
const projector = new BaseTestWidget();
766766
const scheduleRender = spy(projector, 'scheduleRender');
767+
projector.append();
767768
projector.setProperties({ key: 'hello' });
768-
assert.isTrue(scheduleRender.called);
769-
},
770-
'scheduleRender not called on invalidate'() {
771-
const projector = new BaseTestWidget();
772-
const scheduleRender = spy(projector, 'scheduleRender');
773-
projector.__setProperties__({});
774-
projector.invalidate();
775-
assert.isTrue(scheduleRender.notCalled);
769+
assert.isTrue(scheduleRender.calledOnce);
770+
projector.setProperties({ key: 'hello' });
771+
assert.isTrue(scheduleRender.calledOnce);
776772
},
777773
'properties are reset to original state on render'() {
778774
const testProperties = {
@@ -800,9 +796,15 @@ registerSuite({
800796
'invalidate on setting children'() {
801797
const projector = new BaseTestWidget();
802798
const scheduleRender = spy(projector, 'scheduleRender');
803-
799+
projector.append();
800+
projector.setChildren([]);
801+
assert.isTrue(scheduleRender.notCalled);
804802
projector.setChildren([ v('div') ]);
805-
assert.isTrue(scheduleRender.called);
803+
assert.isTrue(scheduleRender.calledOnce);
804+
projector.setChildren([]);
805+
assert.isTrue(scheduleRender.calledTwice);
806+
projector.setChildren([]);
807+
assert.isTrue(scheduleRender.calledTwice);
806808
},
807809
'invalidate before attached'() {
808810
const projector: any = new BaseTestWidget();

0 commit comments

Comments
 (0)