corestore 7.0.2 → 7.0.3

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 +28 -4
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -136,6 +136,7 @@ class Corestore extends ReadyResource {
136
136
  this.streamTracker = this.root ? this.root.streamTracker : new StreamTracker()
137
137
  this.cores = this.root ? this.root.cores : new CoreTracker()
138
138
  this.sessions = new SessionTracker()
139
+ this.corestores = this.root ? this.root.corestores : new Set()
139
140
  this.globalCache = this.root ? this.root.globalCache : (opts.globalCache || null)
140
141
  this.primaryKey = this.root ? this.root.primaryKey : (opts.primaryKey || null)
141
142
  this.ns = opts.namespace || DEFAULT_NAMESPACE
@@ -147,6 +148,8 @@ class Corestore extends ReadyResource {
147
148
 
148
149
  this._ongcBound = this._ongc.bind(this)
149
150
 
151
+ if (this.root) this.corestores.add(this)
152
+
150
153
  this.ready().catch(noop)
151
154
  }
152
155
 
@@ -171,6 +174,7 @@ class Corestore extends ReadyResource {
171
174
  }
172
175
 
173
176
  session (opts) {
177
+ this._maybeClosed()
174
178
  const root = this.root || this
175
179
  return new Corestore(null, { ...opts, root })
176
180
  }
@@ -209,14 +213,22 @@ class Corestore extends ReadyResource {
209
213
  }
210
214
 
211
215
  async _close () {
212
- const sessions = []
216
+ const closing = []
213
217
  const hanging = [...this.sessions]
214
- for (const sess of hanging) sessions.push(sess.close())
218
+ for (const sess of hanging) closing.push(sess.close())
215
219
 
216
220
  if (this.watchers !== null) this.cores.unwatch(this)
217
221
 
218
- await Promise.all(sessions)
219
- if (this.root !== null) return
222
+ if (this.root !== null) {
223
+ await Promise.all(closing)
224
+ return
225
+ }
226
+
227
+ for (const store of this.corestores) {
228
+ closing.push(store.close())
229
+ }
230
+
231
+ await Promise.all(closing)
220
232
 
221
233
  const cores = []
222
234
  for (const core of this.cores) cores.push(core.close())
@@ -239,6 +251,8 @@ class Corestore extends ReadyResource {
239
251
  }
240
252
 
241
253
  replicate (isInitiator, opts) {
254
+ this._maybeClosed()
255
+
242
256
  const isExternal = isStream(isInitiator)
243
257
  const stream = Hypercore.createProtocolStream(isInitiator, {
244
258
  ...opts,
@@ -267,7 +281,15 @@ class Corestore extends ReadyResource {
267
281
  return stream
268
282
  }
269
283
 
284
+ _maybeClosed () {
285
+ if (this.closing || (this.root !== null && this.root.closing)) {
286
+ throw new Error('Corestore is closed')
287
+ }
288
+ }
289
+
270
290
  get (opts) {
291
+ this._maybeClosed()
292
+
271
293
  if (b4a.isBuffer(opts) || typeof opts === 'string') opts = { key: opts }
272
294
  if (!opts) opts = {}
273
295
 
@@ -320,6 +342,8 @@ class Corestore extends ReadyResource {
320
342
  if (this.opened === false) await this.ready()
321
343
 
322
344
  const discoveryKey = opts.name ? await this.storage.getAlias({ name: opts.name, namespace: this.ns }) : null
345
+ this._maybeClosed()
346
+
323
347
  const core = this._getCore(discoveryKey, opts)
324
348
 
325
349
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "corestore",
3
- "version": "7.0.2",
3
+ "version": "7.0.3",
4
4
  "description": "A Hypercore factory that simplifies managing collections of cores.",
5
5
  "main": "index.js",
6
6
  "files": [