Skip to content

Commit

Permalink
Cache child entities in fetch store instead of custom cache
Browse files Browse the repository at this point in the history
  • Loading branch information
axelboc committed Jun 6, 2024
1 parent 2bf36fa commit 5fc9639
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 39 deletions.
11 changes: 2 additions & 9 deletions packages/app/src/providers/DataProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isGroup } from '@h5web/shared/guards';
import type { ChildEntity, Entity, Group } from '@h5web/shared/hdf5-models';
import type { Entity } from '@h5web/shared/hdf5-models';
import { getNameFromPath } from '@h5web/shared/hdf5-utils';
import { createFetchStore } from '@h5web/shared/react-suspense-fetch';
import type { PropsWithChildren } from 'react';
Expand Down Expand Up @@ -43,21 +43,14 @@ function DataProvider(props: PropsWithChildren<Props>) {
const { api, children } = props;

const entitiesStore = useMemo(() => {
const childCache = new Map<string, Exclude<ChildEntity, Group>>();

const store = createFetchStore(async (path: string) => {
const cachedEntity = childCache.get(path);
if (cachedEntity) {
return cachedEntity;
}

const entity = await api.getEntity(path);

if (isGroup(entity)) {
// Cache non-group children (datasets, datatypes and links)
entity.children.forEach((child) => {
if (!isGroup(child)) {
childCache.set(child.path, child);
store.preset(child.path, child);
}
});
}
Expand Down
33 changes: 17 additions & 16 deletions packages/shared/src/react-suspense-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@ interface Instance<Result> {
export interface FetchStore<Input, Result> {
prefetch: (input: Input) => void;
get: (input: Input) => Result;
preset: (input: Input, result: Result) => void;
evict: (input: Input) => void;
abort: (input: Input) => void;
}

export function createFetchStore<Input, Result>(
fetchFunc: FetchFunc<Input, Result>,
areEqual?: AreEqual<Input>,
areEqual: AreEqual<Input> = Object.is,
) {
const cache = createCache<Input, Result>(areEqual);
const cache = createCache<Input, Instance<Result>>(areEqual);

return {
prefetch: (input: Input): void => {
Expand All @@ -34,6 +35,14 @@ export function createFetchStore<Input, Result>(
cache.set(input, instance);
return instance.get();
},
preset: (input: Input, result: Result): void => {
cache.set(input, {
get: () => result,
abort: () => {
/* noop */
},
});
},
evict: (input: Input): void => {
cache.delete(input);
},
Expand All @@ -43,15 +52,7 @@ export function createFetchStore<Input, Result>(
};
}

function createCache<Input, Result>(areEqual?: AreEqual<Input>) {
if (!areEqual) {
return new Map<Input, Instance<Result>>();
}

return createMapLikeWithComparator<Input, Instance<Result>>(areEqual);
}

function createMapLikeWithComparator<K, V>(areEqual: AreEqual<K>) {
function createCache<K, V>(areEqual: AreEqual<K>) {
const map = new Map<K, V>();

return {
Expand Down Expand Up @@ -88,9 +89,9 @@ function createInstance<Input, Result>(
input: Input,
fetchFunc: FetchFunc<Input, Result>,
): Instance<Result> {
let promise: Promise<void> | null = null;
let result: Result | null = null;
let error: unknown = null;
let promise: Promise<void> | undefined;
let result: Result | undefined;
let error: unknown;
const controller = new AbortController();

promise = (async () => {
Expand All @@ -99,7 +100,7 @@ function createInstance<Input, Result>(
} catch (error_) {
error = error_;
} finally {
promise = null;
promise = undefined;
}
})();

Expand All @@ -108,7 +109,7 @@ function createInstance<Input, Result>(
if (promise) {
throw promise; // eslint-disable-line @typescript-eslint/no-throw-literal
}
if (error !== null) {
if (error !== undefined) {
throw error; // eslint-disable-line @typescript-eslint/no-throw-literal
}
return result as Result;
Expand Down
28 changes: 14 additions & 14 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5fc9639

Please sign in to comment.