@@ -52,9 +52,13 @@ function isHistogram(object) {
52
52
return object ?. [ kHandle ] !== undefined ;
53
53
}
54
54
55
+ const kSkipThrow = Symbol ( 'kSkipThrow' ) ;
56
+
55
57
class Histogram {
56
- constructor ( ) {
57
- throw new ERR_ILLEGAL_CONSTRUCTOR ( ) ;
58
+ constructor ( skipThrowSymbol = undefined ) {
59
+ if ( skipThrowSymbol !== kSkipThrow ) {
60
+ throw new ERR_ILLEGAL_CONSTRUCTOR ( ) ;
61
+ }
58
62
}
59
63
60
64
[ kInspect ] ( depth , options ) {
@@ -242,7 +246,7 @@ class Histogram {
242
246
const handle = this [ kHandle ] ;
243
247
return {
244
248
data : { handle } ,
245
- deserializeInfo : 'internal/histogram:internalHistogram ' ,
249
+ deserializeInfo : 'internal/histogram:ClonedHistogram ' ,
246
250
} ;
247
251
}
248
252
@@ -264,8 +268,12 @@ class Histogram {
264
268
}
265
269
266
270
class RecordableHistogram extends Histogram {
267
- constructor ( ) {
268
- throw new ERR_ILLEGAL_CONSTRUCTOR ( ) ;
271
+ constructor ( skipThrowSymbol = undefined ) {
272
+ if ( skipThrowSymbol !== kSkipThrow ) {
273
+ throw new ERR_ILLEGAL_CONSTRUCTOR ( ) ;
274
+ }
275
+
276
+ super ( skipThrowSymbol ) ;
269
277
}
270
278
271
279
/**
@@ -309,7 +317,7 @@ class RecordableHistogram extends Histogram {
309
317
const handle = this [ kHandle ] ;
310
318
return {
311
319
data : { handle } ,
312
- deserializeInfo : 'internal/histogram:internalRecordableHistogram ' ,
320
+ deserializeInfo : 'internal/histogram:ClonedRecordableHistogram ' ,
313
321
} ;
314
322
}
315
323
@@ -318,26 +326,34 @@ class RecordableHistogram extends Histogram {
318
326
}
319
327
}
320
328
321
- function internalHistogram ( handle ) {
329
+ function ClonedHistogram ( handle ) {
322
330
return ReflectConstruct (
323
331
function ( ) {
324
332
markTransferMode ( this , true , false ) ;
325
333
this [ kHandle ] = handle ;
326
334
this [ kMap ] = new SafeMap ( ) ;
327
335
} , [ ] , Histogram ) ;
328
336
}
329
- internalHistogram . prototype [ kDeserialize ] = ( ) => { } ;
330
337
331
- function internalRecordableHistogram ( handle ) {
332
- return ReflectConstruct (
333
- function ( ) {
334
- markTransferMode ( this , true , false ) ;
335
- this [ kHandle ] = handle ;
336
- this [ kMap ] = new SafeMap ( ) ;
337
- this [ kRecordable ] = true ;
338
- } , [ ] , RecordableHistogram ) ;
338
+ ClonedHistogram . prototype [ kDeserialize ] = ( ) => { } ;
339
+
340
+ function ClonedRecordableHistogram ( handle ) {
341
+ const histogram = new RecordableHistogram ( kSkipThrow ) ;
342
+
343
+ markTransferMode ( histogram , true , false ) ;
344
+ histogram [ kRecordable ] = true ;
345
+ histogram [ kMap ] = new SafeMap ( ) ;
346
+ histogram [ kHandle ] = handle ;
347
+ histogram . constructor = RecordableHistogram ;
348
+
349
+ return histogram ;
350
+ }
351
+
352
+ ClonedRecordableHistogram . prototype [ kDeserialize ] = ( ) => { } ;
353
+
354
+ function createRecordableHistogram ( handle ) {
355
+ return new ClonedRecordableHistogram ( handle ) ;
339
356
}
340
- internalRecordableHistogram . prototype [ kDeserialize ] = ( ) => { } ;
341
357
342
358
/**
343
359
* @param {{
@@ -363,14 +379,14 @@ function createHistogram(options = kEmptyObject) {
363
379
throw new ERR_INVALID_ARG_VALUE . RangeError ( 'options.highest' , highest ) ;
364
380
}
365
381
validateInteger ( figures , 'options.figures' , 1 , 5 ) ;
366
- return internalRecordableHistogram ( new _Histogram ( lowest , highest , figures ) ) ;
382
+ return createRecordableHistogram ( new _Histogram ( lowest , highest , figures ) ) ;
367
383
}
368
384
369
385
module . exports = {
370
386
Histogram,
371
387
RecordableHistogram,
372
- internalHistogram ,
373
- internalRecordableHistogram ,
388
+ ClonedHistogram ,
389
+ ClonedRecordableHistogram ,
374
390
isHistogram,
375
391
kDestroy,
376
392
kHandle,
0 commit comments