Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate mock data lazily and improve API #1537

Merged
merged 2 commits into from
Jan 9, 2024
Merged

Generate mock data lazily and improve API #1537

merged 2 commits into from
Jan 9, 2024

Conversation

axelboc
Copy link
Contributor

@axelboc axelboc commented Jan 5, 2024

I realised that the mockValues and mockMetadata objects were not properly tree-shaken in consumer applications of @h5web/lib or @h5web/app (at least with ones using Vite as their bundler). As a result, those two giant data structures were being created on every page load of myHDF5, just to name one.

Tree-shaking works better (only?) with functions, so the idea is to turn mockValues and mockMetadata into functions. This has benefits even in projects that do need these two structures (e.g. in the demo project), since it means that we can generate them lazily, when they are actually needed (e.g. when navigating to the mock demo).

I had tried to generate the mock metadata and values lazily before, but never really got to a point that I found satisfactory. The API to access or generate entities and arrays that we use in our Jest test suites and in the Storybook was always quite convoluted.

This time, I had the idea of generating ndarrays instead of plain, nested JS arrays in mockValues, which unlocked a lot of things, thanks to the simple fact that ndarrays store the shape of their underlying arrays. For instance, this means that we:

  • no longer need to hard code the dimensions of our mock arrays everywhere (like [20, 41]...) when we generate the mock metadata;
  • no longer need to find a mock dataset's metadata (just so we can know its shape) in order to construct an ndarray from its value.

Additionally, since the internal array inside an ndarray is already flattened, we no longer need to deal with this in MockApi.


The PR is huge particularly because I took the opportunity to clean the metadata a bit and to shorten the metadata utility functions to make the whole thing a bit more readable.

@axelboc axelboc force-pushed the lazy-mock branch 2 times, most recently from d56fba6 to 7d128b3 Compare January 5, 2024 15:58
Copy link
Contributor Author

@axelboc axelboc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First round of comments, but I still need to make some changes before the review.

apps/storybook/src/HeatmapMesh.stories.tsx Show resolved Hide resolved
apps/storybook/src/LineVis.stories.tsx Show resolved Hide resolved
apps/storybook/src/MatrixVis.stories.tsx Show resolved Hide resolved
packages/app/src/providers/mock/mock-api.ts Show resolved Hide resolved
packages/app/src/providers/mock/mock-api.ts Show resolved Hide resolved
packages/shared/src/guards.ts Show resolved Hide resolved
@axelboc axelboc marked this pull request as draft January 5, 2024 16:00
@axelboc axelboc force-pushed the lazy-mock branch 2 times, most recently from 76b3e9a to 318f52b Compare January 8, 2024 08:57
packages/shared/src/mock/metadata-utils.ts Show resolved Hide resolved
packages/shared/src/mock/metadata-utils.ts Show resolved Hide resolved
packages/shared/src/mock/metadata-utils.ts Show resolved Hide resolved
packages/shared/src/mock/metadata-utils.ts Outdated Show resolved Hide resolved
packages/shared/src/mock/values.ts Show resolved Hide resolved
packages/shared/src/mock/values.ts Show resolved Hide resolved
packages/shared/src/mock/values.ts Show resolved Hide resolved
packages/shared/src/mock/values.ts Show resolved Hide resolved
packages/shared/src/models-hdf5.ts Show resolved Hide resolved
@axelboc axelboc marked this pull request as ready for review January 8, 2024 10:30
@axelboc
Copy link
Contributor Author

axelboc commented Jan 8, 2024

/approve

@axelboc axelboc requested a review from loichuder January 8, 2024 10:38
Copy link
Member

@loichuder loichuder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems all fine to me ! Well done !

apps/storybook/src/LineVis.stories.tsx Show resolved Hide resolved
apps/storybook/src/LineVisScales.stories.tsx Show resolved Hide resolved
Comment on lines 418 to 425
The library also exposes the `makeMockFile` utility function that is used by the [mock demo](https://h5web.panosc.eu/mock) to generate mock metadata and values.
You can retrieve entities from the generated mock file by their paths using the `findMockEntity` utility function:

```ts
import { findMockEntity, getMockDataArray } from '@h5web/lib';
import { findMockEntity, makeMockFile } from '@h5web/lib';

const entity = findMockEntity('/nD_datasets/twoD');
const dataArray = getMockDataArray('/nD_datasets/twoD');
const mockFile = makeMockFile();
const entity = findMockEntity(mockFile, '/nD_datasets/twoD');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, not sure it is actually relevant to document this.

packages/shared/src/models-hdf5.ts Show resolved Hide resolved
packages/shared/src/mock/metadata-utils.ts Outdated Show resolved Hide resolved
packages/shared/src/mock/metadata-utils.ts Show resolved Hide resolved
packages/shared/src/mock/metadata-utils.ts Show resolved Hide resolved
@axelboc axelboc merged commit 0f6a713 into main Jan 9, 2024
8 checks passed
@axelboc axelboc deleted the lazy-mock branch January 9, 2024 08:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants