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
}