channel.hasSubscribers
检查此通道是否有活跃订阅者。如果你要发送的消息的准备成本可能很高,这将很有帮助。
¥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
}