-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
66 lines (47 loc) · 1.57 KB
/
index.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// uncomponent - no imports - no external dependencies!
const reducer = (state, action) =>
state + action
// pure with no dependencies
const view = ({ button }) => (state, dispatch) => {
const change = amount =>
button(
{onclick: () => dispatch(amount)},
(amount > 0)
? `+${amount}`
: `-${-amount}`
)
return [
`Increasing by 5 every second: `,
change(10),
` ${state} `,
change(-10)
]
}
// the only basic method you need to import from 'un.js'
// const createMount = require('../../')
const createMount = require('un.js')
// configure the 'mount' method, the only method we need
const mount = createMount({
// your favorite stream factory
// TODO: flyd, most, xstream
// createStream: require("flyd"),
createStream: require("mithril/stream"),
// your favorite element creator
// TODO: (React|Preact|Inferno).createElement, snabbdom/h, hyperscript
createElement: require('mithril'),
// your favorite create tags helpers
createTags: require('hyperscript-helpers'),
// TODO: (React|Preact|Inferno).render, snabbdom-patch, replaceWith
createRender: element => vnode => require('mithril').render(element, vnode)
})
// create dom element
const el = document.createElement('div')
document.body.appendChild(el)
// mount our live uncomponent and get back its writeable stream of actions
const { actions } = mount({ el, reducer, view, initState: 0 })
// --- Some more action - generate periodic actions
const delayedConstant = (val, delay) => stream => {
setInterval(() => stream(val), delay)
return stream
}
delayedConstant(5, 1000)(actions)