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

feat: load ium state #267

Merged
merged 7 commits into from
Nov 4, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/app/context/appState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export interface AppState {
};
}

function getEmptyAppState(): AppState {
export function getEmptyAppState(): AppState {
return {
data: getEmptyDataState(),
isLoading: false,
Expand Down Expand Up @@ -86,6 +86,7 @@ type AppStateAction =
| { type: 'LOAD_START' }
| { type: 'LOAD_END' }
| { type: 'ADD_MEASUREMENTS'; payload: Measurements }
| { type: 'LOAD_STATE'; payload: AppState }
| {
type: 'SELECT_MEASUREMENT';
payload: { id: string; kind: MeasurementKind };
Expand Down Expand Up @@ -163,6 +164,12 @@ function actionHandler(draft: Draft<AppState>, action: AppStateAction) {
draft.isLoading = false;
return;
}
case 'LOAD_STATE': {
const { data, view } = action.payload;
draft.data = data;
draft.view = view;
return;
}

default:
assertUnreachable(type);
Expand Down
28 changes: 21 additions & 7 deletions src/app/context/load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { cary500Loader } from '../data/loaders/proprietary/agilent/cary500Loader
import { spcLoader } from '../data/loaders/spcLoader';
import { wdfLoader } from '../data/loaders/wdfLoader';

import type { AppDispatch } from './appState';
import type { AppDispatch, AppState } from './appState';

const options = {
loaders: [
Expand All @@ -28,18 +28,32 @@ const options = {
],
},
};

export async function loadFiles(
files: File[] | FileCollection,
dispatch: AppDispatch,
) {
dispatch({ type: 'LOAD_START' });
try {
const fileCollection =
files instanceof FileCollection
? files
: await fileCollectionFromFiles(files);
const measurements = await loadMeasurements(fileCollection, options);
dispatch({ type: 'ADD_MEASUREMENTS', payload: measurements });
if (files[0] && !files[1] && files[0].name.match(/\.ium$/i)) {
const data = await files[0].text();
const appState = JSON.parse(data);
const payload: AppState = {
...appState,
isLoading: true,
wadjih-bencheikh18 marked this conversation as resolved.
Show resolved Hide resolved
};
dispatch({
type: 'LOAD_STATE',
payload,
});
} else {
const fileCollection =
files instanceof FileCollection
? files
: await fileCollectionFromFiles(files);
const data = await loadMeasurements(fileCollection, options);
dispatch({ type: 'ADD_MEASUREMENTS', payload: data });
}
} finally {
dispatch({ type: 'LOAD_END' });
}
Expand Down
6 changes: 3 additions & 3 deletions src/app/data/append.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ export async function loadMeasurements(
const measurements: Measurements = getEmptyMeasurements();
const { loaders = [], enhancers = {} } = options;
for (const loader of loaders) {
const loaderMeasurements = await loader(fileCollection);
enhance(loaderMeasurements, enhancers);
mergeMeasurements(measurements, loaderMeasurements);
const loaderData = await loader(fileCollection);
enhance(loaderData, enhancers);
mergeMeasurements(measurements, loaderData);
}
return measurements;
}