Files: 6b099fb2cf92d238df76e99ebab6e3a13c9149d0 / evolve.js
548 bytesRaw
1 | function append(ary, ary2) { |
2 | if(!Array.isArray(ary2)) |
3 | ary.push(ary2) |
4 | else |
5 | while(ary2.length) |
6 | ary.push(ary2.shift()) |
7 | return ary |
8 | } |
9 | |
10 | module.exports = function evolve (update, act, state, events) { |
11 | do { |
12 | var next = update(state, events.shift()) |
13 | state = next.state |
14 | if(Array.isArray(next.effects)) { |
15 | while(next.effects.length) |
16 | append(events, act(state, next.effects.shift())||[]) |
17 | } else if(next.effects) |
18 | append(events, act(state, next.effects) || []) |
19 | } |
20 | while(events.length) |
21 | |
22 | return state |
23 | } |
24 | |
25 |
Built with git-ssb-web