Skip to content

Commit

Permalink
Cache loadResource data
Browse files Browse the repository at this point in the history
  • Loading branch information
arildm committed Mar 14, 2024
1 parent 44db39b commit 72b8805
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 20 deletions.
19 changes: 14 additions & 5 deletions src/library/resources.composable.ts
Original file line number Diff line number Diff line change
@@ -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<string, true> = {};

/** Use this module-scope variable for the request, so that simultaneous calls don't procude multiple requests. */
let loadPromise: Promise<unknown> | null = null;

Expand All @@ -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<Resource | undefined> {
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. */
Expand Down
30 changes: 15 additions & 15 deletions src/store/resource.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@ 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;
status: CorpusStatus;
exports: FileMeta[];
};

type Metadata = Resource & {
export type Metadata = Resource & {
publicId: string;
metadata: string; // YAML
};
Expand All @@ -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("[email protected]", {});
const resources: Record<string, Partial<Resource>> = reactive(
resourcesRef.value,
);
const resources: Record<string, {} | Resource> = reactive(resourcesRef.value);

const corpora = computed<Record<string, Partial<Corpus>>>(() =>
filterResources("corpus"),
Expand All @@ -57,10 +55,13 @@ export const useResourceStore = defineStore("resource", () => {
type: ResourceType,
): Record<string, Partial<T>> =>
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;
},

{},
);

Expand All @@ -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;
Expand Down

0 comments on commit 72b8805

Please sign in to comment.