corestore 7.0.6 → 7.0.8

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 +52 -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,25 @@ 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
+
214
+ suspend () {
215
+ return this.storage.db.suspend()
216
+ }
217
+
218
+ resume () {
219
+ return this.storage.db.resume()
220
+ }
221
+
177
222
  session (opts) {
178
223
  this._maybeClosed()
179
224
  const root = this.root || this
@@ -322,7 +367,7 @@ class Corestore extends ReadyResource {
322
367
  // same goes if user has defined async preload obvs
323
368
  if (opts.name || opts.preload) {
324
369
  conf.preload = this._preload(opts)
325
- return new Hypercore(null, null, conf)
370
+ return this._makeSession(conf)
326
371
  }
327
372
 
328
373
  // if not not we can sync create it, which just is easier for the
@@ -332,8 +377,13 @@ class Corestore extends ReadyResource {
332
377
  conf.core = core
333
378
  conf.sessions = this.sessions.get(core.id)
334
379
  conf.ongc = this._ongcBound
380
+ return this._makeSession(conf)
381
+ }
335
382
 
336
- return new Hypercore(null, null, conf)
383
+ _makeSession (conf) {
384
+ const session = new Hypercore(null, null, conf)
385
+ if (this._findingPeers !== null) this._findingPeers.add(session)
386
+ return session
337
387
  }
338
388
 
339
389
  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.8",
4
4
  "description": "A Hypercore factory that simplifies managing collections of cores.",
5
5
  "main": "index.js",
6
6
  "files": [