Skip to content

Commit

Permalink
feat: throttling to display jcamp from database
Browse files Browse the repository at this point in the history
close #1575
  • Loading branch information
hamed-musallam committed Jun 7, 2022
1 parent 13b4e19 commit 99c7c34
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/component/1d/database/DatabaseSpectrum.tsx
@@ -1,4 +1,5 @@
import { useCallback, useContext, useEffect, useState } from 'react';
import throttle from 'lodash/throttle';
import { useCallback, useContext, useEffect, useRef, useState } from 'react';

import { addJcamp } from '../../../data/SpectraManager';
import { Datum1D } from '../../../data/types/data1d';
Expand Down Expand Up @@ -35,11 +36,10 @@ function DatabaseSpectrum() {
[height, margin, marginBottom, verticalAlign, yDomain],
);

useEffect(() => {
void (async () => {
const loadSpectrum = useRef(
throttle(async (baseURL: string, jcampRelativeURL: string) => {
try {
setLoading(true);

const jcampURL = new URL(jcampRelativeURL, baseURL);
const result = await loadFile(jcampURL);
const spectra = [];
Expand All @@ -58,8 +58,12 @@ function DatabaseSpectrum() {
} catch (e) {
alert.error('Failed to Load spectrum');
}
})();
}, [alert, baseURL, jcampRelativeURL, scaleX, scaleY]);
}, 250),
);

useEffect(() => {
void loadSpectrum.current(baseURL, jcampRelativeURL);
}, [baseURL, jcampRelativeURL, loadSpectrum]);

if (highlight.sourceData?.type !== HighlightedSource.DATABASE) {
return null;
Expand Down

0 comments on commit 99c7c34

Please sign in to comment.