Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit faa6e33

Browse files
committed
Fix canceling subscription. Move subscription state to be local to the function.
1 parent 3b365f7 commit faa6e33

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

src/core/components/pubsub.js

+21-16
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,26 @@ const _values = require('lodash.values')
66

77
const OFFLINE_ERROR = require('../utils').OFFLINE_ERROR
88

9-
let subscriptions = {}
10-
11-
const addSubscription = (topic, request, stream) => {
12-
subscriptions[topic] = { request: request, stream: stream }
13-
}
9+
module.exports = function pubsub (self) {
10+
let subscriptions = {}
1411

15-
const removeSubscription = promisify((topic, callback) => {
16-
if (!subscriptions[topic]) {
17-
return callback(new Error(`Not subscribed to ${topic}`))
12+
const addSubscription = (topic, request, stream) => {
13+
subscriptions[topic] = { request: request, stream: stream }
1814
}
1915

20-
subscriptions[topic].stream.emit('end')
21-
delete subscriptions[topic]
16+
const removeSubscription = promisify((topic, callback) => {
17+
if (!subscriptions[topic]) {
18+
return callback(new Error(`Not subscribed to ${topic}`))
19+
}
2220

23-
if (callback) {
24-
callback(null)
25-
}
26-
})
21+
subscriptions[topic].stream.emit('end')
22+
delete subscriptions[topic]
23+
24+
if (callback) {
25+
callback(null)
26+
}
27+
})
2728

28-
module.exports = function pubsub (self) {
2929
return {
3030
subscribe: promisify((topic, options, callback) => {
3131
if (!self.isOnline()) {
@@ -47,7 +47,11 @@ module.exports = function pubsub (self) {
4747

4848
// There is no explicit unsubscribe; subscriptions have a cancel event
4949
stream.cancel = promisify((cb) => {
50+
// Remove the event listener
51+
self._pubsub.removeAllListeners(topic)
52+
// Make sure floodsub knows we've unsubscribed
5053
self._pubsub.unsubscribe(topic)
54+
// Remove the subscription from pubsub's internal state
5155
removeSubscription(topic, cb)
5256
})
5357

@@ -113,8 +117,9 @@ module.exports = function pubsub (self) {
113117
let peers = []
114118

115119
try {
120+
// This part should be moved down to floodsub
121+
// Just return the list of peers
116122
const peerSet = self._pubsub.getPeerSet()
117-
console.log(peerSet)
118123
_values(peerSet).forEach((peer) => {
119124
const idB58Str = peer.peerInfo.id.toB58String()
120125
const index = peer.topics.indexOf(topic)

0 commit comments

Comments
 (0)