-
Notifications
You must be signed in to change notification settings - Fork 1
/
dev.js
49 lines (41 loc) · 1.69 KB
/
dev.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
import Observable from 'zen-observable'
import Kefir from 'kefir'
import reswap, { reducer, action, merge, combine } from './src'
const assign = (...values) => Object.assign({}, ...values)
const testAction = action((data) => new Promise((resolve) => {
setTimeout(() => resolve(data + 1), 5000)
}))
const testObservable = Kefir.sequentially(3000, ['bar', 'babar', 'babazbar'])
const testObservable2 = Kefir.sequentially(4000, [1, 2, 3, 4, 5])
const testObservable3 = new Observable((observer) => {
testAction.subscribe({
next: (value) => observer.next(value + 1)
})
})
//global config, possible adapters (Kefir, RXJS, etc)
reswap.config({
debug: true,
to: (observable) => Kefir.fromESObservable(observable), //all returned observables by reswap are automatically converted to kefir streams
})
const store = reswap.store({ foo: '', bar: { baz: '' }, plus1: 0, plus2: 0 },
reducer('foo', (currentState, value, value2) => assign(currentState, { foo: currentState.foo + value + value2 })),
reducer(combine([testObservable, testObservable2]), (currentState, value) =>
assign(currentState, { bar: assign(currentState.bar, { baz: value }) })),
reducer(combine([testAction, testObservable3]), (currentState, [plus1, plus2]) =>
assign(currentState, { plus1, plus2 }))
)
setTimeout(() => {
let i = 0;
let interval = setInterval(() => {
if (++i === 4) {
clearInterval(interval)
}
store.reducers.foo('test', 'wadafaaak')
}, 1500)
}, 500)
store.onValue((value) => {
const node = document.createElement('pre')
node.appendChild(document.createTextNode(JSON.stringify(value)))
document.body.appendChild(node)
})
testAction(1)