import { KeyType } from './key-type' ; import { Cache } from './cache' ; import { CacheSPI } from './cache-spi' ; import { AbstractCache } from './abstract' ; import { Metrics } from './metrics/metrics' ; import { RemovalListener } from './removal-listener' ; import { ON_REMOVE , ON_MAINTENANCE , TRIGGER_REMOVE } from './symbols' ; import { RemovalReason } from './removal-reason' ; const PARENT = Symbol ( 'parent' ) ; const REMOVAL_LISTENER = Symbol ( 'removalListener' ) ; /**
 * Wrapper for another cache, used to extend that cache with new behavior,
 * like for loading things or collecting metrics.
 */ export abstract class WrappedCache < K extends KeyType , V > extends AbstractCache < K , V > implements Cache < K , V > , CacheSPI < K , V > { private [ PARENT ] : Cache < K , V > & CacheSPI < K , V > ; public [ ON_REMOVE ] ? : RemovalListener < K , V > ; private [ REMOVAL_LISTENER ] : RemovalListener < K , V > | null ; constructor ( parent , removalListener ) { super ( ) ; this [ PARENT ] = parent ; this [ REMOVAL_LISTENER ] = removalListener ; this [ PARENT ] [ ON_REMOVE ] = this [ TRIGGER_REMOVE ] . bind ( this ) ; } get maxSize ( ) : number { return this [ PARENT ] . maxSize ; } get size ( ) : number { return this [ PARENT ] . size ; } get weightedSize ( ) : number { return this [ PARENT ] . weightedSize ; } public set ( key , value ) { return this [ PARENT ] . set ( key , value ) ; } public getIfPresent ( key ) { return this [ PARENT ] . getIfPresent ( key ) ; } public peek ( key ) { return this [ PARENT ] . peek ( key ) ; } public has ( key ) { return this [ PARENT ] . has ( key ) ; } public delete ( key ) { return this [ PARENT ] . delete ( key ) ; } public clear ( ) { return this [ PARENT ] . clear ( ) ; } public keys ( ) { return this [ PARENT ] . keys ( ) ; } public cleanUp ( ) { return this [ PARENT ] . cleanUp ( ) ; } public get metrics ( ) : Metrics { return this [ PARENT ] . metrics ; } public get [ ON_MAINTENANCE ] ( ) : ( ( ) => void ) | undefined { return this [ PARENT ] [ ON_MAINTENANCE ] ; } public set [ ON_MAINTENANCE ] ( listener ) { this [ PARENT ] [ ON_MAINTENANCE ] = listener ; } private [ TRIGGER_REMOVE ] ( key , value , reason ) { const onRemove = this [ ON_REMOVE ] ; if ( onRemove ) { onRemove ( key , value , reason ) ; } const listener = this [ REMOVAL_LISTENER ] ; if ( listener ) { listener ( key , value , reason ) ; } } }