diff --git a/src/library/resources.composable.ts b/src/library/resources.composable.ts index b1619b2..15d3633 100644 --- a/src/library/resources.composable.ts +++ b/src/library/resources.composable.ts @@ -1,10 +1,13 @@ import useMinkBackend from "@/api/backend.composable"; -import { useResourceStore } from "@/store/resource.store"; +import { useResourceStore, type Resource } from "@/store/resource.store"; import useMessenger from "@/message/messenger.composable"; /** Let resource list be refreshed initially, but skip subsequent load calls. */ let isFresh = false; +/** List of freshly loaded resources. */ +const freshResources: Record = {}; + /** Use this module-scope variable for the request, so that simultaneous calls don't procude multiple requests. */ let loadPromise: Promise | null = null; @@ -14,10 +17,16 @@ export default function useResources() { const mink = useMinkBackend(); /** Load and store data about a given resource. */ - async function loadResource(resourceId: string) { - const data = await mink.resourceInfoOne(resourceId).catch(alertError); - if (!data) return; - return resourceStore.setResource(data); + async function loadResource( + resourceId: string, + ): Promise { + if (!freshResources[resourceId]) { + const data = await mink.resourceInfoOne(resourceId).catch(alertError); + if (!data) return; + resourceStore.setResource(data); + freshResources[resourceId] = true; + } + return resourceStore.resources[resourceId] as Resource; } /** Load and store data about all the user's resources, with caching. */ diff --git a/src/store/resource.store.ts b/src/store/resource.store.ts index 7f5e0a6..9f757b4 100644 --- a/src/store/resource.store.ts +++ b/src/store/resource.store.ts @@ -11,12 +11,12 @@ import type { } from "@/api/api.types"; import type { ConfigOptions } from "@/api/corpusConfig"; -type Resource = { +export type Resource = { type: ResourceType; name: ByLang; }; -type Corpus = Resource & { +export type Corpus = Resource & { type: "corpus"; sources: FileMeta[]; config: ConfigOptions; @@ -24,7 +24,7 @@ type Corpus = Resource & { exports: FileMeta[]; }; -type Metadata = Resource & { +export type Metadata = Resource & { publicId: string; metadata: string; // YAML }; @@ -42,9 +42,7 @@ export const useResourceStore = defineStore("resource", () => { // forget the old state. The actual number doesn't really matter, as long as // it's a new one. const resourcesRef = useStorage("mink@230208.resources", {}); - const resources: Record> = reactive( - resourcesRef.value, - ); + const resources: Record = reactive(resourcesRef.value); const corpora = computed>>(() => filterResources("corpus"), @@ -57,10 +55,13 @@ export const useResourceStore = defineStore("resource", () => { type: ResourceType, ): Record> => Object.keys(resources).reduce( - (filtered, resourceId) => - resources[resourceId].type == type - ? { ...filtered, [resourceId]: resources[resourceId] } - : filtered, + (filtered, resourceId) => { + const resource = resources[resourceId]; + return "type" in resource && resource.type == type + ? { ...filtered, [resourceId]: resource } + : filtered; + }, + {}, ); @@ -79,11 +80,10 @@ export const useResourceStore = defineStore("resource", () => { /** Store new state for a given resource. */ function setResource(info: ResourceInfo): Resource { - // Patch any existing record, otherwise create a new one. - const resource = - info.resource.id in resources ? resources[info.resource.id] : {}; - resource.type = info.resource.type; - resource.name = info.resource.name; + const resource = { + type: info.resource.type, + name: info.resource.name, + }; if (isCorpus(resource)) { resource.sources = info.resource.source_files;