Skip to content

Commit ddf8179

Browse files
committed
lib: initialize suppression storage eagerly
1 parent f9f428a commit ddf8179

1 file changed

Lines changed: 6 additions & 16 deletions

File tree

lib/diagnostics_channel.js

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,10 @@ 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');
3839

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

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-
}
5242
// Can't delete when weakref count reaches 0 as it could increment again.
5343
// Only GC can be used as a valid time to clean up the channels map.
5444
class WeakRefMap extends SafeMap {
@@ -106,7 +96,7 @@ class RunStoresScope {
10696

10797
// Enter stores using withScope
10898
if (activeChannel._stores) {
109-
const activeKeys = getSuppressionsStorage().getStore();
99+
const activeKeys = suppressionStorage.getStore();
110100
for (const entry of activeChannel._stores.entries()) {
111101
const store = entry[0];
112102
const { transform, subscriberId = null } = entry[1];
@@ -224,7 +214,7 @@ class ActiveChannel {
224214

225215
publish(data) {
226216
const subscribers = this._subscribers;
227-
const activeKeys = getSuppressionsStorage().getStore();
217+
const activeKeys = suppressionStorage.getStore();
228218
for (let i = 0; i < (subscribers?.length || 0); i++) {
229219
try {
230220
const { handler, subscriberId = null } = subscribers[i];
@@ -692,10 +682,10 @@ function suppressed(key, fn, thisArg, ...args) {
692682
throw new ERR_INVALID_ARG_TYPE('key', ['object', 'symbol', 'function'], key);
693683
}
694684

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

701691
module.exports = {

0 commit comments

Comments
 (0)