diagnostics_channel.hasSubscribers(name)
检查指定通道是否有活跃订阅者。如果你要发送的消息准备起来可能很昂贵,这会很有用。
【Check if there are active subscribers to the named channel. This is helpful if the message you want to send might be expensive to prepare.】
此 API 是可选的,但在从对性能极其敏感的代码发布消息时非常有用。
【This API is optional but helpful when trying to publish messages from very performance-sensitive code.】
import diagnostics_channel from 'node:diagnostics_channel';
if (diagnostics_channel.hasSubscribers('my-channel')) {
// There are subscribers, prepare and publish message
}const diagnostics_channel = require('node:diagnostics_channel');
if (diagnostics_channel.hasSubscribers('my-channel')) {
// There are subscribers, prepare and publish message
}