@fluid-topics/ft-wc-utils 1.2.73 → 1.3.1
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/build/CacheRegistry.d.ts +21 -15
- package/build/CacheRegistry.js +71 -53
- package/build/globals.min.js +11 -11
- package/package.json +3 -3
package/build/CacheRegistry.d.ts
CHANGED
|
@@ -5,26 +5,32 @@ export declare class ClearCacheEvent extends CustomEvent<{
|
|
|
5
5
|
}> {
|
|
6
6
|
constructor(clearedKeys: string[]);
|
|
7
7
|
}
|
|
8
|
-
|
|
9
|
-
export
|
|
10
|
-
|
|
8
|
+
type CachedContentStatus = "REGISTERED" | "LOADING" | "RESOLVED" | "ERROR";
|
|
9
|
+
export type CachedContent = {
|
|
10
|
+
loader: () => Promise<any>;
|
|
11
|
+
final: boolean;
|
|
12
|
+
status: CachedContentStatus;
|
|
13
|
+
promise?: CancelablePromise<any>;
|
|
14
|
+
value?: any;
|
|
15
|
+
error?: Error;
|
|
16
|
+
clearTimeout?: number;
|
|
17
|
+
};
|
|
18
|
+
export declare class CacheRegistry extends WithEventBus {
|
|
11
19
|
private content;
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
registerFinal(key: string, loader: () => Promise<T>): void;
|
|
16
|
-
clearAll(dispatchEvent?: boolean): void;
|
|
20
|
+
register(key: string, loader: () => Promise<any>, final?: boolean): void;
|
|
21
|
+
registerFinal(key: string, loader: () => Promise<any>): void;
|
|
22
|
+
clearAll(dispatchEvent?: boolean): string[];
|
|
17
23
|
clear(key: string, dispatchEvent?: boolean): string | undefined;
|
|
18
24
|
private forceClear;
|
|
19
25
|
private clearClearTimeout;
|
|
20
|
-
set(key: string, value:
|
|
21
|
-
setFinal(key: string, value:
|
|
22
|
-
get<U =
|
|
23
|
-
|
|
24
|
-
getNow<U =
|
|
26
|
+
set(key: string, value: any, final?: boolean): void;
|
|
27
|
+
setFinal(key: string, value: any): void;
|
|
28
|
+
get<U = any>(key: string, loader?: () => Promise<U>, ttl?: number): Promise<U>;
|
|
29
|
+
isRegistered(key: string): boolean;
|
|
30
|
+
getNow<U = any>(key: string): U | undefined;
|
|
25
31
|
has(key: string): boolean;
|
|
26
32
|
resolvedKeys(): Array<string>;
|
|
27
|
-
resolvedValues(): Array<
|
|
33
|
+
resolvedValues<U = any>(): Array<U>;
|
|
28
34
|
keys(): Array<string>;
|
|
29
|
-
values(): Array<CachedContent<T>>;
|
|
30
35
|
}
|
|
36
|
+
export {};
|
package/build/CacheRegistry.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { cancelable
|
|
1
|
+
import { cancelable } from "./CancelablePromise";
|
|
2
2
|
import { WithEventBus } from "./events";
|
|
3
3
|
export class ClearCacheEvent extends CustomEvent {
|
|
4
4
|
constructor(clearedKeys) {
|
|
@@ -8,22 +8,21 @@ export class ClearCacheEvent extends CustomEvent {
|
|
|
8
8
|
export class CacheRegistry extends WithEventBus {
|
|
9
9
|
constructor() {
|
|
10
10
|
super(...arguments);
|
|
11
|
-
this.loaders = {};
|
|
12
11
|
this.content = {};
|
|
13
|
-
this.clearTimeouts = {};
|
|
14
|
-
this.finalContent = new Set();
|
|
15
12
|
}
|
|
16
|
-
register(key, loader) {
|
|
17
|
-
|
|
18
|
-
this.
|
|
13
|
+
register(key, loader, final = false) {
|
|
14
|
+
var _a;
|
|
15
|
+
const content = this.content[key];
|
|
16
|
+
const status = (_a = content === null || content === void 0 ? void 0 : content.status) !== null && _a !== void 0 ? _a : "REGISTERED";
|
|
17
|
+
this.content[key] = { ...content, loader, final, status };
|
|
19
18
|
}
|
|
20
19
|
registerFinal(key, loader) {
|
|
21
|
-
this.
|
|
22
|
-
this.
|
|
20
|
+
this.forceClear(key); // Force the final content to be reloaded when the loader changes
|
|
21
|
+
this.register(key, loader, true);
|
|
23
22
|
}
|
|
24
23
|
clearAll(dispatchEvent = true) {
|
|
25
24
|
const clearedKeys = [];
|
|
26
|
-
for (
|
|
25
|
+
for (const key in this.content) {
|
|
27
26
|
if (this.clear(key, false) != undefined) {
|
|
28
27
|
clearedKeys.push(key);
|
|
29
28
|
}
|
|
@@ -31,9 +30,10 @@ export class CacheRegistry extends WithEventBus {
|
|
|
31
30
|
if (dispatchEvent && clearedKeys.length > 0) {
|
|
32
31
|
this.dispatchEvent(new ClearCacheEvent(clearedKeys));
|
|
33
32
|
}
|
|
33
|
+
return clearedKeys;
|
|
34
34
|
}
|
|
35
35
|
clear(key, dispatchEvent = true) {
|
|
36
|
-
if (!this.
|
|
36
|
+
if (this.content[key] && !this.content[key].final) {
|
|
37
37
|
this.forceClear(key);
|
|
38
38
|
if (dispatchEvent) {
|
|
39
39
|
this.dispatchEvent(new ClearCacheEvent([key]));
|
|
@@ -43,73 +43,91 @@ export class CacheRegistry extends WithEventBus {
|
|
|
43
43
|
return undefined;
|
|
44
44
|
}
|
|
45
45
|
forceClear(key) {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
46
|
+
var _a;
|
|
47
|
+
const content = this.content[key];
|
|
48
|
+
if (content) {
|
|
49
|
+
this.clearClearTimeout(key);
|
|
50
|
+
(_a = content.promise) === null || _a === void 0 ? void 0 : _a.cancel();
|
|
51
|
+
this.content[key] = {
|
|
52
|
+
loader: content.loader,
|
|
53
|
+
final: content.final,
|
|
54
|
+
status: "REGISTERED",
|
|
55
|
+
};
|
|
49
56
|
}
|
|
50
|
-
delete this.content[key];
|
|
51
57
|
}
|
|
52
58
|
clearClearTimeout(key) {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
59
|
+
var _a, _b, _c;
|
|
60
|
+
if (((_a = this.content[key]) === null || _a === void 0 ? void 0 : _a.clearTimeout) != null) {
|
|
61
|
+
window.clearTimeout((_b = this.content[key]) === null || _b === void 0 ? void 0 : _b.clearTimeout);
|
|
62
|
+
(_c = this.content[key]) === null || _c === void 0 ? true : delete _c.clearTimeout;
|
|
56
63
|
}
|
|
57
64
|
}
|
|
58
|
-
set(key, value) {
|
|
65
|
+
set(key, value, final = false) {
|
|
59
66
|
this.forceClear(key);
|
|
60
|
-
|
|
61
|
-
|
|
67
|
+
const loader = async () => value;
|
|
68
|
+
const status = "RESOLVED";
|
|
69
|
+
this.content[key] = { loader, final, status, value };
|
|
62
70
|
}
|
|
63
71
|
setFinal(key, value) {
|
|
64
|
-
this.
|
|
65
|
-
this.registerFinal(key, async () => value);
|
|
66
|
-
this.content[key] = value;
|
|
72
|
+
return this.set(key, value, true);
|
|
67
73
|
}
|
|
68
74
|
async get(key, loader, ttl) {
|
|
69
|
-
if (this.
|
|
70
|
-
|
|
71
|
-
if (loader == undefined) {
|
|
75
|
+
if (!this.isRegistered(key)) {
|
|
76
|
+
if (!loader) {
|
|
72
77
|
throw new Error("Unknown cache key " + key);
|
|
73
78
|
}
|
|
74
|
-
|
|
75
|
-
this.content[key] = cancelablePromise;
|
|
76
|
-
return cancelablePromise
|
|
77
|
-
.then(v => {
|
|
78
|
-
this.content[key] = v;
|
|
79
|
-
if (ttl != null) {
|
|
80
|
-
this.clearClearTimeout(key);
|
|
81
|
-
this.clearTimeouts[key] = window.setTimeout(() => this.clear(key), ttl);
|
|
82
|
-
}
|
|
83
|
-
return v;
|
|
84
|
-
});
|
|
79
|
+
this.register(key, loader);
|
|
85
80
|
}
|
|
86
|
-
|
|
87
|
-
|
|
81
|
+
const content = this.content[key];
|
|
82
|
+
switch (content.status) {
|
|
83
|
+
case "ERROR":
|
|
84
|
+
throw content.error;
|
|
85
|
+
case "LOADING":
|
|
86
|
+
return content.promise;
|
|
87
|
+
case "RESOLVED":
|
|
88
|
+
return content.value;
|
|
89
|
+
case "REGISTERED":
|
|
90
|
+
loader = loader !== null && loader !== void 0 ? loader : content.loader;
|
|
91
|
+
content.status = "LOADING";
|
|
92
|
+
content.promise = cancelable(loader());
|
|
93
|
+
return content.promise
|
|
94
|
+
.then(value => {
|
|
95
|
+
content.status = "RESOLVED";
|
|
96
|
+
content.value = value;
|
|
97
|
+
if (ttl != null) {
|
|
98
|
+
this.clearClearTimeout(key);
|
|
99
|
+
content.clearTimeout = window.setTimeout(() => this.clear(key), ttl);
|
|
100
|
+
}
|
|
101
|
+
return value;
|
|
102
|
+
})
|
|
103
|
+
.catch(error => {
|
|
104
|
+
var _a;
|
|
105
|
+
if (!((_a = content.promise) === null || _a === void 0 ? void 0 : _a.isCanceled)) {
|
|
106
|
+
content.status = "ERROR";
|
|
107
|
+
content.error = error;
|
|
108
|
+
}
|
|
109
|
+
throw error;
|
|
110
|
+
});
|
|
88
111
|
}
|
|
89
|
-
return this.content[key];
|
|
90
112
|
}
|
|
91
|
-
|
|
92
|
-
return
|
|
113
|
+
isRegistered(key) {
|
|
114
|
+
return this.content[key] != undefined;
|
|
93
115
|
}
|
|
94
116
|
getNow(key) {
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
}
|
|
98
|
-
return undefined;
|
|
117
|
+
var _a;
|
|
118
|
+
return (_a = this.content[key]) === null || _a === void 0 ? void 0 : _a.value;
|
|
99
119
|
}
|
|
100
120
|
has(key) {
|
|
101
|
-
|
|
121
|
+
var _a, _b;
|
|
122
|
+
return ((_a = this.content[key]) === null || _a === void 0 ? void 0 : _a.status) === "RESOLVED" || ((_b = this.content[key]) === null || _b === void 0 ? void 0 : _b.status) === "LOADING";
|
|
102
123
|
}
|
|
103
124
|
resolvedKeys() {
|
|
104
|
-
return Object.keys(this.content).filter(key =>
|
|
125
|
+
return Object.keys(this.content).filter(key => { var _a; return ((_a = this.content[key]) === null || _a === void 0 ? void 0 : _a.status) === "RESOLVED"; });
|
|
105
126
|
}
|
|
106
127
|
resolvedValues() {
|
|
107
|
-
return
|
|
128
|
+
return this.resolvedKeys().map(key => this.content[key].value);
|
|
108
129
|
}
|
|
109
130
|
keys() {
|
|
110
131
|
return Object.keys(this.content);
|
|
111
132
|
}
|
|
112
|
-
values() {
|
|
113
|
-
return Object.values(this.content);
|
|
114
|
-
}
|
|
115
133
|
}
|