6
6
ArrayPrototypePush,
7
7
ArrayPrototypeSplice,
8
8
SafeFinalizationRegistry,
9
+ ObjectDefineProperty,
9
10
ObjectGetPrototypeOf,
10
11
ObjectSetPrototypeOf,
11
12
Promise,
@@ -250,35 +251,41 @@ function assertChannel(value, name) {
250
251
}
251
252
}
252
253
254
+ function channelOrName ( nameOrChannels , name ) {
255
+ if ( typeof nameOrChannels === 'string' ) {
256
+ return channel ( `tracing:${ nameOrChannels } :${ name } ` )
257
+ }
258
+
259
+ if ( typeof nameOrChannels === 'object' ) {
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
+ value : channelOrName ( nameOrChannels , eventName )
275
+ } ) ;
279
276
}
280
277
}
281
278
279
+ get hasSubscribers ( ) {
280
+ for ( const name of traceEvents ) {
281
+ if ( this [ name ] . hasSubscribers ) {
282
+ return true
283
+ }
284
+ }
285
+
286
+ return false
287
+ }
288
+
282
289
subscribe ( handlers ) {
283
290
for ( const name of traceEvents ) {
284
291
if ( ! handlers [ name ] ) continue ;
@@ -302,6 +309,10 @@ class TracingChannel {
302
309
}
303
310
304
311
traceSync ( fn , context = { } , thisArg , ...args ) {
312
+ if ( ! this . hasSubscribers ) {
313
+ return ReflectApply ( fn , thisArg , args ) ;
314
+ }
315
+
305
316
const { start, end, error } = this ;
306
317
307
318
return start . runStores ( context , ( ) => {
@@ -320,6 +331,10 @@ class TracingChannel {
320
331
}
321
332
322
333
tracePromise ( fn , context = { } , thisArg , ...args ) {
334
+ if ( ! this . hasSubscribers ) {
335
+ return ReflectApply ( fn , thisArg , args ) ;
336
+ }
337
+
323
338
const { start, end, asyncStart, asyncEnd, error } = this ;
324
339
325
340
function reject ( err ) {
@@ -358,6 +373,10 @@ class TracingChannel {
358
373
}
359
374
360
375
traceCallback ( fn , position = - 1 , context = { } , thisArg , ...args ) {
376
+ if ( ! this . hasSubscribers ) {
377
+ return ReflectApply ( fn , thisArg , args ) ;
378
+ }
379
+
361
380
const { start, end, asyncStart, asyncEnd, error } = this ;
362
381
363
382
function wrappedCallback ( err , res ) {
0 commit comments