import { hashSum } from "./hashSum" ; export class Cache < I , C > { private _cache : { [ id ] : C ; } = { } ; private _calcID : ( espObj : I | C ) => string ; static hash ( ... args ) { return hashSum ( { ... args } ) ; } constructor ( calcID ) { this . _calcID = calcID ; } has ( espObj ) { return this . _calcID ( espObj ) in this . _cache ; } set ( obj ) { this . _cache [ this . _calcID ( obj ) ] = obj ; return obj ; } get ( espObj ) ; get ( espObj , factory ) ; get ( espObj , factory ? ) { const retVal = this . _cache [ this . _calcID ( espObj ) ] ; if ( ! retVal ) { return factory ? this . set ( factory ( ) ) : null ; } return retVal ; } } export class AsyncCache < I , C > { private _cache : { [ id ] : Promise < C > ; } = { } ; private _calcID : ( espObj : I | C ) => string ; static hash ( ... args ) { return hashSum ( { ... args } ) ; } constructor ( calcID ) { this . _calcID = calcID ; } has ( espObj ) { return this . _calcID ( espObj ) in this . _cache ; } set ( espObj , obj ) { this . _cache [ this . _calcID ( espObj ) ] = obj ; return obj ; } get ( espObj ) ; get ( espObj , factory ) ; get ( espObj , factory ? ) { const retVal = this . _cache [ this . _calcID ( espObj ) ] ; if ( ! retVal ) { return factory ? this . set ( espObj , factory ( ) ) : Promise . resolve ( null ) ; } return retVal ; } }