Browse Source

use thunky instead

Kieran Gibb 2 years ago
parent
commit
ddb6a79c06
No known key found for this signature in database
4 changed files with 16 additions and 17 deletions
  1. 10
    14
      index.js
  2. 2
    2
      package.json
  3. 3
    0
      test/index.test.js
  4. 1
    1
      yarn.lock

+ 10
- 14
index.js View File

@@ -1,7 +1,7 @@
1 1
 const Base = require('cobox-group-base')
2 2
 const swarm = require('cobox-swarm')
3
-const maybe = require('call-me-maybe')
4 3
 const assert = require('assert')
4
+const thunky = require('thunky')
5 5
 
6 6
 module.exports = (storage, address, opts = {}) => (
7 7
   new BlindReplicator(storage, address, opts)
@@ -11,23 +11,11 @@ class BlindReplicator extends Base {
11 11
   constructor (storage, address, opts = {}) {
12 12
     super(storage, address, opts)
13 13
     this._initFeeds(opts)
14
-    this._readyCallback = this.multifeed.ready.bind(this.multifeed)
14
+    this._readyCallback = thunky(this._ready.bind(this))
15 15
     this._connection = null
16 16
     this._isSwarming = true
17 17
   }
18 18
 
19
-  ready (callback) {
20
-    var self = this
21
-    return maybe(callback, new Promise((resolve, reject) => {
22
-      self._readyCallback((err) => {
23
-        if (err) return reject(err)
24
-        self.discoveryKey = self.multifeed._root.discoveryKey
25
-        self._isReady = true
26
-        return resolve()
27
-      })
28
-    }))
29
-  }
30
-
31 19
   swarm (opts = {}) {
32 20
     this._connection = swarm(this, Object.assign(opts, {
33 21
       logger: this.config.logger
@@ -52,5 +40,13 @@ class BlindReplicator extends Base {
52 40
     })
53 41
     return this.config.save()
54 42
   }
43
+
44
+  _ready (callback) {
45
+    var self = this
46
+    self.multifeed.ready((err) => {
47
+      self.discoveryKey = self.multifeed._root.discoveryKey
48
+      callback()
49
+    })
50
+  }
55 51
 }
56 52
 

+ 2
- 2
package.json View File

@@ -17,9 +17,9 @@
17 17
   "license": "AGPL-3.0-or-later",
18 18
   "dependencies": {
19 19
     "assert": "^2.0.0",
20
-    "call-me-maybe": "^1.0.1",
21 20
     "cobox-group-base": "git+https://ledger-git.dyne.org/cobox/cobox-group-base",
22
-    "cobox-swarm": "git+https://ledger-git.dyne.org/cobox/cobox-swarm"
21
+    "cobox-swarm": "git+https://ledger-git.dyne.org/cobox/cobox-swarm",
22
+    "thunky": "^1.1.0"
23 23
   },
24 24
   "devDependencies": {
25 25
     "nyc": "^14.1.1",

+ 3
- 0
test/index.test.js View File

@@ -33,6 +33,9 @@ describe('basic', (context) => {
33 33
     assert.ok(replicator._connection === null, 'connection starts as null')
34 34
 
35 35
     await replicator.ready()
36
+
37
+    assert.ok(replicator.discoveryKey, 'has a discovery key')
38
+
36 39
     replicator.swarm()
37 40
 
38 41
     assert.ok(replicator._connection, 'caches a swarm connection')

+ 1
- 1
yarn.lock View File

@@ -2348,7 +2348,7 @@ thunky@^0.1.0:
2348 2348
   resolved "https://registry.yarnpkg.com/thunky/-/thunky-0.1.0.tgz#bf30146824e2b6e67b0f2d7a4ac8beb26908684e"
2349 2349
   integrity sha1-vzAUaCTituZ7Dy16Ssi+smkIaE4=
2350 2350
 
2351
-thunky@^1.0.2:
2351
+thunky@^1.0.2, thunky@^1.1.0:
2352 2352
   version "1.1.0"
2353 2353
   resolved "https://registry.yarnpkg.com/thunky/-/thunky-1.1.0.tgz#5abaf714a9405db0504732bbccd2cedd9ef9537d"
2354 2354
   integrity sha512-eHY7nBftgThBqOyHGVN+l8gF0BucP09fMo0oO/Lb0w1OF80dJv+lDVpXG60WMQvkcxAkNybKsrEIE3ZtKGmPrA==