cache date on the client

This commit is contained in:
Nikita Galaiko 2023-03-27 17:15:29 +02:00
parent 615f0092a6
commit 3653ae6a31
2 changed files with 136 additions and 66 deletions

View File

@ -23,8 +23,32 @@ export type DeltasEvent = {
filePath: string;
};
export const list = (params: { projectId: string; sessionId: string; paths?: string[] }) =>
invoke<Record<string, Delta[]>>('list_deltas', params);
const cache: Record<string, Record<string, Promise<Record<string, Delta[]>>>> = {};
export const list = async (params: { projectId: string; sessionId: string; paths?: string[] }) => {
const sessionCache = cache[params.projectId] || {};
if (params.sessionId in sessionCache) {
return sessionCache[params.sessionId].then((deltas) =>
Object.fromEntries(
Object.entries(deltas).filter(([path]) =>
params.paths ? params.paths.includes(path) : true
)
)
);
}
const promise = invoke<Record<string, Delta[]>>('list_deltas', {
projectId: params.projectId,
sessionId: params.sessionId
});
sessionCache[params.sessionId] = promise;
cache[params.projectId] = sessionCache;
return promise.then((deltas) =>
Object.fromEntries(
Object.entries(deltas).filter(([path]) => (params.paths ? params.paths.includes(path) : true))
)
);
};
export const subscribe = (
params: { projectId: string; sessionId: string },

View File

@ -29,10 +29,56 @@ export type Session = {
activity: Activity[];
};
export const listFiles = (params: { projectId: string; sessionId: string; paths?: string[] }) =>
invoke<Record<string, string>>('list_session_files', params);
const filesCache: Record<string, Record<string, Promise<Record<string, string>>>> = {};
const list = (params: { projectId: string }) => invoke<Session[]>('list_sessions', params);
export const listFiles = async (params: {
projectId: string;
sessionId: string;
paths?: string[];
}) => {
const sessionFilesCache = filesCache[params.projectId] || {};
if (params.sessionId in sessionFilesCache) {
return sessionFilesCache[params.sessionId].then((files) => {
return Object.fromEntries(
Object.entries(files).filter(([path]) =>
params.paths ? params.paths.includes(path) : true
)
);
});
}
const promise = invoke<Record<string, string>>('list_session_files', {
sessionId: params.sessionId,
projectId: params.projectId
});
sessionFilesCache[params.sessionId] = promise;
filesCache[params.projectId] = sessionFilesCache;
return promise.then((files) => {
return Object.fromEntries(
Object.entries(files).filter(([path]) => (params.paths ? params.paths.includes(path) : true))
);
});
};
const sessionsCache: Record<string, Promise<Session[]>> = {};
const list = async (params: { projectId: string; earliestTimestampMs?: number }) => {
if (params.projectId in sessionsCache) {
return sessionsCache[params.projectId].then((sessions) =>
sessions.filter((s) =>
params.earliestTimestampMs ? s.meta.startTimestampMs >= params.earliestTimestampMs : true
)
);
}
sessionsCache[params.projectId] = invoke<Session[]>('list_sessions', {
projectId: params.projectId
});
return sessionsCache[params.projectId].then((sessions) =>
sessions.filter((s) =>
params.earliestTimestampMs ? s.meta.startTimestampMs >= params.earliestTimestampMs : true
)
);
};
export default async (params: { projectId: string; earliestTimestampMs?: number }) => {
const store = writable([] as Session[]);