channel.hasSubscribers
- 返回:<boolean> 如果有活跃的订阅者
检查此通道是否有活跃的订阅者。如果你要发送的消息准备起来可能很昂贵,这会很有用。
【Check if there are active subscribers to this 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';
const channel = diagnostics_channel.channel('my-channel');
if (channel.hasSubscribers) {
// There are subscribers, prepare and publish message
}const diagnostics_channel = require('node:diagnostics_channel');
const channel = diagnostics_channel.channel('my-channel');
if (channel.hasSubscribers) {
// There are subscribers, prepare and publish message
}