git ssb

0+

punkmonk / double



Tree: 1739ab2f646d5e903603c2b7e59b6dee0c17fd3e

Files: 1739ab2f646d5e903603c2b7e59b6dee0c17fd3e / stores / ledger.js

1227 bytesRaw
1const Flume = require('flumedb')
2const Reduce = require('flumeview-reduce')
3const FlumeLog = require('flumelog-memory')
4
5function ledger (state, emitter) {
6 state.accounts = {}
7 state.transactions = []
8
9 const db = Flume(FlumeLog())
10 .use('transactions', Reduce(
11 1,
12 transactionReducer,
13 null,
14 null,
15 state.transactions))
16 .use('account', Reduce(
17 1,
18 accountReducer,
19 null,
20 null,
21 state.accounts))
22
23 state.db = db
24
25 function transactionReducer (acc, item) {
26 if (item.type !== 'transaction') return acc
27 acc.push(item)
28 return acc
29 }
30
31 function accountReducer (acc, item) {
32 if (item.type !== 'accounts') return acc
33 const {msg} = item
34 switch (msg.type) {
35 case 'TOGGLE':
36 acc[msg.name].archived = !acc[msg.name].archived
37 return acc
38 default:
39 acc[msg.name] = msg
40 return acc
41 }
42 }
43
44 function onLoad () {
45 emitter.on('account', onEvent)
46 emitter.on('transaction:add', onEvent)
47 }
48
49 function onEvent (data) {
50 db.append(data, (err, seq) => {
51 if (err) throw err
52 emitter.emit(state.events.RENDER)
53 })
54 }
55
56 emitter.on('DOMContentLoaded', onLoad)
57}
58
59module.exports = ledger
60

Built with git-ssb-web