4
4
*/
5
5
Object . defineProperty ( exports , "__esModule" , { value : true } ) ;
6
6
exports . LRUCache = void 0 ;
7
- const defaultPerf = ( typeof performance === 'object' &&
7
+ const perf = typeof performance === 'object' &&
8
8
performance &&
9
- typeof performance . now === 'function' ) ?
10
- performance
9
+ typeof performance . now === 'function'
10
+ ? performance
11
11
: Date ;
12
12
const warned = new Set ( ) ;
13
13
/* c8 ignore start */
14
- const PROCESS = ( typeof process === 'object' && ! ! process ?
15
- process
16
- : { } ) ;
14
+ const PROCESS = ( typeof process === 'object' && ! ! process ? process : { } ) ;
17
15
/* c8 ignore start */
18
16
const emitWarning = ( msg , type , code , fn ) => {
19
- typeof PROCESS . emitWarning === 'function' ?
20
- PROCESS . emitWarning ( msg , type , code , fn )
17
+ typeof PROCESS . emitWarning === 'function'
18
+ ? PROCESS . emitWarning ( msg , type , code , fn )
21
19
: console . error ( `[${ code } ] ${ type } : ${ msg } ` ) ;
22
20
} ;
23
21
let AC = globalThis . AbortController ;
@@ -81,11 +79,16 @@ const isPosInt = (n) => n && n === Math.floor(n) && n > 0 && isFinite(n);
81
79
// zeroes at init time is brutal when you get that big.
82
80
// But why not be complete?
83
81
// Maybe in the future, these limits will have expanded.
84
- const getUintArray = ( max ) => ! isPosInt ( max ) ? null
85
- : max <= Math . pow ( 2 , 8 ) ? Uint8Array
86
- : max <= Math . pow ( 2 , 16 ) ? Uint16Array
87
- : max <= Math . pow ( 2 , 32 ) ? Uint32Array
88
- : max <= Number . MAX_SAFE_INTEGER ? ZeroArray
82
+ const getUintArray = ( max ) => ! isPosInt ( max )
83
+ ? null
84
+ : max <= Math . pow ( 2 , 8 )
85
+ ? Uint8Array
86
+ : max <= Math . pow ( 2 , 16 )
87
+ ? Uint16Array
88
+ : max <= Math . pow ( 2 , 32 )
89
+ ? Uint32Array
90
+ : max <= Number . MAX_SAFE_INTEGER
91
+ ? ZeroArray
89
92
: null ;
90
93
/* c8 ignore stop */
91
94
class ZeroArray extends Array {
@@ -144,17 +147,9 @@ class LRUCache {
144
147
#max;
145
148
#maxSize;
146
149
#dispose;
147
- #onInsert;
148
150
#disposeAfter;
149
151
#fetchMethod;
150
152
#memoMethod;
151
- #perf;
152
- /**
153
- * {@link LRUCache.OptionsBase.perf }
154
- */
155
- get perf ( ) {
156
- return this . #perf;
157
- }
158
153
/**
159
154
* {@link LRUCache.OptionsBase.ttl }
160
155
*/
@@ -233,7 +228,6 @@ class LRUCache {
233
228
#hasDispose;
234
229
#hasFetchMethod;
235
230
#hasDisposeAfter;
236
- #hasOnInsert;
237
231
/**
238
232
* Do not call this method unless you need to inspect the
239
233
* inner workings of the cache. If anything returned by this
@@ -310,26 +304,14 @@ class LRUCache {
310
304
get dispose ( ) {
311
305
return this . #dispose;
312
306
}
313
- /**
314
- * {@link LRUCache.OptionsBase.onInsert } (read-only)
315
- */
316
- get onInsert ( ) {
317
- return this . #onInsert;
318
- }
319
307
/**
320
308
* {@link LRUCache.OptionsBase.disposeAfter } (read-only)
321
309
*/
322
310
get disposeAfter ( ) {
323
311
return this . #disposeAfter;
324
312
}
325
313
constructor ( options ) {
326
- const { max = 0 , ttl, ttlResolution = 1 , ttlAutopurge, updateAgeOnGet, updateAgeOnHas, allowStale, dispose, onInsert, disposeAfter, noDisposeOnSet, noUpdateTTL, maxSize = 0 , maxEntrySize = 0 , sizeCalculation, fetchMethod, memoMethod, noDeleteOnFetchRejection, noDeleteOnStaleGet, allowStaleOnFetchRejection, allowStaleOnFetchAbort, ignoreFetchAbort, perf, } = options ;
327
- if ( perf !== undefined ) {
328
- if ( typeof perf ?. now !== 'function' ) {
329
- throw new TypeError ( 'perf option must have a now() method if specified' ) ;
330
- }
331
- }
332
- this . #perf = perf ?? defaultPerf ;
314
+ const { max = 0 , ttl, ttlResolution = 1 , ttlAutopurge, updateAgeOnGet, updateAgeOnHas, allowStale, dispose, disposeAfter, noDisposeOnSet, noUpdateTTL, maxSize = 0 , maxEntrySize = 0 , sizeCalculation, fetchMethod, memoMethod, noDeleteOnFetchRejection, noDeleteOnStaleGet, allowStaleOnFetchRejection, allowStaleOnFetchAbort, ignoreFetchAbort, } = options ;
333
315
if ( max !== 0 && ! isPosInt ( max ) ) {
334
316
throw new TypeError ( 'max option must be a nonnegative integer' ) ;
335
317
}
@@ -373,9 +355,6 @@ class LRUCache {
373
355
if ( typeof dispose === 'function' ) {
374
356
this . #dispose = dispose ;
375
357
}
376
- if ( typeof onInsert === 'function' ) {
377
- this . #onInsert = onInsert ;
378
- }
379
358
if ( typeof disposeAfter === 'function' ) {
380
359
this . #disposeAfter = disposeAfter ;
381
360
this . #disposed = [ ] ;
@@ -385,7 +364,6 @@ class LRUCache {
385
364
this . #disposed = undefined ;
386
365
}
387
366
this . #hasDispose = ! ! this . #dispose;
388
- this . #hasOnInsert = ! ! this . #onInsert;
389
367
this . #hasDisposeAfter = ! ! this . #disposeAfter;
390
368
this . noDisposeOnSet = ! ! noDisposeOnSet ;
391
369
this . noUpdateTTL = ! ! noUpdateTTL ;
@@ -410,8 +388,8 @@ class LRUCache {
410
388
this . updateAgeOnGet = ! ! updateAgeOnGet ;
411
389
this . updateAgeOnHas = ! ! updateAgeOnHas ;
412
390
this . ttlResolution =
413
- isPosInt ( ttlResolution ) || ttlResolution === 0 ?
414
- ttlResolution
391
+ isPosInt ( ttlResolution ) || ttlResolution === 0
392
+ ? ttlResolution
415
393
: 1 ;
416
394
this . ttlAutopurge = ! ! ttlAutopurge ;
417
395
this . ttl = ttl || 0 ;
@@ -447,7 +425,7 @@ class LRUCache {
447
425
const starts = new ZeroArray ( this . #max) ;
448
426
this . #ttls = ttls ;
449
427
this . #starts = starts ;
450
- this . #setItemTTL = ( index , ttl , start = this . # perf. now ( ) ) => {
428
+ this . #setItemTTL = ( index , ttl , start = perf . now ( ) ) => {
451
429
starts [ index ] = ttl !== 0 ? start : 0 ;
452
430
ttls [ index ] = ttl ;
453
431
if ( ttl !== 0 && this . ttlAutopurge ) {
@@ -465,7 +443,7 @@ class LRUCache {
465
443
}
466
444
} ;
467
445
this . #updateItemAge = index => {
468
- starts [ index ] = ttls [ index ] !== 0 ? this . # perf. now ( ) : 0 ;
446
+ starts [ index ] = ttls [ index ] !== 0 ? perf . now ( ) : 0 ;
469
447
} ;
470
448
this . #statusTTL = ( status , index ) => {
471
449
if ( ttls [ index ] ) {
@@ -485,7 +463,7 @@ class LRUCache {
485
463
// that costly call repeatedly.
486
464
let cachedNow = 0 ;
487
465
const getNow = ( ) => {
488
- const n = this . # perf. now ( ) ;
466
+ const n = perf . now ( ) ;
489
467
if ( this . ttlResolution > 0 ) {
490
468
cachedNow = n ;
491
469
const t = setTimeout ( ( ) => ( cachedNow = 0 ) , this . ttlResolution ) ;
@@ -722,7 +700,9 @@ class LRUCache {
722
700
find ( fn , getOptions = { } ) {
723
701
for ( const i of this . #indexes( ) ) {
724
702
const v = this . #valList[ i ] ;
725
- const value = this . #isBackgroundFetch( v ) ? v . __staleWhileFetching : v ;
703
+ const value = this . #isBackgroundFetch( v )
704
+ ? v . __staleWhileFetching
705
+ : v ;
726
706
if ( value === undefined )
727
707
continue ;
728
708
if ( fn ( value , this . #keyList[ i ] , this ) ) {
@@ -744,7 +724,9 @@ class LRUCache {
744
724
forEach ( fn , thisp = this ) {
745
725
for ( const i of this . #indexes( ) ) {
746
726
const v = this . #valList[ i ] ;
747
- const value = this . #isBackgroundFetch( v ) ? v . __staleWhileFetching : v ;
727
+ const value = this . #isBackgroundFetch( v )
728
+ ? v . __staleWhileFetching
729
+ : v ;
748
730
if ( value === undefined )
749
731
continue ;
750
732
fn . call ( thisp , value , this . #keyList[ i ] , this ) ;
@@ -757,7 +739,9 @@ class LRUCache {
757
739
rforEach ( fn , thisp = this ) {
758
740
for ( const i of this . #rindexes( ) ) {
759
741
const v = this . #valList[ i ] ;
760
- const value = this . #isBackgroundFetch( v ) ? v . __staleWhileFetching : v ;
742
+ const value = this . #isBackgroundFetch( v )
743
+ ? v . __staleWhileFetching
744
+ : v ;
761
745
if ( value === undefined )
762
746
continue ;
763
747
fn . call ( thisp , value , this . #keyList[ i ] , this ) ;
@@ -794,18 +778,17 @@ class LRUCache {
794
778
if ( i === undefined )
795
779
return undefined ;
796
780
const v = this . #valList[ i ] ;
797
- /* c8 ignore start - this isn't tested for the info function,
798
- * but it's the same logic as found in other places. */
799
- const value = this . #isBackgroundFetch ( v ) ? v . __staleWhileFetching : v ;
781
+ const value = this . #isBackgroundFetch ( v )
782
+ ? v . __staleWhileFetching
783
+ : v ;
800
784
if ( value === undefined )
801
785
return undefined ;
802
- /* c8 ignore end */
803
786
const entry = { value } ;
804
787
if ( this . #ttls && this . #starts) {
805
788
const ttl = this . #ttls[ i ] ;
806
789
const start = this . #starts[ i ] ;
807
790
if ( ttl && start ) {
808
- const remain = ttl - ( this . # perf. now ( ) - start ) ;
791
+ const remain = ttl - ( perf . now ( ) - start ) ;
809
792
entry . ttl = remain ;
810
793
entry . start = Date . now ( ) ;
811
794
}
@@ -817,7 +800,7 @@ class LRUCache {
817
800
}
818
801
/**
819
802
* Return an array of [key, {@link LRUCache.Entry}] tuples which can be
820
- * passed to {@link LRUCache #load}.
803
+ * passed to {@link LRLUCache #load}.
821
804
*
822
805
* The `start` fields are calculated relative to a portable `Date.now()`
823
806
* timestamp, even if `performance.now()` is available.
@@ -833,15 +816,17 @@ class LRUCache {
833
816
for ( const i of this . #indexes( { allowStale : true } ) ) {
834
817
const key = this . #keyList[ i ] ;
835
818
const v = this . #valList[ i ] ;
836
- const value = this . #isBackgroundFetch( v ) ? v . __staleWhileFetching : v ;
819
+ const value = this . #isBackgroundFetch( v )
820
+ ? v . __staleWhileFetching
821
+ : v ;
837
822
if ( value === undefined || key === undefined )
838
823
continue ;
839
824
const entry = { value } ;
840
825
if ( this . #ttls && this . #starts) {
841
826
entry . ttl = this . #ttls[ i ] ;
842
827
// always dump the start relative to a portable timestamp
843
828
// it's ok for this to be a bit slow, it's a rare operation.
844
- const age = this . # perf. now ( ) - this . #starts[ i ] ;
829
+ const age = perf . now ( ) - this . #starts[ i ] ;
845
830
entry . start = Math . floor ( Date . now ( ) - age ) ;
846
831
}
847
832
if ( this . #sizes) {
@@ -871,7 +856,7 @@ class LRUCache {
871
856
//
872
857
// it's ok for this to be a bit slow, it's a rare operation.
873
858
const age = Date . now ( ) - entry . start ;
874
- entry . start = this . # perf. now ( ) - age ;
859
+ entry . start = perf . now ( ) - age ;
875
860
}
876
861
this . set ( key , entry . value , entry ) ;
877
862
}
@@ -928,9 +913,12 @@ class LRUCache {
928
913
let index = this . #size === 0 ? undefined : this . #keyMap. get ( k ) ;
929
914
if ( index === undefined ) {
930
915
// addition
931
- index = ( this . #size === 0 ? this . #tail
932
- : this . #free. length !== 0 ? this . #free. pop ( )
933
- : this . #size === this . #max ? this . #evict( false )
916
+ index = ( this . #size === 0
917
+ ? this . #tail
918
+ : this . #free. length !== 0
919
+ ? this . #free. pop ( )
920
+ : this . #size === this . #max
921
+ ? this . #evict( false )
934
922
: this . #size) ;
935
923
this . #keyList[ index ] = k ;
936
924
this . #valList[ index ] = v ;
@@ -943,9 +931,6 @@ class LRUCache {
943
931
if ( status )
944
932
status . set = 'add' ;
945
933
noUpdateTTL = false ;
946
- if ( this . #hasOnInsert) {
947
- this . #onInsert?. ( v , k , 'add' ) ;
948
- }
949
934
}
950
935
else {
951
936
// update
@@ -977,8 +962,8 @@ class LRUCache {
977
962
this . #valList[ index ] = v ;
978
963
if ( status ) {
979
964
status . set = 'replace' ;
980
- const oldValue = oldVal && this . #isBackgroundFetch( oldVal ) ?
981
- oldVal . __staleWhileFetching
965
+ const oldValue = oldVal && this . #isBackgroundFetch( oldVal )
966
+ ? oldVal . __staleWhileFetching
982
967
: oldVal ;
983
968
if ( oldValue !== undefined )
984
969
status . oldValue = oldValue ;
@@ -987,9 +972,6 @@ class LRUCache {
987
972
else if ( status ) {
988
973
status . set = 'update' ;
989
974
}
990
- if ( this . #hasOnInsert) {
991
- this . onInsert ?. ( v , k , v === oldVal ? 'update' : 'replace' ) ;
992
- }
993
975
}
994
976
if ( ttl !== 0 && ! this . #ttls) {
995
977
this . #initializeTTLTracking( ) ;
@@ -1172,7 +1154,7 @@ class LRUCache {
1172
1154
const bf = p ;
1173
1155
if ( this . #valList[ index ] === p ) {
1174
1156
if ( v === undefined ) {
1175
- if ( bf . __staleWhileFetching !== undefined ) {
1157
+ if ( bf . __staleWhileFetching ) {
1176
1158
this . #valList[ index ] = bf . __staleWhileFetching ;
1177
1159
}
1178
1160
else {
0 commit comments