Skip to content

Commit 2aa6c44

Browse files
author
xingyu.ren
committed
fix: 🐛 fix destroy and cacheRequest lock
1 parent 217ce19 commit 2aa6c44

File tree

1 file changed

+19
-38
lines changed

1 file changed

+19
-38
lines changed

src/global-store.ts

Lines changed: 19 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,6 @@ export interface GlobalStoreInitParams {
4141
export interface CancelQueriesParams {
4242
queryKeys?: string[];
4343
}
44-
export class GlobalStoreDestroyedError extends Error {
45-
constructor(message = 'GlobalStore has been destroyed') {
46-
super(message);
47-
this.name = 'GlobalStoreDestroyedError';
48-
}
49-
}
5044
export class GlobalStore {
5145
private _apiUrl = '';
5246
private watchWsApiUrl?: string;
@@ -82,10 +76,21 @@ export class GlobalStore {
8276
get kubeApiTimeout() {
8377
return this._kubeApiTimeout;
8478
}
85-
79+
get isDestroyed() {
80+
return this._isDestroyed;
81+
}
82+
// clear cache request
83+
removeCacheRequest(targetPromise: Promise<unknown>) {
84+
const cacheRequestIndex = this.requestsCache.findIndex(f => {
85+
return f.promise === targetPromise;
86+
});
87+
if (cacheRequestIndex > -1) {
88+
this.requestsCache.splice(cacheRequestIndex, 1);
89+
}
90+
}
8691
get<T = UnstructuredList>(resource: string, meta?: MetaQuery): Promise<T> {
8792
if (this._isDestroyed) {
88-
return Promise.reject(new GlobalStoreDestroyedError());
93+
return Promise.reject('GlobalStore has been destroyed');
8994
}
9095

9196
if (this.store.has(resource)) {
@@ -123,6 +128,7 @@ export class GlobalStore {
123128
.listWatch({
124129
onResponse: async (res, event) => {
125130
if (this._isDestroyed) {
131+
console.error('GlobalStore has been destroyed');
126132
return;
127133
}
128134

@@ -137,6 +143,8 @@ export class GlobalStore {
137143
await this.processItem(event.object);
138144
this.notify(resource, event);
139145
}
146+
// TODO: if the request onResponse is timeout, the cache request will not be removed
147+
this.removeCacheRequest(promise);
140148
},
141149
signal,
142150
})
@@ -147,15 +155,9 @@ export class GlobalStore {
147155
}
148156
this.stopWatchHandlers.set(resource, stop);
149157
})
150-
.catch(e => reject(e))
151-
.finally(() => {
152-
// clear cache request
153-
const cacheRequestIndex = this.requestsCache.findIndex(f => {
154-
return f.promise === promise;
155-
});
156-
if (cacheRequestIndex > -1) {
157-
this.requestsCache.splice(cacheRequestIndex, 1);
158-
}
158+
.catch(e => {
159+
this.removeCacheRequest(promise);
160+
reject(e);
159161
});
160162
});
161163

@@ -169,10 +171,6 @@ export class GlobalStore {
169171
}
170172

171173
subscribe(resource: string, onEvent: (data: WatchEvent) => void) {
172-
if (this._isDestroyed) {
173-
throw new GlobalStoreDestroyedError();
174-
}
175-
176174
if (!this.subscribers.has(resource)) {
177175
this.subscribers.set(resource, []);
178176
}
@@ -189,10 +187,6 @@ export class GlobalStore {
189187
}
190188

191189
private notify(resource: string, data: WatchEvent) {
192-
if (this._isDestroyed) {
193-
return;
194-
}
195-
196190
const subscribers = this.subscribers.get(resource);
197191
if (subscribers) {
198192
for (const subscriber of subscribers) {
@@ -202,10 +196,6 @@ export class GlobalStore {
202196
}
203197

204198
publish(resource: string, data: WatchEvent) {
205-
if (this._isDestroyed) {
206-
return;
207-
}
208-
209199
this.notify(resource, data);
210200
}
211201

@@ -239,16 +229,11 @@ export class GlobalStore {
239229
}
240230
loadPlugins(plugins?: IProviderPlugin[]) {
241231
if (plugins) {
242-
this.destroy();
243232
this.plugins = plugins;
244233
this.plugins.forEach(plugin => plugin.init(this));
245234
}
246235
}
247236
private async processList(list: UnstructuredList) {
248-
if (this._isDestroyed) {
249-
return list;
250-
}
251-
252237
let nextList = list;
253238
nextList.items.forEach(item => {
254239
item.id = genResourceId(item);
@@ -260,10 +245,6 @@ export class GlobalStore {
260245
}
261246

262247
private async processItem(item: Unstructured) {
263-
if (this._isDestroyed) {
264-
return item;
265-
}
266-
267248
let nextItem = item;
268249
nextItem.id = genResourceId(item);
269250
for (const plugin of this.plugins) {

0 commit comments

Comments
 (0)