wowok
Version:
Wowok Blockchain TypeScript API
1 lines • 906 B
JavaScript
export class ClientCache{#prefix;#cache;constructor({prefix:a,cache:b}={}){this.#prefix=a??[],this.#cache=b??new Map();}['read'](a,b){const c=[this.#prefix,...a]['join'](':');if(this.#cache['has'](c))return this.#cache['get'](c);const d=b();this.#cache['set'](c,d);if(typeof d==='object'&&d!==null&&'then'in d)return Promise['resolve'](d)['then'](e=>{return this.#cache['set'](c,e),e;})['catch'](e=>{this.#cache['delete'](c);throw e;});return d;}['readSync'](a,b){const c=[this.#prefix,...a]['join'](':');if(this.#cache['has'](c))return this.#cache['get'](c);const d=b();return this.#cache['set'](c,d),d;}['clear'](a){const b=[...this.#prefix,...a??[]]['join'](':');if(!b){this.#cache['clear']();return;}for(const c of this.#cache['keys']()){c['startsWith'](b)&&this.#cache['delete'](c);}}['scope'](a){return new ClientCache({'prefix':[...this.#prefix,...Array['isArray'](a)?a:[a]],'cache':this.#cache});}}