channel.bindStore(store[, transform])


稳定性: 1 - 实验性

当调用 channel.runStores(context, ...) 时,给定的上下文数据将应用于绑定到该通道的任何存储。如果存储已经绑定,之前的 transform 函数将被新的函数替换。transform 函数可以省略,此时给定的上下文数据将直接作为上下文设置。

【When 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 };
});const diagnostics_channel = require('node:diagnostics_channel');
const { AsyncLocalStorage } = require('node:async_hooks');

const store = new AsyncLocalStorage();

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

channel.bindStore(store, (data) => {
  return { data };
});