Skip to content

Commit 9b41f7b

Browse files
fix: group spectra and wait for promises
1 parent 340692e commit 9b41f7b

File tree

2 files changed

+44
-54
lines changed

2 files changed

+44
-54
lines changed

src/NMRiumWrapper.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ export default function NMRiumWrapper() {
129129
urls: [
130130
'https://cheminfo.github.io/nmr-dataset-demo/cytisine/13c.jdx',
131131
'https://cheminfo.github.io/nmr-dataset-demo/cytisine/1h.jdx',
132+
'https://cheminfo.github.io/bruker-data-test/data/zipped/aspirin-1h.zip',
132133
],
133134
});
134135
}}

src/hooks/useLoadSpectraFromURL.ts

Lines changed: 43 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
/* eslint-disable @typescript-eslint/no-explicit-any */
2-
/* eslint-disable no-restricted-syntax */
3-
/* eslint-disable no-await-in-loop */
41
import {
52
FILES_TYPES,
63
getFileExtension,
74
} from 'nmrium/lib/component/utility/FileUtility';
85
import { addBruker, addJcamp, addJDF } from 'nmrium/lib/data/SpectraManager';
9-
import Zip from 'jszip';
106
import { useCallback, useMemo, useState } from 'react';
7+
import { Datum1D } from 'nmrium/lib/data/types/data1d';
8+
import { Datum2D } from 'nmrium/lib/data/types/data2d';
119
import events from '../events';
1210
import observableEvents from '../observables';
1311

12+
type Spectrum = Datum1D | Datum2D;
13+
1414
export function loadFromURLs(
1515
urls: string[],
1616
): Promise<Array<{ value: ArrayBuffer; extension: string }>> {
@@ -25,79 +25,68 @@ export function loadFromURLs(
2525
return Promise.all(fetches);
2626
}
2727

28+
function castToPromise(func, file, usedColors) {
29+
return new Promise((resolve) => {
30+
const spectra = [];
31+
func(spectra, file, {}, usedColors);
32+
resolve(spectra);
33+
});
34+
}
35+
36+
function flatArray(spectra: Array<Spectrum[]>): Spectrum[] {
37+
if (!spectra || spectra.length === 0) {
38+
return [];
39+
}
40+
41+
return spectra.reduce((acc, data) => {
42+
if (data) {
43+
return acc.concat(data as Spectrum[]);
44+
}
45+
return acc;
46+
}, []);
47+
}
48+
2849
export function useLoadSpectraFromURL() {
29-
const [data, setData] = useState<any>({});
50+
const [data, setData] = useState<Spectrum[]>([]);
3051
const [isLoading, setLoading] = useState<boolean>(false);
3152

3253
const load = useCallback(async (urls: string[]) => {
3354
setLoading(true);
3455
const loadedFiles = await loadFromURLs(urls);
35-
const uniqueFileExtensions = {};
3656

37-
for (const { value, extension } of loadedFiles) {
38-
if (uniqueFileExtensions[extension]) {
39-
uniqueFileExtensions[extension].push(value);
40-
} else {
41-
uniqueFileExtensions[extension] = [value];
42-
}
43-
}
44-
45-
for (const extension of Object.keys(uniqueFileExtensions)) {
46-
const spectra: any[] = [];
47-
const usedColors = { '1d': [], '2d': [] };
57+
const usedColors = { '1d': [], '2d': [] };
4858

49-
const files = uniqueFileExtensions[extension];
59+
try {
60+
const callPromises = loadedFiles.map(({ value, extension }) => {
61+
const file = value;
5062

51-
try {
5263
switch (extension) {
5364
case FILES_TYPES.JDX:
5465
case FILES_TYPES.DX: {
55-
for (const jcamp of files) {
56-
addJcamp(spectra, jcamp, {}, usedColors);
57-
}
58-
59-
break;
66+
return castToPromise(addJcamp, file, usedColors);
6067
}
6168
case FILES_TYPES.JDF: {
62-
for (const jdf of files) {
63-
addJDF(spectra, jdf, {}, usedColors);
64-
}
65-
66-
break;
69+
return castToPromise(addJDF, file, usedColors);
6770
}
6871
case FILES_TYPES.ZIP: {
69-
for (const zipFile of files) {
70-
const unzipResult = await Zip.loadAsync(zipFile);
71-
72-
const hasBruker = Object.keys(unzipResult.files).some((path) => {
73-
return ['2rr', 'fid', '1r'].some((brukerFile) =>
74-
path.endsWith(brukerFile),
75-
);
76-
});
77-
78-
if (hasBruker) {
79-
const brukerSpectra = await addBruker({}, zipFile, usedColors);
80-
spectra.concat(brukerSpectra);
81-
} else {
82-
throw new Error('The file is not a valid bruker file');
83-
}
84-
}
85-
86-
break;
72+
return addBruker({}, file, usedColors);
8773
}
8874

8975
default:
9076
throw new Error('The file extension must be zip, dx, jdx, jdf');
9177
}
92-
} catch (error: any) {
93-
events.trigger('error', error);
94-
observableEvents.trigger('error', error);
95-
// eslint-disable-next-line no-console
96-
console.log(error.message);
97-
}
78+
});
79+
const spectra = (await Promise.all(callPromises)) as Spectrum[][];
80+
81+
const result = flatArray(spectra);
9882

99-
setData(spectra);
83+
setData(result);
10084
setLoading(false);
85+
} catch (error: any) {
86+
events.trigger('error', error);
87+
observableEvents.trigger('error', error);
88+
// eslint-disable-next-line no-console
89+
console.log(error);
10190
}
10291
}, []);
10392

0 commit comments

Comments
 (0)