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.
- package/dist/cache/CacheAsync.d.ts +2 -5
- package/dist/cache/LazyAsync.d.ts +2 -2
- package/dist/index.d.ts +12 -16
- package/dist/index.esm.js +1 -1
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/cache/CacheAsync.ts +6 -20
- package/src/cache/LazyAsync.ts +2 -2
package/src/cache/CacheAsync.ts
CHANGED
@@ -1,28 +1,21 @@
|
|
1
|
-
|
1
|
+
import {LazyAsync} from "./LazyAsync";
|
2
2
|
|
3
|
-
|
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
|
-
|
10
|
+
super(supplier);
|
13
11
|
this.maxAgeMs = maxAgeMs;
|
14
12
|
}
|
15
13
|
|
16
14
|
get(): Promise<T> {
|
17
|
-
if (this.
|
18
|
-
|
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
|
}
|