passive-core-watcher 0.0.2 → 1.0.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 (3) hide show
  1. package/README.md +8 -8
  2. package/index.js +19 -11
  3. package/package.json +14 -4
package/README.md CHANGED
@@ -10,7 +10,7 @@ npm i passive-core-watcher
10
10
 
11
11
  #### `const watcher = new PassiveCoreWatcher(corestore, { watch, open })`
12
12
 
13
- Create a new passive core watcher.
13
+ Create a new passive core watcher, and start watching new cores. Existing cores are also processed (e.g. `open` will be called for all existing cores for which `watch` returns true).
14
14
 
15
15
  `corestore` is a Corestore
16
16
 
@@ -22,13 +22,7 @@ The session emits a `close` event when it is closing. If teardown of the side ef
22
22
 
23
23
  `session.on('close', () => { /* run teardown logic for side effects */ } )`
24
24
 
25
- #### `await watcher.ready()`
26
-
27
- Setup the passive watcher, so it starts listening for new hypercores.
28
-
29
- This also checks which of the already-opened hypercores need to be watched, and calls the `open` function for those.
30
-
31
- #### `await watcher.close()`
25
+ #### `watcher.destroy()`
32
26
 
33
27
  Stops watching the corestore for new cores, and closes all weak sessions.
34
28
 
@@ -39,3 +33,9 @@ Stops watching the corestore for new cores, and closes all weak sessions.
39
33
  If the hypercore is open in the corestore, it is added to the watcher lifecycle, and the `open` method is called (does nothing otherwise).
40
34
 
41
35
  Useful when the `watch` condition might be false when the core first opens, but became true later.
36
+
37
+ #### `watcher.on('oncoreopen-error', e)`
38
+
39
+ Emitted when an error is thrown while a new core is being processed (for example during the passed-in `watch` or `open` functions). Useful for debugging and logging.
40
+
41
+ `e` is an error object.
package/index.js CHANGED
@@ -1,31 +1,38 @@
1
- const ReadyResource = require('ready-resource')
1
+ const { EventEmitter } = require('events')
2
2
  const Hypercore = require('hypercore')
3
3
  const b4a = require('b4a')
4
4
  const HypCrypto = require('hypercore-crypto')
5
5
  const IdEnc = require('hypercore-id-encoding')
6
+ const safetyCatch = require('safety-catch')
6
7
 
7
- class PassiveCoreWatcher extends ReadyResource {
8
+ class PassiveCoreWatcher extends EventEmitter {
8
9
  constructor (corestore, { watch, open }) {
9
10
  super()
10
-
11
11
  this.store = corestore
12
12
  this.watch = watch
13
13
  this.open = open
14
14
 
15
15
  this._oncoreopenBound = this._oncoreopen.bind(this)
16
16
  this._openCores = new Map()
17
- }
18
17
 
19
- async _open () {
20
- this.store.watch(this._oncoreopenBound)
21
- await Promise.all(
22
- [...this.store.cores.map.values()].map(this._oncoreopenBound)
23
- )
18
+ this.destroyed = false
19
+
20
+ // Give time to add event handlers
21
+ queueMicrotask(() => {
22
+ if (this.destroyed) return
23
+ this.store.watch(this._oncoreopenBound)
24
+ for (const core of this.store.cores.map.values()) {
25
+ this._oncoreopenBound(core) // never rejects
26
+ }
27
+ })
24
28
  }
25
29
 
26
- async _close () {
30
+ destroy () {
31
+ this.destroyed = true
27
32
  this.store.unwatch(this._oncoreopenBound)
28
- await Promise.allSettled([...this._openCores.values()].map(c => c.close()))
33
+ for (const core of this._openCores.values()) {
34
+ core.close().catch(safetyCatch)
35
+ }
29
36
  }
30
37
 
31
38
  async _oncoreopen (core) { // not allowed to throw
@@ -59,6 +66,7 @@ class PassiveCoreWatcher extends ReadyResource {
59
66
 
60
67
  session.on('close', () => {
61
68
  this._openCores.delete(discKey)
69
+ this.emit('gc', discKey)
62
70
  })
63
71
 
64
72
  await this.open(session)
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "passive-core-watcher",
3
- "version": "0.0.2",
3
+ "version": "1.0.1",
4
4
  "description": "Run conditional logic on a corestore's hypercores",
5
5
  "main": "index.js",
6
6
  "scripts": {
7
- "test": "standard"
7
+ "test": "standard && brittle test.js"
8
8
  },
9
9
  "repository": {
10
10
  "type": "git",
@@ -13,6 +13,12 @@
13
13
  "files": [
14
14
  "index.js"
15
15
  ],
16
+ "imports": {
17
+ "events": {
18
+ "bare": "bare-events",
19
+ "default": "events"
20
+ }
21
+ },
16
22
  "keywords": [
17
23
  "Corestore",
18
24
  "hypercore"
@@ -24,13 +30,17 @@
24
30
  },
25
31
  "homepage": "https://github.com/holepunchto/passive-core-watcher#readme",
26
32
  "devDependencies": {
27
- "standard": "^17.1.2"
33
+ "brittle": "^3.10.0",
34
+ "corestore": "^7.0.0",
35
+ "standard": "^17.1.2",
36
+ "test-tmp": "^1.4.0"
28
37
  },
29
38
  "dependencies": {
30
39
  "b4a": "^1.6.7",
40
+ "bare-events": "^2.5.4",
31
41
  "hypercore": "^11.0.0",
32
42
  "hypercore-crypto": "^3.4.2",
33
43
  "hypercore-id-encoding": "^1.3.0",
34
- "ready-resource": "^1.1.1"
44
+ "safety-catch": "^1.0.2"
35
45
  }
36
46
  }