All files cache.ts

100% Statements 18/18
100% Branches 3/3
100% Functions 5/5
100% Lines 17/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 341x 13x 13x     13x 13x       13x 13x 13x 13x       17x 17x 1x     17x 8x   17x 17x       19x      
export class Cache extends Map {
  private ttl = 60000;
  private schedules = new Map();
 
  constructor(ttl?: number) {
    super();
    if (ttl !== undefined) this.ttl = ttl;
  }
 
  public delete(key: any): boolean {
    const result = super.delete(key);
    clearTimeout(this.schedules.get(key));
    this.schedules.delete(key);
    return result;
  }
 
  public set(key: any, value: any, ttl = this.ttl): this {
    super.set(key, value);
    if (this.schedules.has(key)) {
      clearTimeout(this.schedules.get(key));
    }
 
    const schedule = setTimeout(() => {
      this.delete(key);
    }, ttl);
    this.schedules.set(key, schedule);
    return this;
  }
 
  public get(key: any): any {
    return super.get(key);
  }
}