hyperbee2 2.6.1 → 2.7.1

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 +24 -6
  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
  }
@@ -89,7 +94,9 @@ class Hyperbee {
89
94
  root,
90
95
  view: true,
91
96
  writable,
92
- unbatch
97
+ unbatch,
98
+ autoUpdate: false,
99
+ preload: this.preload
93
100
  })
94
101
  }
95
102
 
@@ -105,6 +112,7 @@ class Hyperbee {
105
112
  this.context = context
106
113
  this.writable = writable
107
114
  this.root = root
115
+ this.emit('update')
108
116
  }
109
117
 
110
118
  snapshot() {
@@ -123,22 +131,30 @@ class Hyperbee {
123
131
  async ready() {
124
132
  if (!this.core.opened) await this.core.ready()
125
133
  if (this.root) return
134
+ if (this.preload) await this.preload()
135
+ if (this.root) return
126
136
 
127
137
  this.root =
128
138
  this.context.core.length === 0
129
139
  ? EMPTY
130
140
  : this.context.createTreeNode(0, this.core.length - 1, 0, false, null)
131
141
 
132
- if (this._autoUpdate) {
142
+ if (this.autoUpdate) {
133
143
  this.core.on('append', () => {
134
144
  this.update()
135
145
  })
136
146
  }
147
+
148
+ this.emit('ready')
137
149
  }
138
150
 
139
151
  async close() {
152
+ if (!this.root) await this.ready()
140
153
  if (this.config.activeRequests.length) Hypercore.clearRequests(this.config.activeRequests)
141
- if (!this.view) await this.store.close()
154
+ if (this.view) return
155
+ if (this.store.closing) return this.store.close()
156
+ await this.store.close()
157
+ this.emit('close')
142
158
  }
143
159
 
144
160
  createReadStream(options) {
@@ -280,12 +296,14 @@ class Hyperbee {
280
296
  this.context = context
281
297
  this.root = length === 0 ? EMPTY : context.createTreeNode(0, length - 1, 0, false, null)
282
298
  this.unbatch = 0
299
+ this.emit('update')
283
300
  }
284
301
  }
285
302
 
286
303
  update(root = null) {
287
304
  this.root = root
288
305
  this.unbatch = 0
306
+ this.emit('update')
289
307
  }
290
308
 
291
309
  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.1",
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",