diff options
author | Guy Bedford <guybedford@gmail.com> | 2021-06-15 20:09:29 +0300 |
---|---|---|
committer | Guy Bedford <guybedford@gmail.com> | 2021-06-25 21:26:49 +0300 |
commit | f4d0a6a07bf72c556c5ba8e98b3b23327c273e80 (patch) | |
tree | bbc9c36d93f4868119d9d2a9bb776e003919d752 /doc/api/diagnostics_channel.md | |
parent | 44ecd418923310aafbef8c6ccf9a9920bcf7af85 (diff) |
doc: esm examples /w imports for process, Buffer
PR-URL: https://github.com/nodejs/node/pull/39043
Reviewed-By: Bradley Farias <bradley.meck@gmail.com>
Diffstat (limited to 'doc/api/diagnostics_channel.md')
-rw-r--r-- | doc/api/diagnostics_channel.md | 98 |
1 files changed, 90 insertions, 8 deletions
diff --git a/doc/api/diagnostics_channel.md b/doc/api/diagnostics_channel.md index 7a22b2f56ee..ba02a5092c3 100644 --- a/doc/api/diagnostics_channel.md +++ b/doc/api/diagnostics_channel.md @@ -11,7 +11,11 @@ to report arbitrary message data for diagnostics purposes. It can be accessed using: -```js +```mjs +import diagnostics_channel from 'diagnostics_channel'; +``` + +```cjs const diagnostics_channel = require('diagnostics_channel'); ``` @@ -33,7 +37,27 @@ other modules. Following is a simple overview of the public API. -```js +```mjs +import diagnostics_channel from 'diagnostics_channel'; + +// Get a reusable channel object +const channel = diagnostics_channel.channel('my-channel'); + +// Subscribe to the channel +channel.subscribe((message, name) => { + // Received data +}); + +// Check if the channel has an active subscriber +if (channel.hasSubscribers) { + // Publish data to the channel + channel.publish({ + some: 'data' + }); +} +``` + +```cjs const diagnostics_channel = require('diagnostics_channel'); // Get a reusable channel object @@ -64,7 +88,15 @@ the message you want to send might be expensive to prepare. This API is optional but helpful when trying to publish messages from very performance-sensitive code. -```js +```mjs +import diagnostics_channel from 'diagnostics_channel'; + +if (diagnostics_channel.hasSubscribers('my-channel')) { + // There are subscribers, prepare and publish message +} +``` + +```cjs const diagnostics_channel = require('diagnostics_channel'); if (diagnostics_channel.hasSubscribers('my-channel')) { @@ -81,7 +113,13 @@ This is the primary entry-point for anyone wanting to interact with a named channel. It produces a channel object which is optimized to reduce overhead at publish time as much as possible. -```js +```mjs +import diagnostics_channel from 'diagnostics_channel'; + +const channel = diagnostics_channel.channel('my-channel'); +``` + +```cjs const diagnostics_channel = require('diagnostics_channel'); const channel = diagnostics_channel.channel('my-channel'); @@ -107,7 +145,17 @@ the message you want to send might be expensive to prepare. This API is optional but helpful when trying to publish messages from very performance-sensitive code. -```js +```mjs +import diagnostics_channel from 'diagnostics_channel'; + +const channel = diagnostics_channel.channel('my-channel'); + +if (channel.hasSubscribers) { + // There are subscribers, prepare and publish message +} +``` + +```cjs const diagnostics_channel = require('diagnostics_channel'); const channel = diagnostics_channel.channel('my-channel'); @@ -124,7 +172,17 @@ if (channel.hasSubscribers) { Publish a message to any subscribers to the channel. This will trigger message handlers synchronously so they will execute within the same context. -```js +```mjs +import diagnostics_channel from 'diagnostics_channel'; + +const channel = diagnostics_channel.channel('my-channel'); + +channel.publish({ + some: 'message' +}); +``` + +```cjs const diagnostics_channel = require('diagnostics_channel'); const channel = diagnostics_channel.channel('my-channel'); @@ -144,7 +202,17 @@ Register a message handler to subscribe to this channel. This message handler will be run synchronously whenever a message is published to the channel. Any errors thrown in the message handler will trigger an [`'uncaughtException'`][]. -```js +```mjs +import diagnostics_channel from 'diagnostics_channel'; + +const channel = diagnostics_channel.channel('my-channel'); + +channel.subscribe((message, name) => { + // Received data +}); +``` + +```cjs const diagnostics_channel = require('diagnostics_channel'); const channel = diagnostics_channel.channel('my-channel'); @@ -161,7 +229,21 @@ channel.subscribe((message, name) => { Remove a message handler previously registered to this channel with [`channel.subscribe(onMessage)`][]. -```js +```mjs +import diagnostics_channel from 'diagnostics_channel'; + +const channel = diagnostics_channel.channel('my-channel'); + +function onMessage(message, name) { + // Received data +} + +channel.subscribe(onMessage); + +channel.unsubscribe(onMessage); +``` + +```cjs const diagnostics_channel = require('diagnostics_channel'); const channel = diagnostics_channel.channel('my-channel'); |