boundedChannel.withScope([context])
context<Object> 用于关联事件的共享对象- 返回:BoundedChannelScope 一次性作用域对象
使用 JavaScript 的显式资源管理(using 语法)为同步操作创建一个一次性作用域。该作用域会自动发布 start 和 end 事件,进入绑定的存储,并在释放时处理清理。
🌐 Create a disposable scope for tracing a synchronous operation using JavaScript's
explicit resource management (using syntax). The scope automatically publishes
start and end events, enters bound stores, and handles cleanup when disposed.
import { boundedChannel } from 'node:diagnostics_channel';
const wc = boundedChannel('my-operation');
const context = { operationId: '123' };
{
using scope = wc.withScope(context);
// Stores are entered, start event is published
// Perform work and set result on context
context.result = 42;
}
// End event is published, stores are restored automaticallyconst { boundedChannel } = require('node:diagnostics_channel');
const wc = boundedChannel('my-operation');
const context = { operationId: '123' };
{
using scope = wc.withScope(context);
// Stores are entered, start event is published
// Perform work and set result on context
context.result = 42;
}
// End event is published, stores are restored automatically