Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cache global store get requests #34

Merged
merged 1 commit into from
Jan 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 69 additions & 34 deletions src/global-store.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { MetaQuery } from '@refinedev/core';
import { isEqual } from 'lodash';
import {
KubeApi,
UnstructuredList,
Expand Down Expand Up @@ -39,6 +40,11 @@ export class GlobalStore {
fieldManager?: string;

private store = new Map<string, UnstructuredList>();
private requestsCache: Array<{
resource: string;
meta?: MetaQuery;
promise: Promise<unknown>;
}> = [];
private subscribers = new Map<string, ((data: WatchEvent) => void)[]>();
private stopWatchHandlers = new Map<string, StopWatchHandler>();
private cancelControllers = new Map<string, AbortController>();
Expand All @@ -60,42 +66,71 @@ export class GlobalStore {
}

get<T = UnstructuredList>(resource: string, meta?: MetaQuery): Promise<T> {
return new Promise((resolve, reject) => {
if (!this.store.has(resource)) {
const kubeApi = new KubeApi({
basePath: this._apiUrl,
watchWsBasePath: this.watchWsApiUrl,
objectConstructor: getObjectConstructor(resource, meta),
kubeApiTimeout: this.kubeApiTimeout,
});
const controller = new AbortController();
const { signal } = controller;
this.cancelControllers.set(resource, controller);
let resolved = false;
kubeApi
.listWatch({
onResponse: async res => {
const processedRes = await this.processList(res);
if (!resolved) {
resolve(processedRes as unknown as T);
resolved = true;
}
this.store.set(resource, processedRes);
},
onEvent: async event => {
await this.processItem(event.object);
this.notify(resource, event);
},
signal,
})
.then(stop => {
this.stopWatchHandlers.set(resource, stop);
})
.catch(e => reject(e));
} else {
if (this.store.has(resource)) {
// return cached data
return new Promise(resolve => {
resolve(this.store.get(resource)! as unknown as T);
}
});
}

const cacheRequest = this.requestsCache.find(f => {
return f.resource === resource && isEqual(f.meta, meta);
});

// return cached fetching request
if (cacheRequest) {
return cacheRequest.promise as Promise<T>;
}

const promise = new Promise<T>((resolve, reject) => {
const kubeApi = new KubeApi({
basePath: this._apiUrl,
watchWsBasePath: this.watchWsApiUrl,
objectConstructor: getObjectConstructor(resource, meta),
kubeApiTimeout: this.kubeApiTimeout,
});
const controller = new AbortController();
const { signal } = controller;
this.cancelControllers.set(resource, controller);
let resolved = false;
kubeApi
.listWatch({
onResponse: async res => {
const processedRes = await this.processList(res);
if (!resolved) {
resolve(processedRes as unknown as T);
resolved = true;
}
this.store.set(resource, processedRes);
},
onEvent: async event => {
await this.processItem(event.object);
this.notify(resource, event);
},
signal,
})
.then(stop => {
this.stopWatchHandlers.set(resource, stop);
})
.catch(e => reject(e))
.finally(() => {
// clear cache request
const cacheRequestIndex = this.requestsCache.findIndex(f => {
return f.promise === promise;
});
if (cacheRequestIndex > -1) {
this.requestsCache.splice(cacheRequestIndex, 1);
}
});
});

// cache the promise
this.requestsCache.push({
resource,
meta,
promise,
});
return promise;
}

subscribe(resource: string, onEvent: (data: WatchEvent) => void) {
Expand Down
Loading