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

Commit cd51120

Browse files
Isan-RivkinAlan Shaw
authored and
Alan Shaw
committed
test: pubsub.unsubscribe without handler reference (#437)
* docs: unsubscribe with no parameters * test: add test with many handlers for unsubscribe * docs: edit unsubscribe explanation * test: remove setTimeout * docs: use async/await in unsubscribe * docs: added extra note on promise * test: convert unsubscribe use to async api
1 parent 2e3fbb2 commit cd51120

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

SPEC/PUBSUB.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,15 @@ A great source of [examples][] can be found in the tests for this API.
5050

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

53-
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.
53+
If the `topic` and `handler` are provided, the `handler` will no longer receive updates for the `topic`. This behaves like [EventEmitter.removeListener](https://nodejs.org/dist/latest/docs/api/events.html#events_emitter_removelistener_eventname_listener). If the `handler` is not equivalent to the `handler` provided on `subscribe`, no action will be taken.
54+
55+
If **only** the `topic` param is provided, unsubscribe will remove **all** handlers for the `topic`. This behaves like [EventEmitter.removeAllListeners](https://nodejs.org/dist/latest/docs/api/events.html#events_emitter_removealllisteners_eventname). Use this if you would like to no longer receive any updates for the `topic`.
56+
57+
**WARNING:** Unsubscribe is an async operation, but removing **all** handlers for a topic can only be done using the Promises API (due to the difficulty in distinguishing between a "handler" and a "callback" - they are both functions). If you _need_ to know when unsubscribe has completed you must use `await` or `.then` on the return value from
58+
59+
```JavaScript
60+
ipfs.pubsub.unsubscribe('topic')
61+
```
5462

5563
**Example:**
5664

@@ -78,6 +86,17 @@ ipfs.pubsub.subscribe(topic, receiveMsg, (err) => {
7886
})
7987
```
8088

89+
Or removing all listeners:
90+
```JavaScript
91+
const topic = 'fruit-of-the-day'
92+
const receiveMsg = (msg) => console.log(msg.toString())
93+
94+
await ipfs.pubsub.subscribe(topic, receiveMsg);
95+
96+
// Will unsubscribe ALL handlers for the given topic
97+
await ipfs.pubsub.unsubscribe(topic);
98+
```
99+
81100
A great source of [examples][] can be found in the tests for this API.
82101

83102
#### `pubsub.publish`

src/pubsub/unsubscribe.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,16 @@ module.exports = (createCommon, options) => {
5757
)
5858
})
5959
})
60+
61+
it('should subscribe 10 handlers and unsunscribe once with no reference to the handlers', async () => {
62+
const count = 10
63+
const someTopic = getTopic()
64+
for (let i = 0; i < count; i++) {
65+
await ipfs.pubsub.subscribe(someTopic, (msg) => {})
66+
}
67+
await ipfs.pubsub.unsubscribe(someTopic)
68+
const topics = await ipfs.pubsub.ls()
69+
expect(topics).to.eql([])
70+
})
6071
})
6172
}

0 commit comments

Comments
 (0)