@@ -36,31 +36,18 @@ const { subscribers: subscriberCounts } = dc_binding;
3636const { WeakReference } = require ( 'internal/util' ) ;
3737const { isPromise } = require ( 'internal/util/types' ) ;
3838
39- // Internal only: tracks a Set of active suppression keys for the current async
40- // context. Uses a simple stack-based approach to avoid bootstrap issues with
41- // async_hooks. This is a simplified implementation that works for typical usage.
42- let suppressionStorage = null ;
39+ let suppressionStorage ;
4340
4441function getSuppressionsStorage ( ) {
45- if ( suppressionStorage === null ) {
46- try {
47- const { AsyncLocalStorage } = require ( 'async_hooks' ) ;
48- suppressionStorage = new AsyncLocalStorage ( ) ;
49- } catch {
50- // If AsyncLocalStorage fails to initialize (rare), use a fallback
51- // that won't provide async context isolation but at least works
52- suppressionStorage = false ; // Marker for "tried and failed"
53- }
42+ if ( suppressionStorage === undefined ) {
43+ const { AsyncLocalStorage } = require ( 'async_hooks' ) ;
44+ suppressionStorage = new AsyncLocalStorage ( ) ;
5445 }
55- return suppressionStorage || undefined ;
46+ return suppressionStorage ;
5647}
5748
5849function withSuppressionsContext ( set , fn , thisArg , args ) {
59- const storage = getSuppressionsStorage ( ) ;
60- if ( storage ) {
61- return storage . run ( set , ( ) => ReflectApply ( fn , thisArg , args ) ) ;
62- }
63- return ReflectApply ( fn , thisArg , args ) ;
50+ return getSuppressionsStorage ( ) . run ( set , ( ) => ReflectApply ( fn , thisArg , args ) ) ;
6451}
6552// Can't delete when weakref count reaches 0 as it could increment again.
6653// Only GC can be used as a valid time to clean up the channels map.
@@ -119,15 +106,14 @@ class RunStoresScope {
119106
120107 // Enter stores using withScope
121108 if ( activeChannel . _stores ) {
122- const storage = getSuppressionsStorage ( ) ;
123- const activeKeys = storage ? storage . getStore ( ) : undefined ;
109+ const activeKeys = getSuppressionsStorage ( ) . getStore ( ) ;
124110 for ( const entry of activeChannel . _stores . entries ( ) ) {
125111 const store = entry [ 0 ] ;
126- const { transform, suppressedBy = null } = entry [ 1 ] ;
112+ const { transform, subscriberId = null } = entry [ 1 ] ;
127113
128114 // Skip this bound store if it opted into suppression and its key
129115 // is active in the current async context.
130- if ( suppressedBy !== null && activeKeys ?. has ( suppressedBy ) ) {
116+ if ( subscriberId !== null && activeKeys ?. has ( subscriberId ) ) {
131117 continue ;
132118 }
133119
@@ -163,17 +149,17 @@ class RunStoresScope {
163149class ActiveChannel {
164150 subscribe ( subscription , options = { } ) {
165151 validateFunction ( subscription , 'subscription' ) ;
166- const suppressedBy = options && options . suppressedBy !== undefined ? options . suppressedBy : null ;
167- if ( suppressedBy !== null ) {
168- const t = typeof suppressedBy ;
152+ const subscriberId = options && options . subscriberId !== undefined ? options . subscriberId : null ;
153+ if ( subscriberId !== null ) {
154+ const t = typeof subscriberId ;
169155 if ( t === 'string' || t === 'number' || t === 'bigint' || t === 'boolean' ) {
170- throw new ERR_INVALID_ARG_TYPE ( 'suppressedBy ' , [ 'object' , 'symbol' , 'function' ] , suppressedBy ) ;
156+ throw new ERR_INVALID_ARG_TYPE ( 'subscriberId ' , [ 'object' , 'symbol' , 'function' ] , subscriberId ) ;
171157 }
172158 }
173159
174160 const handler = subscription ;
175161 this . _subscribers = ArrayPrototypeSlice ( this . _subscribers ) ;
176- ArrayPrototypePush ( this . _subscribers , { handler, suppressedBy } ) ;
162+ ArrayPrototypePush ( this . _subscribers , { handler, subscriberId } ) ;
177163 channels . incRef ( this . name ) ;
178164 if ( this . _index !== undefined ) subscriberCounts [ this . _index ] ++ ;
179165 }
@@ -202,11 +188,11 @@ class ActiveChannel {
202188 }
203189
204190 bindStore ( store , transform , options = { } ) {
205- const suppressedBy = options && options . suppressedBy !== undefined ? options . suppressedBy : null ;
206- if ( suppressedBy !== null ) {
207- const t = typeof suppressedBy ;
191+ const subscriberId = options && options . subscriberId !== undefined ? options . subscriberId : null ;
192+ if ( subscriberId !== null ) {
193+ const t = typeof subscriberId ;
208194 if ( t === 'string' || t === 'number' || t === 'bigint' || t === 'boolean' ) {
209- throw new ERR_INVALID_ARG_TYPE ( 'suppressedBy ' , [ 'object' , 'symbol' , 'function' ] , suppressedBy ) ;
195+ throw new ERR_INVALID_ARG_TYPE ( 'subscriberId ' , [ 'object' , 'symbol' , 'function' ] , subscriberId ) ;
210196 }
211197 }
212198
@@ -215,7 +201,7 @@ class ActiveChannel {
215201 channels . incRef ( this . name ) ;
216202 if ( this . _index !== undefined ) subscriberCounts [ this . _index ] ++ ;
217203 }
218- this . _stores . set ( store , { transform, suppressedBy } ) ;
204+ this . _stores . set ( store , { transform, subscriberId } ) ;
219205 }
220206
221207 unbindStore ( store ) {
@@ -238,12 +224,11 @@ class ActiveChannel {
238224
239225 publish ( data ) {
240226 const subscribers = this . _subscribers ;
241- const storage = getSuppressionsStorage ( ) ;
242- const activeKeys = storage ? storage . getStore ( ) : undefined ;
227+ const activeKeys = getSuppressionsStorage ( ) . getStore ( ) ;
243228 for ( let i = 0 ; i < ( subscribers ?. length || 0 ) ; i ++ ) {
244229 try {
245- const { handler, suppressedBy = null } = subscribers [ i ] ;
246- if ( suppressedBy !== null && activeKeys ?. has ( suppressedBy ) ) {
230+ const { handler, subscriberId = null } = subscribers [ i ] ;
231+ if ( subscriberId !== null && activeKeys ?. has ( subscriberId ) ) {
247232 continue ;
248233 }
249234 handler ( data , this . name ) ;
@@ -707,8 +692,7 @@ function suppressed(key, fn, thisArg, ...args) {
707692 throw new ERR_INVALID_ARG_TYPE ( 'key' , [ 'object' , 'symbol' , 'function' ] , key ) ;
708693 }
709694
710- const storage = getSuppressionsStorage ( ) ;
711- const currentSet = storage ? storage . getStore ( ) : undefined ;
695+ const currentSet = getSuppressionsStorage ( ) . getStore ( ) ;
712696 const next = currentSet ? new SafeSet ( currentSet ) : new SafeSet ( ) ;
713697 next . add ( key ) ;
714698 return withSuppressionsContext ( next , fn , thisArg , args ) ;
0 commit comments