stores/ledger.jsView |
---|
1 | 1 … | const Flume = require('flumedb') |
2 | 2 … | const Reduce = require('flumeview-reduce') |
3 | | - |
4 | 3 … | const FlumeLog = require('flumelog-offset') |
5 | | -const {pull, drain} = require('pull-stream') |
| 4 … | +const {pull, once, drain} = require('pull-stream') |
| 5 … | +const codec = require('flumecodec') |
6 | 6 … | |
7 | 7 … | function ledger (state, emitter) { |
8 | 8 … | state.accounts = {} |
9 | 9 … | state.entries = [] |
10 | 10 … | |
11 | | - const db = Flume(FlumeLog('.test.db')) |
12 | | - .use('entries', Reduce(1, entriesReducer, null, null, state.entries)) |
13 | | - .use('accounts', Reduce(1, accountReducer, null, null, state.accounts)) |
14 | | - .use('balances', Reduce(1, accountBalanceReducer, null, null, state.accounts)) |
| 11 … | + const db = Flume(FlumeLog('.test.db', {codec: codec.json})) |
| 12 … | + .use('entries', Reduce( 1, entriesReducer, null, null, [])) |
| 13 … | + .use('accounts', Reduce(1, accountReducer, null, null, {})) |
15 | 14 … | |
16 | | - state.db = db |
17 | | - state.helpers = {pull, drain} |
| 15 … | + pull(db.accounts.stream({live: true}), drain(acct => console.log('account stream:', acct))) |
| 16 … | + pull(db.entries.stream({live: true}), drain(entry => console.log('entries stream:', entry))) |
18 | 17 … | |
19 | | - pull(db.accounts.stream({live: true}), drain(acct => console.log('added:', acct))) |
| 18 … | + const onDone = () => console.log('DONE') |
| 19 … | + pull(db.accounts.stream(), drain(acctState => { |
| 20 … | + console.log('ACCOUNT STATE', acctState) |
| 21 … | + state.accounts = acctState |
| 22 … | + emitter.emit.RENDER |
| 23 … | + }, onDone)) |
| 24 … | + pull(db.entries.stream(), drain(entryState => { |
| 25 … | + console.log('ENTRY STATE', entryState) |
| 26 … | + state.entries = entryState |
| 27 … | + emitter.emit.RENDER |
| 28 … | + }, onDone)) |
20 | 29 … | |
21 | 30 … | function onLoad () { |
22 | 31 … | emitter.on('accounts', onEvent) |
23 | 32 … | emitter.on('entries', onEvent) |
24 | 33 … | } |
25 | 34 … | |
26 | 35 … | function onEvent (data) { |
27 | | - console.log('event') |
28 | 36 … | db.append(data, (err, seq) => { |
29 | 37 … | if (err) throw err |
30 | | - console.log('append') |
31 | 38 … | emitter.emit(state.events.RENDER) |
32 | 39 … | }) |
33 | 40 … | } |
34 | 41 … | |
35 | 42 … | emitter.on('DOMContentLoaded', onLoad) |
36 | | -} |
37 | 43 … | |
38 | | -function accountBalanceReducer (acc, item) { |
39 | | - console.log('balance') |
40 | | - if (item.type !== 'entries') return acc |
41 | | - const {msg} = item |
| 44 … | + function entriesReducer (acc, item) { |
| 45 … | + if (item.type !== 'entries') return acc |
| 46 … | + acc.push(item) |
42 | 47 … | |
43 | | - acc[msg.debit].debit += msg.amount |
44 | | - acc[msg.credit].credit += msg.amount |
45 | | - return acc |
46 | | -} |
| 48 … | + return acc |
| 49 … | + } |
47 | 50 … | |
48 | | -function entriesReducer (acc, item) { |
49 | | - console.log('entries') |
50 | | - if (item.type !== 'entries') return acc |
51 | | - acc.push(item) |
52 | | - return acc |
53 | | -} |
| 51 … | + function accountReducer (acc, item) { |
| 52 … | + if (item.type === 'entries') { |
| 53 … | + const {msg} = item |
| 54 … | + const debit = acc[msg.debit].debit += msg.amount |
| 55 … | + const credit = acc[msg.credit].credit += msg.amount |
54 | 56 … | |
55 | | -function accountReducer (acc, item) { |
56 | | - console.log('account') |
57 | | - if (item.type !== 'accounts') return acc |
58 | | - const { msg } = item |
59 | | - console.log(acc, item) |
| 57 … | + return acc |
| 58 … | + } |
| 59 … | + if (item.type !== 'accounts') return acc |
| 60 … | + const { msg } = item |
60 | 61 … | |
61 | | - if (!acc[msg.name]) { |
62 | | - acc[msg.name] = msg |
| 62 … | + if (!acc[msg.name]) { |
| 63 … | + acc[msg.name] = msg |
| 64 … | + |
| 65 … | + return acc |
| 66 … | + } |
| 67 … | + |
| 68 … | + Object.assign(acc[msg.name], msg) |
| 69 … | + Object.assign(state.accounts[msg.name], msg) |
63 | 70 … | return acc |
64 | 71 … | } |
65 | | - |
66 | | - Object.assign(acc[msg.name], msg) |
67 | | - return acc |
68 | 72 … | } |
69 | 73 … | |
| 74 … | + |
70 | 75 … | module.exports = ledger |