From 4c5f0592d9a5572419bf221e5ff6bc6cbad5eaea Mon Sep 17 00:00:00 2001 From: davelopez <46503462+davelopez@users.noreply.github.com> Date: Tue, 14 May 2024 18:21:40 +0200 Subject: [PATCH] Add fileSourcesStore To avoid multiple similar queries to the backend since fileSources are rather static. --- client/src/stores/fileSourcesStore.ts | 29 +++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 client/src/stores/fileSourcesStore.ts diff --git a/client/src/stores/fileSourcesStore.ts b/client/src/stores/fileSourcesStore.ts new file mode 100644 index 000000000000..14ef72074eef --- /dev/null +++ b/client/src/stores/fileSourcesStore.ts @@ -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; + }>({}); + + async function getFileSources(options: FilterFileSourcesOptions = {}): Promise { + 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, + }; +});