6
6
ArrayPrototypePush,
7
7
ArrayPrototypeSplice,
8
8
SafeFinalizationRegistry,
9
+ ObjectDefineProperty,
9
10
ObjectGetPrototypeOf,
10
11
ObjectSetPrototypeOf,
11
12
Promise,
@@ -250,35 +251,40 @@ function assertChannel(value, name) {
250
251
}
251
252
}
252
253
254
+ function tracingChannelFrom ( nameOrChannels , name ) {
255
+ if ( typeof nameOrChannels === 'string' ) {
256
+ return channel ( `tracing:${ nameOrChannels } :${ name } ` ) ;
257
+ }
258
+
259
+ if ( typeof nameOrChannels === 'object' && nameOrChannels !== null ) {
260
+ const channel = nameOrChannels [ name ] ;
261
+ assertChannel ( channel , `nameOrChannels.${ name } ` ) ;
262
+ return channel ;
263
+ }
264
+
265
+ throw new ERR_INVALID_ARG_TYPE ( 'nameOrChannels' ,
266
+ [ 'string' , 'object' , 'TracingChannel' ] ,
267
+ nameOrChannels ) ;
268
+ }
269
+
253
270
class TracingChannel {
254
271
constructor ( nameOrChannels ) {
255
- if ( typeof nameOrChannels === 'string' ) {
256
- this . start = channel ( `tracing:${ nameOrChannels } :start` ) ;
257
- this . end = channel ( `tracing:${ nameOrChannels } :end` ) ;
258
- this . asyncStart = channel ( `tracing:${ nameOrChannels } :asyncStart` ) ;
259
- this . asyncEnd = channel ( `tracing:${ nameOrChannels } :asyncEnd` ) ;
260
- this . error = channel ( `tracing:${ nameOrChannels } :error` ) ;
261
- } else if ( typeof nameOrChannels === 'object' ) {
262
- const { start, end, asyncStart, asyncEnd, error } = nameOrChannels ;
263
-
264
- assertChannel ( start , 'nameOrChannels.start' ) ;
265
- assertChannel ( end , 'nameOrChannels.end' ) ;
266
- assertChannel ( asyncStart , 'nameOrChannels.asyncStart' ) ;
267
- assertChannel ( asyncEnd , 'nameOrChannels.asyncEnd' ) ;
268
- assertChannel ( error , 'nameOrChannels.error' ) ;
269
-
270
- this . start = start ;
271
- this . end = end ;
272
- this . asyncStart = asyncStart ;
273
- this . asyncEnd = asyncEnd ;
274
- this . error = error ;
275
- } else {
276
- throw new ERR_INVALID_ARG_TYPE ( 'nameOrChannels' ,
277
- [ 'string' , 'object' , 'Channel' ] ,
278
- nameOrChannels ) ;
272
+ for ( const eventName of traceEvents ) {
273
+ ObjectDefineProperty ( this , eventName , {
274
+ __proto__ : null ,
275
+ value : tracingChannelFrom ( nameOrChannels , eventName ) ,
276
+ } ) ;
279
277
}
280
278
}
281
279
280
+ get hasSubscribers ( ) {
281
+ return this . start . hasSubscribers ||
282
+ this . end . hasSubscribers ||
283
+ this . asyncStart . hasSubscribers ||
284
+ this . asyncEnd . hasSubscribers ||
285
+ this . error . hasSubscribers ;
286
+ }
287
+
282
288
subscribe ( handlers ) {
283
289
for ( const name of traceEvents ) {
284
290
if ( ! handlers [ name ] ) continue ;
@@ -302,6 +308,10 @@ class TracingChannel {
302
308
}
303
309
304
310
traceSync ( fn , context = { } , thisArg , ...args ) {
311
+ if ( ! this . hasSubscribers ) {
312
+ return ReflectApply ( fn , thisArg , args ) ;
313
+ }
314
+
305
315
const { start, end, error } = this ;
306
316
307
317
return start . runStores ( context , ( ) => {
@@ -320,6 +330,10 @@ class TracingChannel {
320
330
}
321
331
322
332
tracePromise ( fn , context = { } , thisArg , ...args ) {
333
+ if ( ! this . hasSubscribers ) {
334
+ return ReflectApply ( fn , thisArg , args ) ;
335
+ }
336
+
323
337
const { start, end, asyncStart, asyncEnd, error } = this ;
324
338
325
339
function reject ( err ) {
@@ -358,6 +372,10 @@ class TracingChannel {
358
372
}
359
373
360
374
traceCallback ( fn , position = - 1 , context = { } , thisArg , ...args ) {
375
+ if ( ! this . hasSubscribers ) {
376
+ return ReflectApply ( fn , thisArg , args ) ;
377
+ }
378
+
361
379
const { start, end, asyncStart, asyncEnd, error } = this ;
362
380
363
381
function wrappedCallback ( err , res ) {
0 commit comments