stores/ledger.jsView |
---|
1 | 1 … | const Flume = require('flumedb') |
2 | 2 … | const Reduce = require('flumeview-reduce') |
3 | | -const FlumeLog = require('flumelog-memory') |
| 3 … | + |
| 4 … | +const FlumeLog = require('flumelog-offset') |
4 | 5 … | const {pull, drain} = require('pull-stream') |
5 | 6 … | |
6 | 7 … | function ledger (state, emitter) { |
7 | 8 … | state.accounts = {} |
|
8 | 9 … | state.entries = [] |
9 | 10 … | |
10 | | - const db = Flume(FlumeLog()) |
| 11 … | + const db = Flume(FlumeLog('.test.db')) |
11 | 12 … | .use('entries', Reduce(1, entriesReducer, null, null, state.entries)) |
12 | 13 … | .use('accounts', Reduce(1, accountReducer, null, null, state.accounts)) |
13 | 14 … | .use('balances', Reduce(1, accountBalanceReducer, null, null, state.accounts)) |
14 | 15 … | |
22 | 23 … | emitter.on('entries', onEvent) |
23 | 24 … | } |
24 | 25 … | |
25 | 26 … | function onEvent (data) { |
| 27 … | + console.log('event') |
26 | 28 … | db.append(data, (err, seq) => { |
27 | 29 … | if (err) throw err |
| 30 … | + console.log('append') |
28 | 31 … | emitter.emit(state.events.RENDER) |
29 | 32 … | }) |
30 | 33 … | } |
31 | 34 … | |
32 | 35 … | emitter.on('DOMContentLoaded', onLoad) |
33 | 36 … | } |
34 | 37 … | |
35 | 38 … | function accountBalanceReducer (acc, item) { |
| 39 … | + console.log('balance') |
36 | 40 … | if (item.type !== 'entries') return acc |
37 | 41 … | const {msg} = item |
38 | 42 … | |
39 | 43 … | acc[msg.debit].debit += msg.amount |
41 | 45 … | return acc |
42 | 46 … | } |
43 | 47 … | |
44 | 48 … | function entriesReducer (acc, item) { |
| 49 … | + console.log('entries') |
45 | 50 … | if (item.type !== 'entries') return acc |
46 | 51 … | acc.push(item) |
47 | 52 … | return acc |
48 | 53 … | } |
49 | 54 … | |
50 | 55 … | function accountReducer (acc, item) { |
| 56 … | + console.log('account') |
51 | 57 … | if (item.type !== 'accounts') return acc |
52 | 58 … | const { msg } = item |
| 59 … | + console.log(acc, item) |
53 | 60 … | |
54 | 61 … | if (!acc[msg.name]) { |
55 | 62 … | acc[msg.name] = msg |
56 | 63 … | return acc |