-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpromiseCache.ts
More file actions
194 lines (157 loc) · 5.02 KB
/
Copy pathpromiseCache.ts
File metadata and controls
194 lines (157 loc) · 5.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import { createLogger, ILogger } from '../logger';
export type DeferredGetter<T> = {
readonly current: T;
readonly promise: Promise<T>;
readonly busy: boolean;
};
export namespace DeferredGetter {
const _resolvedPromise = Promise.resolve<undefined>(undefined);
export const Empty = {
get current(): undefined { return undefined; },
get promise(): Promise<undefined> { return _resolvedPromise; },
get busy() { return undefined; },
isEmpty: true,
} as DeferredGetter<null>;
}
export class PromiseCache<T, K = string> {
protected readonly _itemsCache: Record<string, T> = { };
protected readonly _itemsStatus: Record<string, boolean> = { };
private readonly _fetchCache: Record<string, Promise<T>> = { };
private _logger: ILogger = null;
protected _busyCount = 0;
constructor(
readonly fetcher: (id: K) => Promise<T>,
readonly keyAdapter?: K extends string ? null : (k: K) => string,
readonly keyParser?: K extends string ? null : (id: string) => K
) {
//
}
public get busyCount() { return this._busyCount; }
private _pk(k: K): string {
if (k == null) {
throw new Error('PromiseCache: null keys are not supported');
}
if (typeof k === 'string') {
return k;
}
if (!this.keyAdapter) {
throw new Error('Provide key adapter for non-string keys');
}
return this.keyAdapter(k);
}
useLogger(name?: string) {
this._logger = createLogger(`[PromiseCache:${name || '?'}]`);
return this;
}
getDeferred(key: K): DeferredGetter<T> {
const self = this;
return {
get current() { return self.getCurrent(key); },
get promise() { return self.get(key); },
get busy() { return self.getIsBusy(key); },
};
}
getIsBusy(id: K): boolean {
const key = this._pk(id);
return this._itemsStatus[key];
}
getCurrent(id: K, initiateFetch = true): T {
const key = this._pk(id);
const item = this._itemsCache[key];
if (initiateFetch) {
this.get(id);
}
this._logger?.log(key, 'getCurrent returns', item);
return item;
}
get(id: K): Promise<T> {
const key = this._pk(id);
const item = this._itemsCache[key];
if (item !== undefined) {
this._logger?.log(key, 'item resolved to', item);
return Promise.resolve(item);
}
let promise = this._fetchCache[key];
if (promise) {
this._logger?.log(key, 'item resolved to <promise>');
return promise;
}
this.setStatus(key, true);
promise = this._doFetchAsync(id, key);
this.setPromise(key, promise);
return promise;
}
protected _doFetchAsync = async (id: K, key: string) => {
try {
this.onBeforeFetch(key);
let res = await this.fetcher(id);
if (this._fetchCache[key]) {
this._logger?.log(key, 'item\'s <promise> resolved to', res);
res = this.prepareResult(res);
this.storeResult(key, res);
}
return res;
} finally {
this.onFetchComplete(key);
}
};
invalidate(id: K) {
const key = this._pk(id);
this._set(key, undefined, undefined, undefined);
}
updateValueDirectly(id: K, value: T) {
const key = this._pk(id);
this._set(key, value, undefined, undefined);
}
hasKey(id: K) {
const key = this._pk(id);
return this._itemsCache[key] !== undefined || this._itemsStatus[key] !== undefined;
}
keys() {
return Object.keys(this._itemsCache);
}
keysParsed() {
if (!this.keyParser) {
return null;
}
return this.keys().map(this.keyParser);
}
protected _set(key: string, item: T, promise: Promise<T>, busy: boolean) {
_setX(key, this._fetchCache, promise);
_setX(key, this._itemsStatus, busy);
_setX(key, this._itemsCache, item);
}
/** @override */
protected setStatus(key: string, status: boolean) {
this._itemsStatus[key] = status;
}
/** @override */
protected setPromise(key: string, promise: Promise<T>) {
this._fetchCache[key] = promise;
}
/** @override */
protected onBeforeFetch(_key: string) {
++this._busyCount;
}
/** @override */
protected prepareResult(res: Awaited<T>) {
return res || null;
}
/** @override */
protected storeResult(key: string, res: Awaited<T>) {
this._itemsCache[key] = res;
}
/** @override */
protected onFetchComplete(key: string) {
--this._busyCount;
delete this._fetchCache[key];
this._itemsStatus[key] = false;
}
}
function _setX<T>(key: string, obj: Record<string, T>, val: T) {
if (val === undefined) {
delete obj[key];
} else {
obj[key] = val;
}
}