-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
To avoid multiple similar queries to the backend since fileSources are rather static.
- Loading branch information
Showing
1 changed file
with
29 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
}); |