corestore 7.9.1 → 7.9.2

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 +20 -6
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -204,6 +204,7 @@ class FindingPeers {
204
204
  constructor() {
205
205
  this.count = 0
206
206
  this.pending = []
207
+ this.destroyed = false
207
208
  }
208
209
 
209
210
  add(core) {
@@ -212,7 +213,7 @@ class FindingPeers {
212
213
  }
213
214
 
214
215
  inc(sessions) {
215
- if (++this.count !== 1) return
216
+ if (this.destroyed || ++this.count !== 1) return
216
217
 
217
218
  for (const core of sessions) {
218
219
  this.pending.push(core.findingPeers())
@@ -220,7 +221,12 @@ class FindingPeers {
220
221
  }
221
222
 
222
223
  dec(sessions) {
223
- if (--this.count !== 0) return
224
+ if (this.destroyed || --this.count !== 0) return
225
+ while (this.pending.length > 0) this.pending.pop()()
226
+ }
227
+
228
+ destroy() {
229
+ this.destroyed = true
224
230
  while (this.pending.length > 0) this.pending.pop()()
225
231
  }
226
232
  }
@@ -262,8 +268,6 @@ class Corestore extends ReadyResource {
262
268
  )
263
269
  }
264
270
 
265
- if (this.root) this.corestores.add(this)
266
-
267
271
  this.ready().catch(noop)
268
272
  }
269
273
 
@@ -292,7 +296,7 @@ class Corestore extends ReadyResource {
292
296
  this._findingPeers.inc(this.sessions)
293
297
  let done = false
294
298
  return () => {
295
- if (done) return
299
+ if (done || !this._findingPeers) return
296
300
  done = true
297
301
  this._findingPeers.dec(this.sessions)
298
302
  }
@@ -352,6 +356,7 @@ class Corestore extends ReadyResource {
352
356
 
353
357
  async _open() {
354
358
  if (this.root !== null) {
359
+ this.corestores.add(this)
355
360
  if (this.root.opened === false) await this.root.ready()
356
361
  this.primaryKey = this.root.primaryKey
357
362
  return
@@ -374,10 +379,19 @@ class Corestore extends ReadyResource {
374
379
  const hanging = [...this.sessions]
375
380
  for (const sess of hanging) closing.push(sess.close())
376
381
 
377
- if (this.watchers !== null) this.cores.unwatch(this)
382
+ if (this.watchers !== null) {
383
+ this.cores.unwatch(this)
384
+ this.watchers = null
385
+ }
386
+
387
+ if (this._findingPeers !== null) {
388
+ this._findingPeers.destroy()
389
+ this._findingPeers = null
390
+ }
378
391
 
379
392
  if (this.root !== null) {
380
393
  await Promise.all(closing)
394
+ this.corestores.delete(this)
381
395
  return
382
396
  }
383
397
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "corestore",
3
- "version": "7.9.1",
3
+ "version": "7.9.2",
4
4
  "description": "A Hypercore factory that simplifies managing collections of cores.",
5
5
  "main": "index.js",
6
6
  "files": [