Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc, test: tracing channel hasSubscribers getter #52908

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 37 additions & 0 deletions doc/api/diagnostics_channel.md
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,43 @@ channels.asyncStart.bindStore(myStore, (data) => {
});
```

#### `tracingChannel.hasSubscribers`

<!-- YAML
added:
- v22.0.0
- v20.13.0
-->

> Stability: 1 - Experimental

* Returns: {boolean} `true` if any of the individual channels has a subscriber,
`false` if not.

This is a helper method available on a [`TracingChannel`][] instance to check if
tlhunter marked this conversation as resolved.
Show resolved Hide resolved
any of the [TracingChannel Channels][] have subscribers. A `true` is returned if
any of them have at least one subscriber, a `false` is returned otherwise.

```mjs
import diagnostics_channel from 'node:diagnostics_channel';

const channels = diagnostics_channel.tracingChannel('my-channel');

if (channels.hasSubscribers) {
// Do something
}
```

```cjs
const diagnostics_channel = require('node:diagnostics_channel');

const channels = diagnostics_channel.tracingChannel('my-channel');

if (channels.hasSubscribers) {
// Do something
}
```

### TracingChannel Channels

A TracingChannel is a collection of several diagnostics\_channels representing
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
'use strict';

const common = require('../common');
const dc = require('diagnostics_channel');
const assert = require('assert');

const handler = common.mustNotCall();

{
const handlers = {
start: common.mustNotCall()
};

const channel = dc.tracingChannel('test');

assert.strictEqual(channel.hasSubscribers, false);

channel.subscribe(handlers);
assert.strictEqual(channel.hasSubscribers, true);

channel.unsubscribe(handlers);
assert.strictEqual(channel.hasSubscribers, false);

channel.start.subscribe(handler);
assert.strictEqual(channel.hasSubscribers, true);

channel.start.unsubscribe(handler);
assert.strictEqual(channel.hasSubscribers, false);
}

{
const handlers = {
asyncEnd: common.mustNotCall()
};

const channel = dc.tracingChannel('test');

assert.strictEqual(channel.hasSubscribers, false);

channel.subscribe(handlers);
assert.strictEqual(channel.hasSubscribers, true);

channel.unsubscribe(handlers);
assert.strictEqual(channel.hasSubscribers, false);

channel.asyncEnd.subscribe(handler);
assert.strictEqual(channel.hasSubscribers, true);

channel.asyncEnd.unsubscribe(handler);
assert.strictEqual(channel.hasSubscribers, false);
}