src: add C++ support for diagnostics channels · nodejs/node@46ee1ed
@@ -31,6 +31,9 @@ const {
31313232const { triggerUncaughtException } = internalBinding('errors');
333334+const dc_binding = internalBinding('diagnostics_channel');
35+const { subscribers: subscriberCounts } = dc_binding;
36+3437const { WeakReference } = require('internal/util');
35383639// Can't delete when weakref count reaches 0 as it could increment again.
@@ -108,6 +111,7 @@ class ActiveChannel {
108111this._subscribers = ArrayPrototypeSlice(this._subscribers);
109112ArrayPrototypePush(this._subscribers, subscription);
110113channels.incRef(this.name);
114+if (this._index !== undefined) subscriberCounts[this._index]++;
111115}
112116113117unsubscribe(subscription) {
@@ -120,14 +124,18 @@ class ActiveChannel {
120124ArrayPrototypePushApply(this._subscribers, after);
121125122126channels.decRef(this.name);
127+if (this._index !== undefined) subscriberCounts[this._index]--;
123128maybeMarkInactive(this);
124129125130return true;
126131}
127132128133bindStore(store, transform) {
129134const replacing = this._stores.has(store);
130-if (!replacing) channels.incRef(this.name);
135+if (!replacing) {
136+channels.incRef(this.name);
137+if (this._index !== undefined) subscriberCounts[this._index]++;
138+}
131139this._stores.set(store, transform);
132140}
133141@@ -139,6 +147,7 @@ class ActiveChannel {
139147this._stores.delete(store);
140148141149channels.decRef(this.name);
150+if (this._index !== undefined) subscriberCounts[this._index]--;
142151maybeMarkInactive(this);
143152144153return true;
@@ -183,6 +192,9 @@ class Channel {
183192this._subscribers = undefined;
184193this._stores = undefined;
185194this.name = name;
195+if (typeof name === 'string') {
196+this._index = dc_binding.getOrCreateChannelIndex(name);
197+}
186198187199channels.set(name, this);
188200}
@@ -434,6 +446,8 @@ function tracingChannel(nameOrChannels) {
434446return new TracingChannel(nameOrChannels);
435447}
436448449+dc_binding.linkNativeChannel((name) => channel(name));
450+437451module.exports = {
438452 channel,
439453 hasSubscribers,