git ssb

0+

punkmonk / double



Commit 2fc95bb03dc629547fc2632a480d945bf1b89cef

working version with flumelog-offset

austinfrey committed on 11/1/2018, 9:18:09 PM
Parent: 741b824742f41b309cb66ec85a4a02d5616f1e8b

Files changed

.gitignorechanged
package-lock.jsonchanged
package.jsonchanged
stores/ledger.jschanged
.gitignoreView
@@ -5,4 +5,6 @@
55 tmp/
66 npm-debug.log*
77 .DS_Store
88 .test.db
9 +entries.json
10 +accounts.json
package-lock.jsonView
The diff is too large to show. Use a local git client to view these changes.
Old file size: 384353 bytes
New file size: 384244 bytes
package.jsonView
@@ -12,8 +12,9 @@
1212 },
1313 "dependencies": {
1414 "choo": "^6.13.0",
1515 "choo-service-worker": "^2.4.0",
16 + "flumecodec": "0.0.1",
1617 "flumedb": "^1.0.0",
1718 "flumelog-offset": "^3.3.2",
1819 "flumeview-reduce": "^1.3.14",
1920 "hyperscript": "^2.0.2",
stores/ledger.jsView
@@ -1,70 +1,75 @@
11 const Flume = require('flumedb')
22 const Reduce = require('flumeview-reduce')
3-//const FlumeLog = require('flumelog-memory')
43 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')
66
77 function ledger (state, emitter) {
88 state.accounts = {}
99 state.entries = []
1010
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, {}))
1514
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)))
1817
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))
2029
2130 function onLoad () {
2231 emitter.on('accounts', onEvent)
2332 emitter.on('entries', onEvent)
2433 }
2534
2635 function onEvent (data) {
27- console.log('event')
2836 db.append(data, (err, seq) => {
2937 if (err) throw err
30- console.log('append')
3138 emitter.emit(state.events.RENDER)
3239 })
3340 }
3441
3542 emitter.on('DOMContentLoaded', onLoad)
36-}
3743
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)
4247
43- acc[msg.debit].debit += msg.amount
44- acc[msg.credit].credit += msg.amount
45- return acc
46-}
48 + return acc
49 + }
4750
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) { // TODO account for account creation of existing account
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
5456
55-function accountReducer (acc, item) { // TODO account for account creation of existing account
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
6061
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)
6370 return acc
6471 }
65-
66- Object.assign(acc[msg.name], msg)
67- return acc
6872 }
6973
74 +
7075 module.exports = ledger

Built with git-ssb-web