Skip to content

Commit f336e61

Browse files
tlhuntermarco-ippolito
authored andcommitted
doc, test: tracing channel hasSubscribers getter
follow up work for #51915 PR-URL: #52908 Reviewed-By: Stephen Belanger <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 5eea419 commit f336e61

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

doc/api/diagnostics_channel.md

+37
Original file line numberDiff line numberDiff line change
@@ -967,6 +967,43 @@ channels.asyncStart.bindStore(myStore, (data) => {
967967
});
968968
```
969969

970+
#### `tracingChannel.hasSubscribers`
971+
972+
<!-- YAML
973+
added:
974+
- v22.0.0
975+
- v20.13.0
976+
-->
977+
978+
> Stability: 1 - Experimental
979+
980+
* Returns: {boolean} `true` if any of the individual channels has a subscriber,
981+
`false` if not.
982+
983+
This is a helper method available on a [`TracingChannel`][] instance to check if
984+
any of the [TracingChannel Channels][] have subscribers. A `true` is returned if
985+
any of them have at least one subscriber, a `false` is returned otherwise.
986+
987+
```mjs
988+
import diagnostics_channel from 'node:diagnostics_channel';
989+
990+
const channels = diagnostics_channel.tracingChannel('my-channel');
991+
992+
if (channels.hasSubscribers) {
993+
// Do something
994+
}
995+
```
996+
997+
```cjs
998+
const diagnostics_channel = require('node:diagnostics_channel');
999+
1000+
const channels = diagnostics_channel.tracingChannel('my-channel');
1001+
1002+
if (channels.hasSubscribers) {
1003+
// Do something
1004+
}
1005+
```
1006+
9701007
### TracingChannel Channels
9711008

9721009
A TracingChannel is a collection of several diagnostics\_channels representing
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const dc = require('diagnostics_channel');
5+
const assert = require('assert');
6+
7+
const handler = common.mustNotCall();
8+
9+
{
10+
const handlers = {
11+
start: common.mustNotCall()
12+
};
13+
14+
const channel = dc.tracingChannel('test');
15+
16+
assert.strictEqual(channel.hasSubscribers, false);
17+
18+
channel.subscribe(handlers);
19+
assert.strictEqual(channel.hasSubscribers, true);
20+
21+
channel.unsubscribe(handlers);
22+
assert.strictEqual(channel.hasSubscribers, false);
23+
24+
channel.start.subscribe(handler);
25+
assert.strictEqual(channel.hasSubscribers, true);
26+
27+
channel.start.unsubscribe(handler);
28+
assert.strictEqual(channel.hasSubscribers, false);
29+
}
30+
31+
{
32+
const handlers = {
33+
asyncEnd: common.mustNotCall()
34+
};
35+
36+
const channel = dc.tracingChannel('test');
37+
38+
assert.strictEqual(channel.hasSubscribers, false);
39+
40+
channel.subscribe(handlers);
41+
assert.strictEqual(channel.hasSubscribers, true);
42+
43+
channel.unsubscribe(handlers);
44+
assert.strictEqual(channel.hasSubscribers, false);
45+
46+
channel.asyncEnd.subscribe(handler);
47+
assert.strictEqual(channel.hasSubscribers, true);
48+
49+
channel.asyncEnd.unsubscribe(handler);
50+
assert.strictEqual(channel.hasSubscribers, false);
51+
}

0 commit comments

Comments
 (0)