Skip to content

Commit ec8db51

Browse files
authored
Merge pull request #49 from ChainSafe/refactor/async
refactor: async migration
2 parents 1852ec8 + 3de6f46 commit ec8db51

25 files changed

+1691
-2060
lines changed

README.md

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,29 @@ Gossipsub is an implementation of pubsub based on meshsub and floodsub. You can
2929
```javascript
3030
const Gossipsub = require('libp2p-gossipsub')
3131

32-
const gsub = new Gossipsub(node)
32+
const registrar = {
33+
handle: (multicodecs, handle) => {
34+
// register multicodec to libp2p
35+
// handle function is called everytime a remote peer opens a stream to the peer.
36+
},
37+
register: (multicodecs, handlers) => {
38+
// handlers will be used to notify pubsub of peer connection establishment or closing
39+
},
40+
unregister: (id) => {
3341

34-
gsub.start((err) => {
35-
if (err) {
36-
console.log('Upsy', err)
3742
}
38-
gsub.on('fruit', (data) => {
39-
console.log(data)
40-
})
41-
gsub.subscribe('fruit')
43+
}
4244

43-
gsub.publish('fruit', new Buffer('banana'))
45+
const gsub = new Gossipsub(peerInfo, registrar, options)
46+
47+
await gsub.start()
48+
49+
gsub.on('fruit', (data) => {
50+
console.log(data)
4451
})
52+
gsub.subscribe('fruit')
4553

54+
gsub.publish('fruit', new Buffer('banana'))
4655
```
4756

4857
## API
@@ -51,14 +60,16 @@ gsub.start((err) => {
5160

5261
```js
5362
const options = {…}
54-
const gossipsub = new Gossipsub(libp2pNode, options)
63+
const gossipsub = new Gossipsub(peerInfo, registrar, options)
5564
```
5665

5766
Options is an optional object with the following key-value pairs:
5867

5968
* **`fallbackToFloodsub`**: boolean identifying whether the node should fallback to the floodsub protocol, if another connecting peer does not support gossipsub (defaults to **true**).
6069
* **`emitSelf`**: boolean identifying whether the node should emit to self on publish, in the event of the topic being subscribed (defaults to **false**).
6170

71+
For the remaining API, see https://github.com/libp2p/js-libp2p-pubsub
72+
6273
## Contribute
6374

6475
This module is actively under development. Please check out the issues and submit PRs!

package.json

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,35 +32,33 @@
3232
"lint"
3333
],
3434
"dependencies": {
35-
"async": "^2.6.2",
36-
"err-code": "^1.1.2",
37-
"libp2p-floodsub": "~0.17.1",
38-
"libp2p-pubsub": "~0.2.0",
39-
"multistream-select": "~0.14.6",
40-
"peer-id": "~0.12.2",
41-
"peer-info": "~0.15.1",
35+
"debug": "^4.1.1",
36+
"err-code": "^2.0.0",
37+
"it-length-prefixed": "^2.0.0",
38+
"it-pipe": "^1.0.1",
39+
"libp2p-floodsub": "^0.19.0",
40+
"libp2p-pubsub": "~0.3.1",
41+
"p-map": "^3.0.0",
42+
"peer-id": "~0.13.3",
43+
"peer-info": "~0.17.0",
4244
"protons": "^1.0.1",
43-
"pull-length-prefixed": "^1.3.3",
44-
"pull-stream": "^3.6.13"
45+
"time-cache": "^0.3.0"
4546
},
4647
"devDependencies": {
47-
"@types/chai": "^4.1.7",
48+
"@types/chai": "^4.2.3",
4849
"@types/mocha": "^5.2.7",
49-
"aegir": "^20.0.0",
50+
"aegir": "^20.4.1",
5051
"benchmark": "^2.1.4",
5152
"chai": "^4.2.0",
5253
"chai-spies": "^1.0.0",
5354
"detect-node": "^2.0.4",
5455
"dirty-chai": "^2.0.1",
55-
"libp2p": "~0.25.5",
56-
"libp2p-secio": "~0.11.1",
57-
"libp2p-spdy": "~0.13.3",
58-
"libp2p-tcp": "~0.13.0",
59-
"libp2p-websockets": "~0.12.2",
56+
"it-pair": "^1.0.0",
6057
"lodash": "^4.17.15",
61-
"mocha": "^5.2.0",
58+
"mocha": "^6.2.1",
59+
"p-times": "^2.1.0",
6260
"promisify-es6": "^1.0.3",
63-
"sinon": "^7.3.2"
61+
"sinon": "^7.5.0"
6462
},
6563
"contributors": [
6664
"Cayman <[email protected]>",

src/heartbeat.js

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ class Heartbeat {
1212
this.gossipsub = gossipsub
1313
}
1414

15-
start (callback) {
15+
start () {
1616
if (this._heartbeatTimer) {
1717
const errMsg = 'Heartbeat timer is already running'
1818
this.gossipsub.log(errMsg)
19-
return callback(errcode(new Error(errMsg), 'ERR_HEARTBEAT_ALREADY_RUNNING'))
19+
throw errcode(new Error(errMsg), 'ERR_HEARTBEAT_ALREADY_RUNNING')
2020
}
2121

2222
const heartbeatTimer = {
@@ -25,39 +25,35 @@ class Heartbeat {
2525
runPeriodically: (fn, period) => {
2626
heartbeatTimer._timeoutId = setInterval(fn, period)
2727
},
28-
cancel: (cb) => {
28+
cancel: () => {
2929
clearTimeout(heartbeatTimer._timeoutId)
30-
cb()
3130
}
3231
}
3332

3433
const heartbeat = this._heartbeat.bind(this)
34+
3535
setTimeout(() => {
3636
heartbeat()
3737
heartbeatTimer.runPeriodically(heartbeat, constants.GossipSubHeartbeatInterval)
3838
}, constants.GossipSubHeartbeatInitialDelay)
3939

4040
this._heartbeatTimer = heartbeatTimer
41-
callback()
4241
}
4342

4443
/**
4544
* Unmounts the gossipsub protocol and shuts down every connection
46-
*
4745
* @override
48-
* @param {Function} callback
4946
* @returns {void}
5047
*/
51-
stop (callback) {
48+
stop () {
5249
if (!this._heartbeatTimer) {
5350
const errMsg = 'Heartbeat timer is not running'
5451
this.gossipsub.log(errMsg)
55-
return callback(errcode(new Error(errMsg), 'ERR_HEARTBEAT_NO_RUNNING'))
52+
throw errcode(new Error(errMsg), 'ERR_HEARTBEAT_NO_RUNNING')
5653
}
57-
this._heartbeatTimer.cancel(() => {
58-
this._heartbeatTimer = null
59-
callback()
60-
})
54+
55+
this._heartbeatTimer.cancel()
56+
this._heartbeatTimer = null
6157
}
6258

6359
/**

0 commit comments

Comments
 (0)