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
function view(count, handler) { | |
return h('div', [ | |
h('button', { | |
on : { click: handler.bind(null, Action.Increment()) } | |
}, '+'), | |
h('button', { | |
on : { click: handler.bind(null, Action.Decrement()) } | |
}, '-'), | |
h('div', `Count : ${count}`), | |
]); |
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
function update(count, action) { | |
return Action.case({ | |
Increment : () => count + 1, | |
Decrement : () => count - 1 | |
}, action); | |
} |
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
const Action = Type({ | |
Add : [], | |
Remove : [Number], | |
Reset : [], | |
Update : [Number, counter.Action], | |
}); |
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
function update(model, action) { | |
return Action.case({ | |
Add : () => addCounter(model), | |
Remove : id => removeCounter(model, id), | |
Reset : () => resetCounters(model), | |
Update : (id, action) => updateCounter(model, id, action) | |
}, action); | |
} |
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
function init() { | |
return 0; | |
} |
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
function init() { | |
return { first: counter.init(), second: counter.init() }; | |
} |
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
function update(model, action) { | |
return action.type === RESET ? | |
{ | |
first : counter.init(), | |
second: counter.init() | |
} | |
: action.type === UPDATE_FIRST ? | |
{...model, first : counter.update(model.first, action.data) } | |
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
main( | |
twoCounters.init(), // the initial state | |
document.getElementById('placeholder'), | |
twoCounters | |
); |
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
function init() { | |
return { nextID: 1, counters: [] }; | |
} |
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
/** @jsx html */ | |
import { html } from 'snabbdom-jsx'; | |
import Type from 'union-type'; | |
const Action = Type({ | |
Increment : [], | |
Decrement : [], | |
}); | |