vdom-thunk
Version:
A thunk optimization for virtual-dom
30 lines (24 loc) • 656 B
JavaScript
function Thunk(fn, args, key, eqArgs) {
this.fn = fn;
this.args = args;
this.key = key;
this.eqArgs = eqArgs;
}
Thunk.prototype.type = 'Thunk';
Thunk.prototype.render = render;
module.exports = Thunk;
function shouldUpdate(current, previous) {
if (!current || !previous || current.fn !== previous.fn) {
return true;
}
var cargs = current.args;
var pargs = previous.args;
return !current.eqArgs(cargs, pargs);
}
function render(previous) {
if (shouldUpdate(this, previous)) {
return this.fn.apply(null, this.args);
} else {
return previous.vnode;
}
}