Skip to content
This repository has been archived by the owner on Mar 10, 2020. It is now read-only.

pubsub.unsubscribe without handler reference #437

Merged
merged 7 commits into from
Feb 26, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion SPEC/PUBSUB.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ A great source of [examples][] can be found in the tests for this API.

If no `callback` is passed, a [promise][] is returned.

This works like `EventEmitter.removeListener`, as that only the `handler` passed to a `subscribe` call before is removed from listening. The underlying subscription will only be canceled once all listeners for a topic have been removed.
If the `topic` and `handler` are provided, the `handler` will no longer receive updates for the `topic`. This behaves like `EventEmitter.removeListener`. If the `handler` is not equivalent to the `handler` provided on `subscribe`, no action will be taken.
Isan-Rivkin marked this conversation as resolved.
Show resolved Hide resolved

If **only** the `topic` param is provided, unsubscribe will remove **all** handlers for the `topic`. This behaves like `EventEmitter.remoteAllListeners`. Use this if you would like to no longer receive any updates for the `topic`.
Isan-Rivkin marked this conversation as resolved.
Show resolved Hide resolved
Isan-Rivkin marked this conversation as resolved.
Show resolved Hide resolved
Isan-Rivkin marked this conversation as resolved.
Show resolved Hide resolved
Isan-Rivkin marked this conversation as resolved.
Show resolved Hide resolved

**Example:**

Expand Down Expand Up @@ -78,6 +80,25 @@ ipfs.pubsub.subscribe(topic, receiveMsg, (err) => {
})
```

Or removing all listeners:
```JavaScript
const topic = 'fruit-of-the-day'
const receiveMsg = (msg) => console.log(msg.toString())

ipfs.pubsub.subscribe(topic, receiveMsg, (err) => {
if (err) {
return console.error(`failed to subscribe to ${topic}`, err)
}

console.log(`subscribed to ${topic}`)

setTimeout(() => {
// Will unsubscribe ALL handlers for the given topic
ipfs.pubsub.unsubscribe(topic);
}, 1000)
Isan-Rivkin marked this conversation as resolved.
Show resolved Hide resolved
})
```
Isan-Rivkin marked this conversation as resolved.
Show resolved Hide resolved

A great source of [examples][] can be found in the tests for this API.

#### `pubsub.publish`
Expand Down
21 changes: 21 additions & 0 deletions src/pubsub/unsubscribe.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,26 @@ module.exports = (createCommon, options) => {
)
})
})

it('should subscribe 10 handlers and unsunscribe once with no reference to the handlers', (done) => {
const count = 10
const someTopic = getTopic()

timesSeries(count, (_, cb) => {
const handler = (msg) => {}
ipfs.pubsub.subscribe(someTopic, handler, (err) => cb(err))
}, (err) => {
expect(err).to.not.exist()
ipfs.pubsub.unsubscribe(someTopic)
setTimeout(() => {
// Assert unsubscribe worked
ipfs.pubsub.ls((err, topics) => {
expect(err).to.not.exist()
expect(topics).to.eql([])
done()
})
}, 500)
})
})
Isan-Rivkin marked this conversation as resolved.
Show resolved Hide resolved
Isan-Rivkin marked this conversation as resolved.
Show resolved Hide resolved
})
}