Skip to content

Instantly share code, notes, and snippets.

View yelouafi's full-sized avatar

Yassine Elouafi yelouafi

View GitHub Profile
function init() {
return 0;
}
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);
}
const Action = Type({
Add : [],
Remove : [Number],
Reset : [],
Update : [Number, counter.Action],
});
function update(count, action) {
return Action.case({
Increment : () => count + 1,
Decrement : () => count - 1
}, action);
}
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}`),
]);
import Type from 'union-type';
const Action = Type({
Increment : [],
Decrement : []
});
function updateCounter(model, id, action) {
return {...model,
counters : model.counters.map(item =>
item.id !== id ?
item
: { ...item,
counter : counter.update(item.counter, action)
}
)
};
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')
]);
}
//{ first : counter.model, second : counter.model }
const RESET = Symbol('reset');
const UPDATE_FIRST = Symbol('update first');
const UPDATE_SECOND = Symbol('update second');
/*
model : {
counters: [{id: Number, counter: counter.model}],
nextID : Number
}
*/
const ADD = Symbol('add');
const UPDATE = Symbol('update counter');
const REMOVE = Symbol('remove');