|
If we talk about functional API, did you consider providing a push method, like with a Promise, to express the updating of an effect or a subscription ? (the function 'update' in the code below) app({
init: 0,
view: state =>
h("div", {}, [
h("h1", {}, state),
h("button", { onclick: (state, event, update) => {
window.setTimeout(() => update(state => state - 1), 1000);
} }, "subtract"),
h("button", { onclick: state => state + 1 }, "add")
]),
node: document.getElementById("app")
})
|
Instead, we have "controlled effects" in Hyperapp. This `delay` function doesn't even call setTimeout itself, but return an object that tells Hyperapp how. Just like how `h("button")` or `<button>` with JSX doesn't actually create a button, but an object representation of it.
Finally this part: [state, delay(100, Decrement)], only placed here for convenience (as you'll usually want that in its own action) is how you tell Hyperapp to do the effect when the button is clicked. This is the same (model Cmd) continuation pattern used in Elm.