@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.
@@ -5,26 +5,32 @@ export declare class ClearCacheEvent extends CustomEvent<{
5
5
  }> {
6
6
  constructor(clearedKeys: string[]);
7
7
  }
8
- export type CachedContent<T = any> = undefined | CancelablePromise<T> | T | Error;
9
- export declare class CacheRegistry<T = any> extends WithEventBus {
10
- private loaders;
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
- private clearTimeouts;
13
- private finalContent;
14
- register(key: string, loader: () => Promise<T>): void;
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: T): void;
21
- setFinal(key: string, value: T): void;
22
- get<U = T>(key: string, loader?: () => Promise<U>, ttl?: number): Promise<U>;
23
- private isResolvedValue;
24
- getNow<U = T>(key: string): U | undefined;
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<T>;
33
+ resolvedValues<U = any>(): Array<U>;
28
34
  keys(): Array<string>;
29
- values(): Array<CachedContent<T>>;
30
35
  }
36
+ export {};
@@ -1,4 +1,4 @@
1
- import { cancelable, CancelablePromise } from "./CancelablePromise";
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
- this.loaders[key] = loader;
18
- this.finalContent.delete(key);
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.loaders[key] = loader;
22
- this.finalContent.add(key);
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 (let key in this.content) {
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.finalContent.has(key)) {
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
- this.clearClearTimeout(key);
47
- if (this.content[key] instanceof CancelablePromise) {
48
- this.content[key].cancel();
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
- if (this.clearTimeouts[key] != null) {
54
- window.clearTimeout(this.clearTimeouts[key]);
55
- delete this.clearTimeouts[key];
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
- this.register(key, async () => value);
61
- this.content[key] = value;
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.forceClear(key);
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.content[key] === undefined) {
70
- loader = loader !== null && loader !== void 0 ? loader : this.loaders[key];
71
- if (loader == undefined) {
75
+ if (!this.isRegistered(key)) {
76
+ if (!loader) {
72
77
  throw new Error("Unknown cache key " + key);
73
78
  }
74
- const cancelablePromise = cancelable(loader());
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
- if (this.content[key] instanceof Error) {
87
- throw this.content[key];
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
- isResolvedValue(value) {
92
- return value != null && !(value instanceof Promise) && !(value instanceof Error);
113
+ isRegistered(key) {
114
+ return this.content[key] != undefined;
93
115
  }
94
116
  getNow(key) {
95
- if (this.isResolvedValue(this.content[key])) {
96
- return this.content[key];
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
- return this.content[key] != null;
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 => this.isResolvedValue(this.content[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 Object.values(this.content).filter(value => this.isResolvedValue(value));
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
  }