◐ Shell
clean mode source ↗

src: add C++ support for diagnostics channels · nodejs/node@46ee1ed

@@ -31,6 +31,9 @@ const {

31313232

const { triggerUncaughtException } = internalBinding('errors');

333334+

const dc_binding = internalBinding('diagnostics_channel');

35+

const { subscribers: subscriberCounts } = dc_binding;

36+3437

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

35383639

// Can't delete when weakref count reaches 0 as it could increment again.

@@ -108,6 +111,7 @@ class ActiveChannel {

108111

this._subscribers = ArrayPrototypeSlice(this._subscribers);

109112

ArrayPrototypePush(this._subscribers, subscription);

110113

channels.incRef(this.name);

114+

if (this._index !== undefined) subscriberCounts[this._index]++;

111115

}

112116113117

unsubscribe(subscription) {

@@ -120,14 +124,18 @@ class ActiveChannel {

120124

ArrayPrototypePushApply(this._subscribers, after);

121125122126

channels.decRef(this.name);

127+

if (this._index !== undefined) subscriberCounts[this._index]--;

123128

maybeMarkInactive(this);

124129125130

return true;

126131

}

127132128133

bindStore(store, transform) {

129134

const 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+

}

131139

this._stores.set(store, transform);

132140

}

133141

@@ -139,6 +147,7 @@ class ActiveChannel {

139147

this._stores.delete(store);

140148141149

channels.decRef(this.name);

150+

if (this._index !== undefined) subscriberCounts[this._index]--;

142151

maybeMarkInactive(this);

143152144153

return true;

@@ -183,6 +192,9 @@ class Channel {

183192

this._subscribers = undefined;

184193

this._stores = undefined;

185194

this.name = name;

195+

if (typeof name === 'string') {

196+

this._index = dc_binding.getOrCreateChannelIndex(name);

197+

}

186198187199

channels.set(name, this);

188200

}

@@ -434,6 +446,8 @@ function tracingChannel(nameOrChannels) {

434446

return new TracingChannel(nameOrChannels);

435447

}

436448449+

dc_binding.linkNativeChannel((name) => channel(name));

450+437451

module.exports = {

438452

channel,

439453

hasSubscribers,