zavadil-ts-common 1.2.42 → 1.2.43

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,28 +1,21 @@
1
- export class CacheAsync<T> {
1
+ import {LazyAsync} from "./LazyAsync";
2
2
 
3
- private cache?: T;
4
-
5
- private supplier: () => Promise<T>;
3
+ export class CacheAsync<T> extends LazyAsync<T> {
6
4
 
7
5
  private maxAgeMs?: number;
8
6
 
9
7
  private expires?: Date;
10
8
 
11
9
  constructor(supplier: () => Promise<T>, maxAgeMs?: number) {
12
- this.supplier = supplier;
10
+ super(supplier);
13
11
  this.maxAgeMs = maxAgeMs;
14
12
  }
15
13
 
16
14
  get(): Promise<T> {
17
- if (this.cache === undefined || (this.expires && this.expires > new Date())) {
18
- return this.supplier()
19
- .then((v: T) => {
20
- this.set(v);
21
- return v;
22
- });
23
- } else {
24
- return Promise.resolve(this.cache);
15
+ if (this.expires && this.expires > new Date()) {
16
+ this.reset();
25
17
  }
18
+ return super.get();
26
19
  }
27
20
 
28
21
  set(v: T, expires?: Date) {
@@ -33,11 +26,4 @@ export class CacheAsync<T> {
33
26
  }
34
27
  }
35
28
 
36
- hasCache() {
37
- return (this.cache !== undefined);
38
- }
39
-
40
- getCache(): T | undefined {
41
- return this.cache;
42
- }
43
29
  }
@@ -1,8 +1,8 @@
1
1
  export class LazyAsync<T> {
2
2
 
3
- private cache?: T;
3
+ protected cache?: T;
4
4
 
5
- private supplier: () => Promise<T>;
5
+ protected supplier: () => Promise<T>;
6
6
 
7
7
  private promise?: Promise<T>;
8
8