zavadil-ts-common 1.0.3 → 1.0.5
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/package.json
CHANGED
@@ -0,0 +1,17 @@
|
|
1
|
+
export class Lazy<T> {
|
2
|
+
|
3
|
+
private cache?: T;
|
4
|
+
|
5
|
+
private supplier: () => T;
|
6
|
+
|
7
|
+
constructor(supplier: () => T) {
|
8
|
+
this.supplier = supplier;
|
9
|
+
}
|
10
|
+
|
11
|
+
get(): T {
|
12
|
+
if (this.cache === undefined) {
|
13
|
+
this.cache = this.supplier();
|
14
|
+
}
|
15
|
+
return this.cache;
|
16
|
+
}
|
17
|
+
}
|
@@ -0,0 +1,22 @@
|
|
1
|
+
export class LazyAsync<T> {
|
2
|
+
|
3
|
+
private cache?: T;
|
4
|
+
|
5
|
+
private supplier: () => Promise<T>;
|
6
|
+
|
7
|
+
constructor(supplier: () => Promise<T>) {
|
8
|
+
this.supplier = supplier;
|
9
|
+
}
|
10
|
+
|
11
|
+
get(): Promise<T> {
|
12
|
+
if (this.cache === undefined) {
|
13
|
+
return this.supplier()
|
14
|
+
.then((v: T) => {
|
15
|
+
this.cache = v;
|
16
|
+
return v;
|
17
|
+
});
|
18
|
+
} else {
|
19
|
+
return Promise.resolve(this.cache);
|
20
|
+
}
|
21
|
+
}
|
22
|
+
}
|