This file contains 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 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 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 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 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 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 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
import Type from 'union-type'; | |
const Action = Type({ | |
Increment : [], | |
Decrement : [] | |
}); |
This file contains 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 updateCounter(model, id, action) { | |
return {...model, | |
counters : model.counters.map(item => | |
item.id !== id ? | |
item | |
: { ...item, | |
counter : counter.update(item.counter, action) | |
} | |
) | |
}; |
This file contains 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 counterItemView(item, handler) { | |
return h('div.counter-item', {key: item.id }, [ | |
h('button.remove', { | |
on : { click: e => handler({ type: REMOVE, id: item.id}) } | |
}, 'Remove'), | |
counter.view(item.counter, a => handler({type: UPDATE, id: item.id, data: a})), | |
h('hr') | |
]); | |
} |
This file contains 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
//{ first : counter.model, second : counter.model } | |
const RESET = Symbol('reset'); | |
const UPDATE_FIRST = Symbol('update first'); | |
const UPDATE_SECOND = Symbol('update second'); |