8000
We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 8ceefeb commit c028d21Copy full SHA for c028d21
lib/diagnostics_channel.js
@@ -4,6 +4,8 @@ const {
4
ArrayPrototypeAt,
5
ArrayPrototypeIndexOf,
6
ArrayPrototypePush,
7
+ ArrayPrototypePushApply,
8
+ ArrayPrototypeSlice,
9
ArrayPrototypeSplice,
10
ObjectDefineProperty,
11
ObjectGetPrototypeOf,
@@ -97,6 +99,7 @@ function wrapStoreRun(store, data, next, transform = defaultTransform) {
97
99
class ActiveChannel {
98
100
subscribe(subscription) {
101
validateFunction(subscription, 'subscription');
102
+ this._subscribers = ArrayPrototypeSlice(this._subscribers);
103
ArrayPrototypePush(this._subscribers, subscription);
104
channels.incRef(this.name);
105
}
@@ -105,7 +108,10 @@ class ActiveChannel {
108
const index = ArrayPrototypeIndexOf(this._subscribers, subscription);
106
109
if (index === -1) return false;
107
110
- ArrayPrototypeSplice(this._subscribers, index, 1);
111
+ const before = ArrayPrototypeSlice(this._subscribers, 0, index);
112
+ const after = ArrayPrototypeSlice(this._subscribers, index + 1);
113
+ this._subscribers = before;
114
+ ArrayPrototypePushApply(this._subscribers, after);
115
116
channels.decRef(this.name);
117
maybeMarkInactive(this);
@@ -137,9 +143,10 @@ class ActiveChannel {
137
143
138
144
139
145
publish(data) {
140
- for (let i = 0; i < (this._subscribers?.length || 0); i++) {
146
+ const subscribers = this._subscribers;
147
+ for (let i = 0; i < (subscribers?.length || 0); i++) {
141
148
try {
142
- const onMessage = this._subscribers[i];
149
+ const onMessage = subscribers[i];
150
onMessage(data, this.name);
151
} catch (err) {
152
process.nextTick(() => {
test/parallel/test-diagnostics-channel-sync-unsubscribe.js
@@ -9,6 +9,7 @@ const published_data = 'some message';
const onMessageHandler = common.mustCall(() => dc.unsubscribe(channel_name, onMessageHandler));
dc.subscribe(channel_name, onMessageHandler);
12
+dc.subscribe(channel_name, common.mustCall());
13
14
// This must not throw.
15
dc.channel(channel_name).publish(published_data);