-
-
Save Matt-Esch/9867761 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 cuid = require("cuid") | |
var dom = require("./render") | |
var diff = require("./diff") | |
var patch = require("./patch") | |
var isString = require("./lib/is-string") | |
/* Usage | |
var thunk = require("virtual-dom/thunk") | |
module.exports = thunk(function render(current, previous) { | |
if (previous && previous.id === current.id) { | |
return null | |
} | |
return h("div", current.value) | |
}) | |
*/ | |
module.exports = thunk | |
function thunk(type, render) { | |
if (arguments.length === 1) { | |
render = type | |
type = null | |
} | |
function Thunk(state) { | |
if (!(this instanceof Thunk)) { | |
return new Thunk(state) | |
} | |
this.state = state | |
} | |
Thunk.prototype.type = isString(type) ? type : cuid() | |
Thunk.prototype.render = render | |
Thunk.prototype.init = initThunk | |
Thunk.prototype.update = updateThunk | |
return Thunk | |
} | |
function initThunk() { | |
var vnode = this.vnode = this.render(this.state) | |
return dom(vnode) | |
} | |
function updateThunk(previous, domNode) { | |
var currentState = this.state | |
var render = this.render | |
var vnode = render(currentState, previous.state) | |
if (vnode) { | |
this.vnode = vnode | |
patch(domNode, diff(previous.vnode, vnode)) | |
} else { | |
this.vnode = previous.vnode | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment