GeezerVison is showing github.com — lib: add return value for DC channel.unsubscribe · nodejs/node@625be75
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: doc/api/diagnostics_channel.md +10Lines changed: 10 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -156,7 +156,17 @@ channel.subscribe((message, name) => { |
156 | 156 |
|
157 | 157 | #### `channel.unsubscribe(onMessage)` |
158 | 158 |
|
| 159 | +<!-- YAML |
| 160 | +added: |
| 161 | + - v14.17.0 |
| 162 | +changes: |
| 163 | + - version: REPLACEME |
| 164 | + pr-url: https://github.com/nodejs/node/pull/40433 |
| 165 | + description: Added return value. |
| 166 | +--> |
| 167 | + |
159 | 168 | * `onMessage` {Function} The previous subscribed handler to remove |
| 169 | +* Returns: {boolean} `true` if the handler was found, `false` otherwise. |
160 | 170 |
|
161 | 171 | Remove a message handler previously registered to this channel with |
162 | 172 | [`channel.subscribe(onMessage)`][]. |
|
Collapse file
Copy file name to clipboardExpand all lines: lib/diagnostics_channel.js +9-7Lines changed: 9 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -32,15 +32,17 @@ class ActiveChannel { |
32 | 32 |
|
33 | 33 | unsubscribe(subscription) { |
34 | 34 | const index = ArrayPrototypeIndexOf(this._subscribers, subscription); |
35 | | - if (index >= 0) { |
36 | | - ArrayPrototypeSplice(this._subscribers, index, 1); |
| 35 | + if (index === -1) return false; |
37 | 36 |
|
38 | | - // When there are no more active subscribers, restore to fast prototype. |
39 | | - if (!this._subscribers.length) { |
40 | | - // eslint-disable-next-line no-use-before-define |
41 | | - ObjectSetPrototypeOf(this, Channel.prototype); |
42 | | - } |
| 37 | + ArrayPrototypeSplice(this._subscribers, index, 1); |
| 38 | + |
| 39 | + // When there are no more active subscribers, restore to fast prototype. |
| 40 | + if (!this._subscribers.length) { |
| 41 | + // eslint-disable-next-line no-use-before-define |
| 42 | + ObjectSetPrototypeOf(this, Channel.prototype); |
43 | 43 | } |
| 44 | + |
| 45 | + return true; |
44 | 46 | } |
45 | 47 |
|
46 | 48 | get hasSubscribers() { |
|
Collapse file
Copy file name to clipboardExpand all lines: test/parallel/test-diagnostics-channel-object-channel-pub-sub.js +4-1Lines changed: 4 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -35,9 +35,12 @@ assert.ok(channel instanceof Channel); |
35 | 35 | channel.publish(input); |
36 | 36 |
|
37 | 37 | // Should not publish after subscriber is unsubscribed |
38 | | -channel.unsubscribe(subscriber); |
| 38 | +assert.ok(channel.unsubscribe(subscriber)); |
39 | 39 | assert.ok(!channel.hasSubscribers); |
40 | 40 |
|
| 41 | +// unsubscribe() should return false when subscriber is not found |
| 42 | +assert.ok(!channel.unsubscribe(subscriber)); |
| 43 | + |
41 | 44 | assert.throws(() => { |
42 | 45 | channel.subscribe(null); |
43 | 46 | }, { code: 'ERR_INVALID_ARG_TYPE' }); |