corestore 7.0.6 → 7.0.7

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 +44 -2
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -127,6 +127,31 @@ class CoreTracker extends EventEmitter {
127
127
  }
128
128
  }
129
129
 
130
+ class FindingPeers {
131
+ constructor () {
132
+ this.count = 0
133
+ this.pending = []
134
+ }
135
+
136
+ add (core) {
137
+ if (this.count === 0) return
138
+ this.pending.push(core.findingPeers())
139
+ }
140
+
141
+ inc (sessions) {
142
+ if (++this.count !== 1) return
143
+
144
+ for (const core of sessions) {
145
+ this.pending.push(core.findingPeers())
146
+ }
147
+ }
148
+
149
+ dec (sessions) {
150
+ if (--this.count !== 0) return
151
+ while (this.pending.length > 0) this.pending.pop()()
152
+ }
153
+ }
154
+
130
155
  class Corestore extends ReadyResource {
131
156
  constructor (storage, opts = {}) {
132
157
  super()
@@ -147,6 +172,7 @@ class Corestore extends ReadyResource {
147
172
 
148
173
  this.manifestVersion = 1 // just compat
149
174
 
175
+ this._findingPeers = null // here for legacy
150
176
  this._ongcBound = this._ongc.bind(this)
151
177
 
152
178
  if (this.root) this.corestores.add(this)
@@ -174,6 +200,17 @@ class Corestore extends ReadyResource {
174
200
  }
175
201
  }
176
202
 
203
+ findingPeers () {
204
+ if (this._findingPeers === null) this._findingPeers = new FindingPeers()
205
+ this._findingPeers.inc(this.sessions)
206
+ let done = false
207
+ return () => {
208
+ if (done) return
209
+ done = true
210
+ this._findingPeers.dec(this.sessions)
211
+ }
212
+ }
213
+
177
214
  session (opts) {
178
215
  this._maybeClosed()
179
216
  const root = this.root || this
@@ -322,7 +359,7 @@ class Corestore extends ReadyResource {
322
359
  // same goes if user has defined async preload obvs
323
360
  if (opts.name || opts.preload) {
324
361
  conf.preload = this._preload(opts)
325
- return new Hypercore(null, null, conf)
362
+ return this._makeSession(conf)
326
363
  }
327
364
 
328
365
  // if not not we can sync create it, which just is easier for the
@@ -332,8 +369,13 @@ class Corestore extends ReadyResource {
332
369
  conf.core = core
333
370
  conf.sessions = this.sessions.get(core.id)
334
371
  conf.ongc = this._ongcBound
372
+ return this._makeSession(conf)
373
+ }
335
374
 
336
- return new Hypercore(null, null, conf)
375
+ _makeSession (conf) {
376
+ const session = new Hypercore(null, null, conf)
377
+ if (this._findingPeers !== null) this._findingPeers.add(session)
378
+ return session
337
379
  }
338
380
 
339
381
  async createKeyPair (name, ns = this.ns) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "corestore",
3
- "version": "7.0.6",
3
+ "version": "7.0.7",
4
4
  "description": "A Hypercore factory that simplifies managing collections of cores.",
5
5
  "main": "index.js",
6
6
  "files": [