<commit_before>var cuid = require("cuid")

var dom = require("./render")
var diff = require("./diff")
var patch = require("./patch")
var isString = require("./lib/is-string")

/* Usage
var h = require("virtual-dom/h")
var thunk = require("virtual-dom/thunk")

module.exports = thunk("myThunk", render, shouldUpdate)

function render(state) {
    return h("div", state.value)
}

function shouldUpdate(prev, curr) {
    return prev.value !== curr.value
}
*/

/* Function signature

thunk(render<Function>) : <Widget>
thunk(type<String>, render<Function>) : <Widget>
thunk(render<Function>, shouldUpdate<Function|Boolean>) : <Widget>
thunk(type<String>, render<Function>, shouldUpdate<Function|Boolean>) : <Widget>

*/
module.exports = thunk

function thunk(type, render, shouldUpdate) {
    if (arguments.length === 2) {
        if (!isString(type)) {
            shouldUpdate = render
            render = type
            type = null
        }
    } else if (arguments.length === 1) {
        render = type
        type = null
    }

    function Thunk(state) {
        if (!(this instanceof Thunk)) {
            return new Thunk(state)
        }

        this.state = state
    }

    Thunk.prototype.init = initThunk
    Thunk.prototype.update = updateThunk

    Thunk.prototype.type = isString(type) ? type : cuid()
    Thunk.prototype.render = render
    Thunk.prototype.shouldUpdate = getUpdate(shouldUpdate)

    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 shouldUpdate = this.shouldUpdate

    if (shouldUpdate === false || (shouldUpdate !== true &&
            !shouldUpdate(previous.state, currentState))) {
        return
    }

    // Render is pure, so currentState will always yield the same value
    var vnode = this.vnode = (this.vnode || render(currentState))
    patch(domNode, diff(previous.vnode, vnode))
}

function getUpdate(shouldUpdate) {
    var type = typeof shouldUpdate
    if (type === "boolean" || type === "function") {
        return shouldUpdate
    } else {
        return shouldUpdateDefault
    }
}

function shouldUpdateDefault(previous, current) {
    return previous === current
}
<commit_msg>copy previous vnode in thunk on early exit<commit_after>var cuid = require("cuid")

var dom = require("./render")
var diff = require("./diff")
var patch = require("./patch")
var isString = require("./lib/is-string")

/* Usage
var h = require("virtual-dom/h")
var thunk = require("virtual-dom/thunk")

module.exports = thunk("myThunk", render, shouldUpdate)

function render(state) {
    return h("div", state.value)
}

function shouldUpdate(prev, curr) {
    return prev.value !== curr.value
}
*/

/* Function signature

thunk(render<Function>) : <Widget>
thunk(type<String>, render<Function>) : <Widget>
thunk(render<Function>, shouldUpdate<Function|Boolean>) : <Widget>
thunk(type<String>, render<Function>, shouldUpdate<Function|Boolean>) : <Widget>

*/
module.exports = thunk

function thunk(type, render, shouldUpdate) {
    if (arguments.length === 2) {
        if (!isString(type)) {
            shouldUpdate = render
            render = type
            type = null
        }
    } else if (arguments.length === 1) {
        render = type
        type = null
    }

    function Thunk(state) {
        if (!(this instanceof Thunk)) {
            return new Thunk(state)
        }

        this.state = state
    }

    Thunk.prototype.init = initThunk
    Thunk.prototype.update = updateThunk

    Thunk.prototype.type = isString(type) ? type : cuid()
    Thunk.prototype.render = render
    Thunk.prototype.shouldUpdate = getUpdate(shouldUpdate)

    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 shouldUpdate = this.shouldUpdate

    if (shouldUpdate === false || (shouldUpdate !== true &&
            !shouldUpdate(previous.state, currentState))) {
        this.vnode = previous.vnode
        return
    }

    // Render is pure, so currentState will always yield the same value
    var vnode = this.vnode = (this.vnode || render(currentState))
    patch(domNode, diff(previous.vnode, vnode))
}

function getUpdate(shouldUpdate) {
    var type = typeof shouldUpdate
    if (type === "boolean" || type === "function") {
        return shouldUpdate
    } else {
        return shouldUpdateDefault
    }
}

function shouldUpdateDefault(previous, current) {
    return previous === current
}

{'fraction_non_alphanumeric': 0.12326246933769419, 'fraction_numerical': 0.0008176614881439084, 'mean_word_length': 2.6190828402366866, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4272286', 'n_tokens_mistral': 1494, 'n_tokens_neox': 1425, 'n_words': 349}