import { IPromise, Result } from '@pilotlab/result'; import ICache from './iCache'; export abstract class Cache implements ICache { /*====================================================================* START: Properties *====================================================================*/ private _cache:Map = new Map(); /*====================================================================* START: Public Methods *====================================================================*/ get(key:string, isCache:boolean = false):IPromise { let result:IPromise = new Result(); if (this._cache.has(key)) result.resolve(this._cache.get(key)); else this.p_createNew(key, result, isCache).then((obj:T) => { if (isCache) this._cache.set(key, obj); }); return result; } preLoad(key:string):IPromise { return this.get(key, true); } release(key:string):void { this._cache.delete(key); } releaseAll():void { this._cache.clear(); } protected p_createNew(key:string, result:IPromise, isCache:boolean):IPromise { return result.resolve(null); } } // End class export default Cache;