Skip to content

Commit 36e44f1

Browse files
authored
doc, test: tracing channel hasSubscribers getter
follow up work for #51915 PR-URL: #52908 Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 35f92d9 commit 36e44f1

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
@@ -977,6 +977,43 @@ channels.asyncStart.bindStore(myStore, (data) => {
977977
});
978978
```
979979

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

9821019
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)