Created
March 31, 2014 05:53
-
-
Save Raynos/9886085 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var createElement = require("virtual-dom/render") | |
var diff = require("virtual-dom/diff") | |
var patch = require("virtual-dom/patch") | |
module.exports = partial | |
function partial(fn) { | |
var args = [].slice.call(arguments) | |
return new Thunk(fn, args) | |
} | |
function Thunk(fn, args) { | |
this.fn = fn | |
this.args = args | |
this.vnode = null | |
} | |
Thunk.prototype.type = "immutable-thunk" | |
Thunk.prototype.update = update | |
Thunk.prototype.init = init | |
function shouldUpdate(current, previous) { | |
return current.fn === previous.fn && | |
current.args.every(function (arg, index) { | |
return arg === previous[index] | |
}) | |
} | |
function update(previous, domNode) { | |
if (!shouldUpdate(this, previous)) { | |
this.vnode = previous.vnode | |
return | |
} | |
this.vnode = (this.vnode || this.fn.apply(null, this.args)) | |
patch(domNode, diff(previous.vnode, this.vnode)) | |
} | |
function init() { | |
this.vnode = this.fn.apply(null, this.args) | |
return createElement(this.vnode) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment