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; filePath: string;
}; };
export const list = (params: { projectId: string; sessionId: string; paths?: string[] }) => const cache: Record<string, Record<string, Promise<Record<string, Delta[]>>>> = {};
invoke<Record<string, Delta[]>>('list_deltas', params);
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 = ( export const subscribe = (
params: { projectId: string; sessionId: string }, params: { projectId: string; sessionId: string },

View File

@ -29,10 +29,56 @@ export type Session = {
activity: Activity[]; activity: Activity[];
}; };
export const listFiles = (params: { projectId: string; sessionId: string; paths?: string[] }) => const filesCache: Record<string, Record<string, Promise<Record<string, string>>>> = {};
invoke<Record<string, string>>('list_session_files', params);
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 }) => { export default async (params: { projectId: string; earliestTimestampMs?: number }) => {
const store = writable([] as Session[]); const store = writable([] as Session[]);