-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathmain.js
48 lines (41 loc) · 1.3 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/* jshint esnext: true */
const R = require('ramda');
const Type = require('union-type');
const patch = require('snabbdom').init([
require('snabbdom/modules/class'),
require('snabbdom/modules/props'),
require('snabbdom/modules/eventlisteners'),
require('snabbdom/modules/style'),
]);
const h = require('snabbdom/h');
// Update
const Action = Type({Increment: [], Decrement: []});
const update = (model, action) => Action.case({
Increment: () => model + 1,
Decrement: () => model - 1,
}, action);
// View
const view = R.curry((actions$, model) =>
h('div', {style: countStyle}, [
h('button', {on: {click: [actions$, Action.Decrement()]}}, '–'),
h('div', {style: countStyle}, model),
h('button', {on: {click: [actions$, Action.Increment()]}}, '+'),
]));
const countStyle = {
fontSize: '20px',
fontFamily: 'monospace',
width: '50px',
textAlign: 'center',
};
const main = (oldState, oldVnode, {view, update}) => {
const newVnode = view((action) => {
const newState = update(oldState, action);
main(newState, newVnode, {view, update});
}, oldState);
patch(oldVnode, newVnode);
};
// Begin rendering when the DOM is ready
window.addEventListener('DOMContentLoaded', () => {
const container = document.getElementById('container');
main(0, container, {view, update});
});