Skip to content

Commit

Permalink
Add fileSourcesStore
Browse files Browse the repository at this point in the history
To avoid multiple similar queries to the backend since fileSources are rather static.
  • Loading branch information
davelopez committed May 15, 2024
1 parent f432ddf commit 4c5f059
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions client/src/stores/fileSourcesStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { defineStore } from "pinia";
import { ref } from "vue";

import { type BrowsableFilesSourcePlugin, fetchFileSources, type FilterFileSourcesOptions } from "@/api/remoteFiles";

export const useFileSourcesStore = defineStore("fileSourcesStore", () => {
const cachedFileSources = ref<{
[key: string]: BrowsableFilesSourcePlugin[] | Promise<BrowsableFilesSourcePlugin[]>;
}>({});

async function getFileSources(options: FilterFileSourcesOptions = {}): Promise<BrowsableFilesSourcePlugin[]> {
const cacheKey = getCacheKey(options);
if (cachedFileSources.value[cacheKey] === undefined) {
cachedFileSources.value[cacheKey] = fetchFileSources(options);
}
if (cachedFileSources.value[cacheKey] instanceof Promise) {
cachedFileSources.value[cacheKey] = await cachedFileSources.value[cacheKey]!;
}
return cachedFileSources.value[cacheKey]!;
}

function getCacheKey(options: FilterFileSourcesOptions) {
return JSON.stringify(options);
}

return {
getFileSources,
};
});

0 comments on commit 4c5f059

Please sign in to comment.