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

Commit

Permalink
explicit if checks for undefined, boolean and length and for loop ove…
Browse files Browse the repository at this point in the history
…r array methods
  • Loading branch information
agubler committed Jul 25, 2017
1 parent 7f735d4 commit 975ae0b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 43 deletions.
77 changes: 41 additions & 36 deletions src/WidgetBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ export class WidgetBase<P = WidgetProperties, C extends DNode = DNode> extends E

this._properties = diffPropertyResults;

if (changedPropertyKeys.length) {
if (changedPropertyKeys.length > 0) {
this.invalidate();
}
}
Expand All @@ -353,24 +353,22 @@ export class WidgetBase<P = WidgetProperties, C extends DNode = DNode> extends E

public __setChildren__(children: (C | null)[]): void {
this._renderState = WidgetRenderState.CHILDREN;
if (this._children.length || children.length) {
if (this._children.length > 0 || children.length > 0) {
this._children = children;
this.invalidate();
}
}

public __render__(): VirtualDomNode | VirtualDomNode[] {
this._renderState = WidgetRenderState.RENDER;
if (this._dirty || !this._cachedVNode) {
if (this._dirty === true || this._cachedVNode === undefined) {
this._dirty = false;
const render = this._runBeforeRenders();
let dNode = render();
dNode = this.runAfterRenders(dNode);
const widget = this._dNodeToVNode(dNode);
this._manageDetachedChildren();
if (widget) {
this._cachedVNode = widget;
}
this._cachedVNode = widget;
this._renderState = WidgetRenderState.IDLE;
return widget;
}
Expand Down Expand Up @@ -525,14 +523,17 @@ export class WidgetBase<P = WidgetProperties, C extends DNode = DNode> extends E
private _runBeforeRenders(): Render {
const beforeRenders = this.getDecorator('beforeRender');

return beforeRenders.reduce((render: Render, beforeRenderFunction: BeforeRender) => {
const updatedRender = beforeRenderFunction.call(this, render, this._properties, this._children);
if (!updatedRender) {
console.warn('Render function not returned from beforeRender, using previous render');
return render;
}
return updatedRender;
}, this._boundRenderFunc);
if (beforeRenders.length > 0) {
return beforeRenders.reduce((render: Render, beforeRenderFunction: BeforeRender) => {
const updatedRender = beforeRenderFunction.call(this, render, this._properties, this._children);
if (!updatedRender) {
console.warn('Render function not returned from beforeRender, using previous render');
return render;
}
return updatedRender;
}, this._boundRenderFunc);
}
return this._boundRenderFunc;
}

/**
Expand All @@ -543,9 +544,12 @@ export class WidgetBase<P = WidgetProperties, C extends DNode = DNode> extends E
protected runAfterRenders(dNode: DNode | DNode[]): DNode | DNode[] {
const afterRenders = this.getDecorator('afterRender');

return afterRenders.reduce((dNode: DNode | DNode[], afterRenderFunction: AfterRender) => {
return afterRenderFunction.call(this, dNode);
}, dNode);
if (afterRenders.length > 0) {
return afterRenders.reduce((dNode: DNode | DNode[], afterRenderFunction: AfterRender) => {
return afterRenderFunction.call(this, dNode);
}, dNode);
}
return dNode;
}

/**
Expand Down Expand Up @@ -584,29 +588,28 @@ export class WidgetBase<P = WidgetProperties, C extends DNode = DNode> extends E
const childrenMapKey = key || widgetConstructor;
let cachedChildren = this._cachedChildrenMap.get(childrenMapKey) || [];
let cachedChild: WidgetCacheWrapper | undefined;
cachedChildren.some((cachedChildWrapper) => {
if (cachedChildWrapper.widgetConstructor === widgetConstructor && !cachedChildWrapper.used) {

for (let i = 0; i < cachedChildren.length; i++) {
const cachedChildWrapper = cachedChildren[i];
if (cachedChildWrapper.widgetConstructor === widgetConstructor && cachedChildWrapper.used === false) {
cachedChild = cachedChildWrapper;
return true;
break;
}
return false;
});
}

if (!(<any> properties).bind) {
if ((<any> properties).bind === undefined) {
(<any> properties).bind = this;
}

if (cachedChild) {
if (cachedChild !== undefined) {
child = cachedChild.child;
child.__setProperties__(properties);
cachedChild.used = true;
}
else {
child = new widgetConstructor();
child.__setProperties__(properties);
child.own(child.on('invalidated', () => {
this.invalidate();
}));
child.own(child.on('invalidated', this._boundInvalidate));
cachedChildren = [...cachedChildren, { child, widgetConstructor, used: true }];
this._cachedChildrenMap.set(childrenMapKey, cachedChildren);
this.own(child);
Expand All @@ -632,7 +635,7 @@ export class WidgetBase<P = WidgetProperties, C extends DNode = DNode> extends E
dNode.properties.afterUpdate = this._afterUpdateCallback;
}

if (!dNode.properties.bind) {
if (dNode.properties.bind === undefined) {
(<any> dNode).properties.bind = this;
}

Expand All @@ -653,15 +656,17 @@ export class WidgetBase<P = WidgetProperties, C extends DNode = DNode> extends E
*/
private _manageDetachedChildren(): void {
this._cachedChildrenMap.forEach((cachedChildren, key) => {
const filterCachedChildren = cachedChildren.filter((cachedChild) => {
if (cachedChild.used) {
cachedChild.used = false;
return true;
const filteredCacheChildren: WidgetCacheWrapper[] = [];
for (let i = 0; i < cachedChildren.length; i++) {
const cachedChild = cachedChildren[i];
if (cachedChild.used === false) {
cachedChild.child.destroy();
continue;
}
cachedChild.child.destroy();
return false;
});
this._cachedChildrenMap.set(key, filterCachedChildren);
cachedChild.used = false;
filteredCacheChildren.push(cachedChild);
}
this._cachedChildrenMap.set(key, filteredCacheChildren);
});
}

Expand Down
11 changes: 4 additions & 7 deletions src/d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,20 +97,17 @@ export function v(tag: string, propertiesOrChildren: VirtualDomProperties | DNod
properties = {};
}

if (properties) {
let { classes } = properties;
if (typeof classes === 'function') {
classes = classes();
properties = assign(properties, { classes });
}
let { classes } = properties;
if (typeof classes === 'function') {
classes = classes();
properties = assign(properties, { classes });
}

return {
tag,
children,
properties,
render(this: { vNodes: VNode[], properties: VNodeProperties }) {

return h(tag, this.properties, this.vNodes);
},
type: HNODE
Expand Down

0 comments on commit 975ae0b

Please sign in to comment.