Skip to content

Instantly share code, notes, and snippets.

@Raynos
Created March 31, 2014 05:53
Show Gist options
  • Save Raynos/9886085 to your computer and use it in GitHub Desktop.
Save Raynos/9886085 to your computer and use it in GitHub Desktop.
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