UNPKG

@pilotlab/lux-tools

Version:

A luxurious user experience framework, developed by your friends at Pilot.

33 lines (22 loc) 1.21 kB
import { IPromise, Result } from '@pilotlab/result'; import ICache from './iCache'; export abstract class Cache<T> implements ICache { /*====================================================================* START: Properties *====================================================================*/ private _cache:Map<string, T> = new Map<string, T>(); /*====================================================================* START: Public Methods *====================================================================*/ get(key:string, isCache:boolean = false):IPromise<T> { let result:IPromise<T> = new Result<T>(); 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<T> { 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<T>, isCache:boolean):IPromise<T> { return result.resolve(null); } } // End class export default Cache;