Channel
[<Interface>]
type Channel<'StoreType, 'ContextType> =
abstract member hasSubscribers: bool
abstract member name: U2<string,Symbol>
static member Create<'Args, 'Result, 'ThisArg> (name: U2<string,Symbol>, hasSubscribers: bool, publish: obj -> unit, subscribe: ChannelListener -> unit, unsubscribe: ChannelListener -> unit, bindStore: Action<AsyncLocalStorage<'StoreType>,('ContextType -> 'StoreType) option>, unbindStore: AsyncLocalStorage<'StoreType> -> bool, runStores: Func<'ContextType,('Args -> 'Result),'ThisArg option,'Args,'Result>) : Channel<'StoreType,'ContextType>
abstract member bindStore (store: AsyncLocalStorage<'StoreType>, ?transform: ('ContextType -> 'StoreType)) : unit
abstract member publish (message: obj) : unit
abstract member runStores<'ThisArg, 'Args, 'Result> (context: 'ContextType, fn: 'Args -> 'Result, thisArg: 'ThisArg option, args: 'Args) : 'Result
abstract member subscribe (onMessage: ChannelListener) : unit
abstract member unbindStore (store: AsyncLocalStorage<'StoreType>) : bool
abstract member unsubscribe (onMessage: ChannelListener) : unit| Assembly | Xantham.Fable.Node |
| Attributes | [<Interface>] |
The class Channel represents an individual named channel within the data pipeline. It is used to track subscribers and to publish messages when there are subscribers present. It exists as a separate object to avoid channel lookups at publish time, enabling very fast publish speeds and allowing for heavy use while incurring very minimal cost. Channels are created with channel, constructing a channel directly with new Channel(name) is not supported.
@since v15.1.0, v14.17.0
Properties
abstract member hasSubscribers: bool
abstract member hasSubscribers: boolCheck if there are active subscribers to this channel. This is helpful if 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.
import diagnostics_channel from 'node:diagnostics_channel';
const channel = diagnostics_channel.channel('my-channel');
if (channel.hasSubscribers) {
// There are subscribers, prepare and publish message
}
@since v15.1.0, v14.17.0
Methods
static member Create<'Args, 'Result, 'ThisArg> (name: U2<string,Symbol>, hasSubscribers: bool, publish: obj -> unit, subscribe: ChannelListener -> unit, unsubscribe: ChannelListener -> unit, bindStore: Action<AsyncLocalStorage<'StoreType>,('ContextType -> 'StoreType) option>, unbindStore: AsyncLocalStorage<'StoreType> -> bool, runStores: Func<'ContextType,('Args -> 'Result),'ThisArg option,'Args,'Result>) : Channel<'StoreType,'ContextType>abstract member bindStore (store: AsyncLocalStorage<'StoreType>, ?transform: ('ContextType -> 'StoreType)) : unit
abstract member bindStore (store: AsyncLocalStorage<'StoreType>, ?transform: ('ContextType -> 'StoreType)) : unitWhen channel.runStores(context, ...) is called, the given context data will be applied to any store bound to the channel. If the store has already been bound the previous transform function will be replaced with the new one. The transform function may be omitted to set the given context data as the context directly.
import diagnostics_channel from 'node:diagnostics_channel';
import { AsyncLocalStorage } from 'node:async_hooks';
const store = new AsyncLocalStorage();
const channel = diagnostics_channel.channel('my-channel');
channel.bindStore(store, (data) => {
return { data };
});
@since v19.9.0
| Parameter | |
|---|---|
store | The store to which to bind the context data |
transform | Transform context data before setting the store context |
abstract member publish (message: obj) : unit
abstract member publish (message: obj) : unitPublish a message to any subscribers to the channel. This will trigger message handlers synchronously so they will execute within the same context.
import diagnostics_channel from 'node:diagnostics_channel';
const channel = diagnostics_channel.channel('my-channel');
channel.publish({
some: 'message',
});
@since v15.1.0, v14.17.0
| Parameter | |
|---|---|
message | The message to send to the channel subscribers |
abstract member runStores<'ThisArg, 'Args, 'Result> (context: 'ContextType, fn: 'Args -> 'Result, thisArg: 'ThisArg option, args: 'Args) : 'Result
abstract member runStores<'ThisArg, 'Args, 'Result> (context: 'ContextType, fn: 'Args -> 'Result, thisArg: 'ThisArg option, args: 'Args) : 'ResultApplies the given data to any AsyncLocalStorage instances bound to the channel for the duration of the given function, then publishes to the channel within the scope of that data is applied to the stores.
If a transform function was given to channel.bindStore(store) it will be applied to transform the message data before it becomes the context value for the store. The prior storage context is accessible from within the transform function in cases where context linking is required.
The context applied to the store should be accessible in any async code which continues from execution which began during the given function, however there are some situations in which context loss may occur.
import diagnostics_channel from 'node:diagnostics_channel';
import { AsyncLocalStorage } from 'node:async_hooks';
const store = new AsyncLocalStorage();
const channel = diagnostics_channel.channel('my-channel');
channel.bindStore(store, (message) => {
const parent = store.getStore();
return new Span(message, parent);
});
channel.runStores({ some: 'message' }, () => {
store.getStore(); // Span({ some: 'message' })
});
@since v19.9.0
| Parameter | |
|---|---|
context | Message to send to subscribers and bind to stores |
fn | Handler to run within the entered storage context |
thisArg | The receiver to be used for the function call. |
args | Optional arguments to pass to the function. |
abstract member subscribe (onMessage: ChannelListener) : unit
abstract member subscribe (onMessage: ChannelListener) : unitRegister 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'.
import diagnostics_channel from 'node:diagnostics_channel';
const channel = diagnostics_channel.channel('my-channel');
channel.subscribe((message, name) => {
// Received data
});
@since v15.1.0, v14.17.0
| Parameter | |
|---|---|
onMessage | The handler to receive channel messages |
abstract member unbindStore (store: AsyncLocalStorage<'StoreType>) : bool
abstract member unbindStore (store: AsyncLocalStorage<'StoreType>) : boolRemove a message handler previously registered to this channel with channel.bindStore(store).
import diagnostics_channel from 'node:diagnostics_channel';
import { AsyncLocalStorage } from 'node:async_hooks';
const store = new AsyncLocalStorage();
const channel = diagnostics_channel.channel('my-channel');
channel.bindStore(store);
channel.unbindStore(store);
@since v19.9.0
| Parameter | |
|---|---|
store | The store to unbind from the channel. |
Returns. true if the store was found, false otherwise.
abstract member unsubscribe (onMessage: ChannelListener) : unit
abstract member unsubscribe (onMessage: ChannelListener) : unitRemove a message handler previously registered to this channel with channel.subscribe(onMessage).
import diagnostics_channel from 'node:diagnostics_channel';
const channel = diagnostics_channel.channel('my-channel');
function onMessage(message, name) {
// Received data
}
channel.subscribe(onMessage);
channel.unsubscribe(onMessage);
@since v15.1.0, v14.17.0
| Parameter | |
|---|---|
onMessage | The previous subscribed handler to remove |
Returns. true if the handler was found, false otherwise.