hyperbee2 2.6.1 → 2.7.0

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.
Files changed (2) hide show
  1. package/index.js +21 -5
  2. package/package.json +9 -2
package/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  const b4a = require('b4a')
2
2
  const Hypercore = require('hypercore')
3
+ const EventEmitter = require('events')
3
4
  const { RangeStream } = require('./lib/ranges.js')
4
5
  const { DiffStream } = require('./lib/diff.js')
5
6
  const { ChangesStream } = require('./lib/changes.js')
@@ -10,8 +11,10 @@ const SessionConfig = require('./lib/session-config.js')
10
11
  const { Pointer, KeyPointer, ValuePointer, TreeNode, EMPTY } = require('./lib/tree.js')
11
12
  const { DeltaOp, DeltaCohort, OP_COHORT } = require('./lib/compression.js')
12
13
 
13
- class Hyperbee {
14
+ class Hyperbee extends EventEmitter {
14
15
  constructor(store, options = {}) {
16
+ super()
17
+
15
18
  const {
16
19
  t = 128, // legacy number for now, should be 128 now
17
20
  key = null,
@@ -27,7 +30,8 @@ class Hyperbee {
27
30
  view = false,
28
31
  writable = true,
29
32
  unbatch = 0,
30
- autoUpdate = false
33
+ autoUpdate = false,
34
+ preload = null
31
35
  } = options
32
36
 
33
37
  this.store = store
@@ -38,7 +42,8 @@ class Hyperbee {
38
42
  this.writable = writable
39
43
  this.unbatch = unbatch
40
44
 
41
- this._autoUpdate = autoUpdate
45
+ this.autoUpdate = autoUpdate
46
+ this.preload = preload
42
47
 
43
48
  this.ready().catch(noop)
44
49
  }
@@ -105,6 +110,7 @@ class Hyperbee {
105
110
  this.context = context
106
111
  this.writable = writable
107
112
  this.root = root
113
+ this.emit('update')
108
114
  }
109
115
 
110
116
  snapshot() {
@@ -123,22 +129,30 @@ class Hyperbee {
123
129
  async ready() {
124
130
  if (!this.core.opened) await this.core.ready()
125
131
  if (this.root) return
132
+ if (this.preload) await this.preload()
133
+ if (this.root) return
126
134
 
127
135
  this.root =
128
136
  this.context.core.length === 0
129
137
  ? EMPTY
130
138
  : this.context.createTreeNode(0, this.core.length - 1, 0, false, null)
131
139
 
132
- if (this._autoUpdate) {
140
+ if (this.autoUpdate) {
133
141
  this.core.on('append', () => {
134
142
  this.update()
135
143
  })
136
144
  }
145
+
146
+ this.emit('ready')
137
147
  }
138
148
 
139
149
  async close() {
150
+ if (!this.root) await this.ready()
140
151
  if (this.config.activeRequests.length) Hypercore.clearRequests(this.config.activeRequests)
141
- if (!this.view) await this.store.close()
152
+ if (this.view) return
153
+ if (this.store.closing) return this.store.close()
154
+ await this.store.close()
155
+ this.emit('close')
142
156
  }
143
157
 
144
158
  createReadStream(options) {
@@ -280,12 +294,14 @@ class Hyperbee {
280
294
  this.context = context
281
295
  this.root = length === 0 ? EMPTY : context.createTreeNode(0, length - 1, 0, false, null)
282
296
  this.unbatch = 0
297
+ this.emit('update')
283
298
  }
284
299
  }
285
300
 
286
301
  update(root = null) {
287
302
  this.root = root
288
303
  this.unbatch = 0
304
+ this.emit('update')
289
305
  }
290
306
 
291
307
  async get(key, opts) {
package/package.json CHANGED
@@ -1,13 +1,19 @@
1
1
  {
2
2
  "name": "hyperbee2",
3
- "version": "2.6.1",
4
- "description": "btree",
3
+ "version": "2.7.0",
4
+ "description": "Scalable P2P BTree",
5
5
  "main": "index.js",
6
6
  "files": [
7
7
  "index.js",
8
8
  "lib/*.js",
9
9
  "spec"
10
10
  ],
11
+ "imports": {
12
+ "events": {
13
+ "bare": "bare-events",
14
+ "default": "events"
15
+ }
16
+ },
11
17
  "scripts": {
12
18
  "format": "prettier . --write",
13
19
  "lint": "lunte && prettier . --check",
@@ -17,6 +23,7 @@
17
23
  },
18
24
  "dependencies": {
19
25
  "b4a": "^1.6.7",
26
+ "bare-events": "^2.8.2",
20
27
  "compact-encoding": "^2.16.1",
21
28
  "hypercore": "^11.15.0",
22
29
  "hypercore-errors": "^1.5.0",