Skip to content

Commit 909f0fc

Browse files
committed
Revert "lib: initialize suppression storage eagerly"
This reverts commit ddf8179.
1 parent ddf8179 commit 909f0fc

1 file changed

Lines changed: 16 additions & 6 deletions

File tree

lib/diagnostics_channel.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,20 @@ const { subscribers: subscriberCounts } = dc_binding;
3535

3636
const { WeakReference } = require('internal/util');
3737
const { isPromise } = require('internal/util/types');
38-
const { AsyncLocalStorage } = require('async_hooks');
3938

40-
const suppressionStorage = new AsyncLocalStorage();
39+
let suppressionStorage;
4140

41+
function getSuppressionsStorage() {
42+
if (suppressionStorage === undefined) {
43+
const { AsyncLocalStorage } = require('async_hooks');
44+
suppressionStorage = new AsyncLocalStorage();
45+
}
46+
return suppressionStorage;
47+
}
48+
49+
function withSuppressionsContext(set, fn, thisArg, args) {
50+
return getSuppressionsStorage().run(set, fn.bind(thisArg), ...args);
51+
}
4252
// Can't delete when weakref count reaches 0 as it could increment again.
4353
// Only GC can be used as a valid time to clean up the channels map.
4454
class WeakRefMap extends SafeMap {
@@ -96,7 +106,7 @@ class RunStoresScope {
96106

97107
// Enter stores using withScope
98108
if (activeChannel._stores) {
99-
const activeKeys = suppressionStorage.getStore();
109+
const activeKeys = getSuppressionsStorage().getStore();
100110
for (const entry of activeChannel._stores.entries()) {
101111
const store = entry[0];
102112
const { transform, subscriberId = null } = entry[1];
@@ -214,7 +224,7 @@ class ActiveChannel {
214224

215225
publish(data) {
216226
const subscribers = this._subscribers;
217-
const activeKeys = suppressionStorage.getStore();
227+
const activeKeys = getSuppressionsStorage().getStore();
218228
for (let i = 0; i < (subscribers?.length || 0); i++) {
219229
try {
220230
const { handler, subscriberId = null } = subscribers[i];
@@ -682,10 +692,10 @@ function suppressed(key, fn, thisArg, ...args) {
682692
throw new ERR_INVALID_ARG_TYPE('key', ['object', 'symbol', 'function'], key);
683693
}
684694

685-
const currentSet = suppressionStorage.getStore();
695+
const currentSet = getSuppressionsStorage().getStore();
686696
const next = currentSet ? new SafeSet(currentSet) : new SafeSet();
687697
next.add(key);
688-
return suppressionStorage.run(next, () => ReflectApply(fn, thisArg, args));
698+
return withSuppressionsContext(next, fn, thisArg, args);
689699
}
690700

691701
module.exports = {

0 commit comments

Comments
 (0)