@fireproof/core 0.5.10 → 0.5.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -47,7 +47,7 @@ export function useFireproof (name = 'useFireproof', defineDatabaseFn = () => {}
47
47
  */
48
48
  const addSubscriber = (label, fn) => {
49
49
  // todo test that the label is not needed
50
- return database.subscribe(fn)
50
+ return database.registerListener(fn)
51
51
  // inboundSubscriberQueue.set(label, fn)
52
52
  }
53
53
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fireproof/core",
3
- "version": "0.5.10",
3
+ "version": "0.5.12",
4
4
  "description": "Cloudless database for apps, the browser, and IPFS",
5
5
  "main": "dist/src/fireproof.js",
6
6
  "module": "dist/src/fireproof.mjs",
package/src/blockstore.js CHANGED
@@ -164,8 +164,8 @@ export class TransactionBlockstore {
164
164
  if (doSync) {
165
165
  // const all =
166
166
  await Promise.all([...this.syncs].map(async sync => sync.sendUpdate(innerBlockstore).catch(e => {
167
- console.error('sync error', e)
168
- this.syncs.delete(sync)
167
+ console.error('sync error, cancelling', e)
168
+ sync.destroy()
169
169
  })))
170
170
  }
171
171
  }
package/src/db-index.js CHANGED
@@ -153,7 +153,7 @@ export class DbIndex {
153
153
  if (matches.length === 0) {
154
154
  matches = /=>\s*(.*)/.exec(this.mapFnString)
155
155
  }
156
- if (matches.length === 0) {
156
+ if (matches === null) {
157
157
  return this.mapFnString
158
158
  } else {
159
159
  // it's a consise arrow function, match everythign after the arrow
package/src/sync.js CHANGED
@@ -14,6 +14,7 @@ export class Sync {
14
14
  * @memberof Sync
15
15
  * @static
16
16
  */
17
+ status = 'new'
17
18
  constructor (database, PeerClass = SimplePeer) {
18
19
  this.database = database
19
20
  this.database.blocks.syncs.add(this) // should this happen during setup?
@@ -29,6 +30,7 @@ export class Sync {
29
30
  }
30
31
 
31
32
  async offer () {
33
+ this.status = 'offering'
32
34
  return this.setupPeer(true)
33
35
  }
34
36
 
@@ -36,11 +38,13 @@ export class Sync {
36
38
  const offer = JSON.parse(atob(base64offer))
37
39
  const p = this.setupPeer(false)
38
40
  this.peer.signal(offer)
41
+ this.status = 'accepting'
39
42
  return p
40
43
  }
41
44
 
42
45
  connect (base64accept) {
43
46
  const accept = JSON.parse(atob(base64accept))
47
+ this.status = 'connecting'
44
48
  this.peer.signal(accept)
45
49
  }
46
50
 
@@ -71,6 +75,7 @@ export class Sync {
71
75
  // console.log('not a car', data.toString())
72
76
  }
73
77
  if (reader) {
78
+ this.status = 'parking car'
74
79
  const blz = new Set()
75
80
  for await (const block of reader.blocks()) {
76
81
  blz.add(block)
@@ -87,23 +92,28 @@ export class Sync {
87
92
  // )
88
93
  // @ts-ignore
89
94
  reader.entries = reader.blocks
90
- await this.database.blocks.commit({
91
- label: 'sync',
92
- entries: () => [...blz],
93
- get: async cid => await reader.get(cid),
94
- lastCid: [...blz][0].cid // doesn't matter
95
- }, false)
95
+ await this.database.blocks.commit(
96
+ {
97
+ label: 'sync',
98
+ entries: () => [...blz],
99
+ get: async cid => await reader.get(cid),
100
+ lastCid: [...blz][0].cid // doesn't matter
101
+ },
102
+ false
103
+ )
96
104
  // first arg could be the roots parents?
97
105
  // get the roots parents
98
- const parents = await Promise.all(roots.map(async (cid) => {
99
- const rbl = await reader.get(cid)
100
- if (!rbl) {
101
- console.log('missing root block', cid.toString(), reader)
102
- throw new Error('missing root block')
103
- }
104
- const block = await decodeEventBlock(rbl.bytes)
105
- return block.value.parents
106
- }))
106
+ const parents = await Promise.all(
107
+ roots.map(async cid => {
108
+ const rbl = await reader.get(cid)
109
+ if (!rbl) {
110
+ console.log('missing root block', cid.toString(), reader)
111
+ throw new Error('missing root block')
112
+ }
113
+ const block = await decodeEventBlock(rbl.bytes)
114
+ return block.value.parents
115
+ })
116
+ )
107
117
  this.database.applyClock(parents.flat(), roots)
108
118
  this.database.notifyReset()
109
119
  // console.log('after', this.database.clockToJSON())
@@ -113,6 +123,7 @@ export class Sync {
113
123
  const message = JSON.parse(data.toString())
114
124
  // console.log('got message', message)
115
125
  if (message.ok) {
126
+ this.status = 'ok'
116
127
  this.pushBacklogResolve({ ok: true })
117
128
  } else if (message.clock) {
118
129
  const reqCidDiff = message
@@ -120,12 +131,14 @@ export class Sync {
120
131
  console.log('got diff', reqCidDiff)
121
132
  const carBlock = await Sync.makeCar(this.database, null, reqCidDiff.cids)
122
133
  if (!carBlock) {
123
- // we are full synced
124
- // console.log('we are full synced')
134
+ // we are full synced
135
+ // console.log('we are full synced')
136
+ this.status = 'full synced'
125
137
  this.peer.send(JSON.stringify({ ok: true }))
126
138
  // this.pushBacklogResolve({ ok: true })
127
139
  } else {
128
- // console.log('do send', carBlock.bytes.length)
140
+ // console.log('do send', carBlock.bytes.length)
141
+ this.status = 'sending diff car'
129
142
  this.peer.send(carBlock.bytes)
130
143
  // this.pushBacklogResolve({ ok: true })
131
144
  }
@@ -133,10 +146,18 @@ export class Sync {
133
146
  }
134
147
  }
135
148
 
149
+ destroy () {
150
+ this.database.blocks.syncs.delete(this)
151
+ this.status = 'destroyed'
152
+ this.peer.destroy()
153
+ }
154
+
136
155
  async sendUpdate (blockstore) {
156
+ if (!this.peer) return
137
157
  // console.log('send update from', this.database.instanceId)
138
158
  // todo should send updates since last sync
139
159
  const newCar = await blocksToCarBlock(blockstore.lastCid, blockstore)
160
+ this.status = 'sending update car'
140
161
  this.peer.send(newCar.bytes)
141
162
  }
142
163
 
@@ -149,6 +170,7 @@ export class Sync {
149
170
  cids: allCIDs.map(cid => cid.toString())
150
171
  }
151
172
  // console.log('send diff', reqCidDiff)
173
+ this.status = 'sending cid diff'
152
174
  this.peer.send(JSON.stringify(reqCidDiff))
153
175
  }
154
176