Skip to content

Commit ce98f31

Browse files
committed
supports async pubsub.unsubscribe() and ipfsv0.29. Fixes #43
1 parent c13b461 commit ce98f31

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
## js-ipfs
1313

14-
This package has been tested with js-ipfs version __0.28.0__.
14+
This package has been tested with js-ipfs version __0.29.1__.
1515

1616
## Install
1717

@@ -77,7 +77,7 @@ Broacasts message (string or buffer).
7777

7878
Sends message (string or buffer) to peer.
7979

80-
## room.leave()
80+
## async room.leave()
8181

8282
Leaves room, stopping everything.
8383

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"chai": "^4.1.1",
3939
"dirty-chai": "^2.0.1",
4040
"aegir": "^12.1.3",
41-
"ipfs": "~0.28.0"
41+
"ipfs": "~0.29.1"
4242
},
4343
"browser": {
4444
"./test/utils/create-repo-node.js": "./test/utils/create-repo-browser.js"

src/index.js

+16-7
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,15 @@ class PubSubRoom extends EventEmitter {
5252
}
5353

5454
leave () {
55-
timers.clearInterval(this._interval)
56-
Object.keys(this._connections).forEach((peer) => {
57-
this._connections[peer].stop()
55+
return new Promise((resolve, reject) => {
56+
timers.clearInterval(this._interval)
57+
Object.keys(this._connections).forEach((peer) => {
58+
this._connections[peer].stop()
59+
})
60+
directConnection.emitter.removeListener(this._topic, this._handleDirectMessage)
61+
this.once('stopped', () => resolve())
62+
this.emit('stopping')
5863
})
59-
directConnection.emitter.removeListener(this._topic, this._handleDirectMessage)
60-
this.emit('stop')
6164
}
6265

6366
broadcast (_message) {
@@ -117,8 +120,14 @@ class PubSubRoom extends EventEmitter {
117120
}
118121
})
119122

120-
this.once('stop', () => {
121-
this._ipfs.pubsub.unsubscribe(this._topic, listener)
123+
this.once('stopping', () => {
124+
this._ipfs.pubsub.unsubscribe(this._topic, listener, (err) => {
125+
if (err) {
126+
this.emit('error', err)
127+
} else {
128+
this.emit('stopped')
129+
}
130+
})
122131
})
123132

124133
this._ipfs._libp2pNode.handle(PROTOCOL, directConnection.handler)

0 commit comments

Comments
 (0)