File size: 507 Bytes
3c778ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
class Cache<T> {
  static readonly populatedBy = <T>(populate: () => T) => new Cache(populate);

  private readonly populate: () => T;
  private value: T | undefined;

  private constructor(populate: () => T) {
    this.populate = populate;
    this.value = undefined;
  }

  getValue(): T | undefined {
    return this.value;
  }

  access(): T {
    if (!this.value) this.value = this.populate();
    return this.value;
  }

  invalidate(): void {
    this.value = undefined;
  }
}

export default Cache;