Skip to content

Commit ae26a9e

Browse files
committed
diagnostics_channel: add meta built-in channels
Signed-off-by: simon-id <simon.id@protonmail.com>
1 parent 4d1557a commit ae26a9e

File tree

3 files changed

+79
-2
lines changed

3 files changed

+79
-2
lines changed

doc/api/diagnostics_channel.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,6 +1153,20 @@ passed to `console.warn()`.
11531153
Emitted when `console.error()` is called. Receives and array of the arguments
11541154
passed to `console.error()`.
11551155

1156+
#### Diagnotics Channel
1157+
1158+
> Stability: 1 - Experimental
1159+
1160+
<!-- YAML
1161+
added: REPLACEME
1162+
-->
1163+
1164+
##### Event: `'diagnostics_channel.subscribe'`
1165+
1166+
* `args` {any\[]}
1167+
1168+
Emitted when `diagnostics_channel.subcribe()` or `channel.subscribe()` is called. Receives an object with the channel that was subscribed to, and the subscription function.
1169+
11561170
#### HTTP
11571171

11581172
> Stability: 1 - Experimental

lib/diagnostics_channel.js

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ const { triggerUncaughtException } = internalBinding('errors');
3333

3434
const { WeakReference } = require('internal/util');
3535

36+
const dcSubscribeChannel = dc.channel('diagnostics_channel.subscribe');
37+
const dcUnsubscribeChannel = dc.channel('diagnostics_channel.unsubscribe');
38+
const dcPublishChannel = dc.channel('diagnostics_channel.publish');
39+
3640
// Can't delete when weakref count reaches 0 as it could increment again.
3741
// Only GC can be used as a valid time to clean up the channels map.
3842
class WeakRefMap extends SafeMap {
@@ -105,12 +109,21 @@ function wrapStoreRun(store, data, next, transform = defaultTransform) {
105109
class ActiveChannel {
106110
subscribe(subscription) {
107111
validateFunction(subscription, 'subscription');
112+
113+
if (dcSubscribeChannel.hasSubscribers) {
114+
dcSubscribeChannel.publish({ channel: this, subscription });
115+
}
116+
108117
this._subscribers = ArrayPrototypeSlice(this._subscribers);
109118
ArrayPrototypePush(this._subscribers, subscription);
110119
channels.incRef(this.name);
111120
}
112121

113122
unsubscribe(subscription) {
123+
if (dcUnsubscribeChannel.hasSubscribers) {
124+
dcUnsubscribeChannel.publish({ channel: this, subscription });
125+
}
126+
114127
const index = ArrayPrototypeIndexOf(this._subscribers, subscription);
115128
if (index === -1) return false;
116129

@@ -148,7 +161,7 @@ class ActiveChannel {
148161
return true;
149162
}
150163

151-
publish(data) {
164+
_publish(data) {
152165
const subscribers = this._subscribers;
153166
for (let i = 0; i < (subscribers?.length || 0); i++) {
154167
try {
@@ -162,6 +175,14 @@ class ActiveChannel {
162175
}
163176
}
164177

178+
publish(data) {
179+
if (dcPublishChannel.hasSubscribers) {
180+
dcPublishChannel._publish({ channel: this, data });
181+
}
182+
183+
this._publish(data);
184+
}
185+
165186
runStores(data, fn, thisArg, ...args) {
166187
let run = () => {
167188
this.publish(data);
@@ -199,6 +220,9 @@ class Channel {
199220
}
200221

201222
unsubscribe() {
223+
if (dcUnsubscribeChannel.hasSubscribers) {
224+
dcUnsubscribeChannel.publish({ channel: this, subscription });
225+
}
202226
return false;
203227
}
204228

@@ -215,7 +239,11 @@ class Channel {
215239
return false;
216240
}
217241

218-
publish() {}
242+
publish(data) {
243+
if (dcUnsubscribeChannel.hasSubscribers) {
244+
dcUnsubscribeChannel.publish({ channel: this, subscription });
245+
}
246+
}
219247

220248
runStores(data, fn, thisArg, ...args) {
221249
return ReflectApply(fn, thisArg, args);
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const dc = require('diagnostics_channel');
5+
const assert = require('assert');
6+
7+
const testedChannel = dc.channel('test');
8+
const testedSubscription = () => {};
9+
const testedData = { foo: 'bar' };
10+
11+
// should publish on meta channel for subscribe() on both inactive and active prototype
12+
dc.subscribe('diagnostics_channel.subscribe', common.mustCall(({ channel, subscription }) => {
13+
assert.strictEqual(channel, testedChannel);
14+
assert.strictEqual(subscription, testedSubscription);
15+
}, 2)); // called twice
16+
testedChannel.subscribe(testedSubscription); // inactive prototype
17+
testedChannel.subscribe(testedSubscription); // active prototype
18+
19+
// should publish on meta channel for publish()
20+
dc.subscribe('diagnostics_channel.publish', common.mustCall(({ channel, data }) => {
21+
assert.strictEqual(channel, testedChannel);
22+
assert.strictEqual(data, testedData);
23+
}));
24+
testedChannel.publish(testedData);
25+
26+
// should publish on meta channel for unsubscribe() on both inactive and active prototype
27+
dc.subscribe('diagnostics_channel.unsubscribe', common.mustCall(({ channel, subscription }) => {
28+
assert.strictEqual(channel, testedChannel);
29+
assert.strictEqual(subscription, testedSubscription);
30+
}, 2)); // called twice
31+
testedChannel.unsubscribe(testedSubscription); // active prototype
32+
testedChannel.unsubscribe(testedSubscription); // inactive prototype
33+
34+
35+
// TODO: should it publish on inactive channels ?

0 commit comments

Comments
 (0)