Skip to content

Commit

Permalink
chore: pass logger to filter for time consuming information
Browse files Browse the repository at this point in the history
  • Loading branch information
jobo322 committed Nov 23, 2024
1 parent 7f6b32d commit 176128e
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 45 deletions.
5 changes: 5 additions & 0 deletions src/component/EventsTrackers/KeysListenerTracker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { useAssignmentData } from '../assignment/AssignmentsContext.js';
import { useChartData } from '../context/ChartContext.js';
import { useDispatch } from '../context/DispatchContext.js';
import { useLoader } from '../context/LoaderContext.js';
import { useLogger } from '../context/LoggerContext.js';
import { usePreferences } from '../context/PreferencesContext.js';
import { useToaster } from '../context/ToasterContext.js';
import type { AlertButton } from '../elements/Alert.js';
Expand Down Expand Up @@ -83,6 +84,7 @@ function KeysListenerTracker(props: KeysListenerTrackerProps) {
return displayerMode === '1D' && data && data.length > 0;
}, [data, displayerMode]);

const { logger } = useLogger();
const deleteHandler = useCallback(
async (sourceData) => {
const { type, extra } = sourceData;
Expand Down Expand Up @@ -176,6 +178,7 @@ function KeysListenerTracker(props: KeysListenerTrackerProps) {
type: 'DELETE_EXCLUSION_ZONE',
payload: {
zone,
logger,
},
});
hideLoading();
Expand All @@ -194,6 +197,7 @@ function KeysListenerTracker(props: KeysListenerTrackerProps) {
payload: {
zone,
spectrumId: spectrumID,
logger,
},
});
hideLoading();
Expand Down Expand Up @@ -288,6 +292,7 @@ function KeysListenerTracker(props: KeysListenerTrackerProps) {
toaster,
dispatchPreferences,
activeTab,
logger,
],
);

Expand Down
1 change: 1 addition & 0 deletions src/component/loader/useLoadFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export function useLoadFiles(onOpenMetaInformation?: (file: File) => void) {
containsNmrium,
parseMetaFileResult,
spectraColors,
logger,
},
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/component/main/NMRiumStateProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export default function NMRiumStateProvider(props: NMRiumStateProviderProps) {
},
});
}
dispatch({ type: 'INITIATE', payload: { nmriumState } });
dispatch({ type: 'INITIATE', payload: { nmriumState, logger } });
})
.catch((error: unknown) => {
dispatch({ type: 'SET_LOADING_FLAG', payload: { isLoading: false } });
Expand All @@ -129,7 +129,7 @@ export default function NMRiumStateProvider(props: NMRiumStateProviderProps) {
reportError(error);
});
}
}, [nmriumData, dispatch, dispatchPreferences]);
}, [nmriumData, dispatch, dispatchPreferences, logger]);
const { sortOptions } = useSortSpectra();

const spectra = useMemo(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type { MatrixFilter } from '../../../data/matrixGeneration.js';
import { getMatrixFilters } from '../../../data/matrixGeneration.js';
import { useChartData } from '../../context/ChartContext.js';
import { useDispatch } from '../../context/DispatchContext.js';
import { useLogger } from '../../context/LoggerContext.js';
import { usePreferences } from '../../context/PreferencesContext.js';
import type { GroupPaneStyle } from '../../elements/GroupPane.js';
import { GroupPane } from '../../elements/GroupPane.js';
Expand Down Expand Up @@ -124,6 +125,7 @@ function InnerMatrixGenerationPanel() {
activeTab,
);

const { logger } = useLogger();
const matrixOptions = getMatrixOptions(nucleusMatrixOptions.matrixOptions, {
from: originDomain.xDomain[0],
to: originDomain.xDomain[1],
Expand Down Expand Up @@ -152,7 +154,7 @@ function InnerMatrixGenerationPanel() {
function handleRemoveProcessing() {
dispatch({
type: 'DELETE_SPECTRA_FILTER',
payload: { filterName: signalProcessing.name },
payload: { filterName: signalProcessing.name, logger },
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { getFilterLabel } from '../../../../data/getFilterLabel.js';
import type { FilterEntry as BaseFilterEntry } from '../../../../data/types/common/FilterEntry.js';
import { useChartData } from '../../../context/ChartContext.js';
import { useDispatch } from '../../../context/DispatchContext.js';
import { useLogger } from '../../../context/LoggerContext.js';
import { useToaster } from '../../../context/ToasterContext.js';
import type { AlertButton } from '../../../elements/Alert.js';
import { useAlert } from '../../../elements/Alert.js';
Expand Down Expand Up @@ -62,14 +63,14 @@ function FilterElements(props: FilterElementsProps) {
} = props;
const { id, name, enabled } = filter;
const label = getFilterLabel(name);

const { logger } = useLogger();
function handleFilterCheck(id, event: React.ChangeEvent<HTMLInputElement>) {
const enabled = event.target.checked;
const hideLoading = toaster.showLoading({
message: `${enabled ? 'Enable' : 'Disable'} filter in progress`,
});
setTimeout(() => {
dispatch({ type: 'ENABLE_FILTER', payload: { id, enabled } });
dispatch({ type: 'ENABLE_FILTER', payload: { id, enabled, logger } });
hideLoading();
}, 0);
onEnableChange();
Expand All @@ -83,7 +84,10 @@ function FilterElements(props: FilterElementsProps) {
const hideLoading = await toaster.showAsyncLoading({
message: 'Delete filter process in progress',
});
dispatch({ type: 'DELETE_FILTER', payload: { id } });
dispatch({
type: 'DELETE_FILTER',
payload: { id, logger },
});
hideLoading();
},
intent: 'danger',
Expand All @@ -100,7 +104,7 @@ function FilterElements(props: FilterElementsProps) {
});
dispatch({
type: 'DELETE_SPECTRA_FILTER',
payload: { filterName: name },
payload: { filterName: name, logger },
});
hideLoading();
},
Expand Down
81 changes: 49 additions & 32 deletions src/component/reducer/actions/FiltersActions.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { v4 } from '@lukeed/uuid';
import type { NmrData1D, NmrData2DFt } from 'cheminfo-types';
import type { Logger, NmrData1D, NmrData2DFt } from 'cheminfo-types';
import type { Draft } from 'immer';
import { current } from 'immer';
import { xFindClosestIndex } from 'ml-spectra-processing';
import type { ActiveSpectrum, Spectrum, Spectrum1D } from 'nmr-load-save';
import type {
ActiveSpectrum,
Spectrum,
Spectrum1D,
Spectrum2D,
} from 'nmr-load-save';
import {
getBaselineZonesByDietrich,
Filters1DManager,
Expand Down Expand Up @@ -128,12 +133,15 @@ type BaselineCorrectionFilterLiveAction = ActionType<
>;
type EnableFilterAction = ActionType<
'ENABLE_FILTER',
{ id: string; enabled: boolean }
{ id: string; enabled: boolean; logger?: Logger }
>;
type DeleteFilterAction = ActionType<
'DELETE_FILTER',
{ id?: string; logger?: Logger }
>;
type DeleteFilterAction = ActionType<'DELETE_FILTER', { id?: string }>;
type DeleteSpectraFilterAction = ActionType<
'DELETE_SPECTRA_FILTER',
{ filterName: string }
{ filterName: string; logger?: Logger }
>;
type SetFilterSnapshotAction = ActionType<
'SET_FILTER_SNAPSHOT',
Expand All @@ -149,7 +157,7 @@ type AddExclusionZoneAction = ActionType<
>;
type DeleteExclusionZoneAction = ActionType<
'DELETE_EXCLUSION_ZONE',
{ zone: ExclusionZone; spectrumId?: string }
{ zone: ExclusionZone; spectrumId?: string; logger?: Logger }
>;
type ApplySignalProcessingAction = ActionType<
'APPLY_SIGNAL_PROCESSING_FILTER',
Expand Down Expand Up @@ -234,6 +242,7 @@ export interface RollbackSpectrumByFilterOptions {
key?: string | null;
activeSpectrum?: ActiveSpectrum | null;
triggerSource?: 'Apply' | 'none';
logger?: Logger;
}

function getFilterDomain(
Expand Down Expand Up @@ -272,6 +281,7 @@ function rollbackSpectrumByFilter(
reset = false,
key,
activeSpectrum,
logger,
triggerSource = 'none',
} = options || {};

Expand Down Expand Up @@ -306,19 +316,28 @@ function rollbackSpectrumByFilter(
});

if (isSpectrum1D(datum)) {
Filters1DManager.reapplyFilters(datum, filters);
Filters1DManager.reapplyFilters(datum, { filters, logger });

Check failure on line 319 in src/component/reducer/actions/FiltersActions.ts

View workflow job for this annotation

GitHub Actions / nodejs / lint-check-types

Object literal may only specify known properties, but 'filters' does not exist in type 'Filter1DOptions[]'. Did you mean to write 'filter'?
} else {
Filters2DManager.reapplyFilters(datum, filters);
Filters2DManager.reapplyFilters(datum, { filters, logger });

Check failure on line 321 in src/component/reducer/actions/FiltersActions.ts

View workflow job for this annotation

GitHub Actions / nodejs / lint-check-types

Object literal may only specify known properties, but 'filters' does not exist in type 'Filter2DOptions[]'. Did you mean to write 'filter'?
}

draft.tempData = current(draft).data;
// apply the current Filters
if (applyFilter) {
const { name, value } = datum.filters[filterIndex];
if (datum.info.dimension === 1) {
Filters1D[name].apply(datum, value);
const datum1D = datum as Spectrum1D;
Filters1DManager.applyFilter(

Check failure on line 329 in src/component/reducer/actions/FiltersActions.ts

View workflow job for this annotation

GitHub Actions / nodejs / lint-check-types

Property 'applyFilter' does not exist on type 'typeof Filters1DManager'. Did you mean 'applyFilters'?
datum1D,
datum1D.filters[filterIndex],
logger,
);
} else {
Filters2D[name].apply(datum, value);
const datum2D = datum as Spectrum2D;
Filters2DManager.applyFilter(

Check failure on line 336 in src/component/reducer/actions/FiltersActions.ts

View workflow job for this annotation

GitHub Actions / nodejs / lint-check-types

Property 'applyFilter' does not exist on type 'typeof Filters2DManager'. Did you mean 'applyFilters'?
datum2D,
datum2D.filters[filterIndex],
logger,
);
}
}

Expand All @@ -337,9 +356,9 @@ function rollbackSpectrumByFilter(
if (filterIndex === -1 || reset) {
if (draft.tempData) {
if (isSpectrum1D(datum)) {
Filters1DManager.reapplyFilters(datum);
Filters1DManager.reapplyFilters(datum, { logger });

Check failure on line 359 in src/component/reducer/actions/FiltersActions.ts

View workflow job for this annotation

GitHub Actions / nodejs / lint-check-types

Object literal may only specify known properties, and 'logger' does not exist in type 'Filter1DOptions[]'.
} else {
Filters2DManager.reapplyFilters(datum);
Filters2DManager.reapplyFilters(datum, { logger });

Check failure on line 361 in src/component/reducer/actions/FiltersActions.ts

View workflow job for this annotation

GitHub Actions / nodejs / lint-check-types

Object literal may only specify known properties, and 'logger' does not exist in type 'Filter2DOptions[]'.
}
}
//if the filter is not exists, create a clone of the current data
Expand Down Expand Up @@ -1153,15 +1172,15 @@ function handleEnableFilter(draft: Draft<State>, action: EnableFilterAction) {
return;
}

const { id: filterID, enabled } = action.payload;
const { id: filterID, enabled, logger } = action.payload;
const datum = draft.data[activeSpectrum.index];

//apply filter into the spectrum
if (isSpectrum1D(datum)) {
Filters1DManager.enableFilter(datum, filterID, enabled);
Filters1DManager.enableFilter(datum, { id: filterID, enabled, logger });

Check failure on line 1180 in src/component/reducer/actions/FiltersActions.ts

View workflow job for this annotation

GitHub Actions / nodejs / lint-check-types

Argument of type '{ id: string; enabled: boolean; logger: Logger | undefined; }' is not assignable to parameter of type 'string'.
}
if (isSpectrum2D(datum)) {
Filters2DManager.enableFilter(datum, filterID, enabled);
Filters2DManager.enableFilter(datum, { id: filterID, enabled, logger });

Check failure on line 1183 in src/component/reducer/actions/FiltersActions.ts

View workflow job for this annotation

GitHub Actions / nodejs / lint-check-types

Argument of type '{ id: string; enabled: boolean; logger: Logger | undefined; }' is not assignable to parameter of type 'string'.
}

resetSelectedTool(draft);
Expand All @@ -1180,28 +1199,26 @@ function handleEnableFilter(draft: Draft<State>, action: EnableFilterAction) {
}
}

function deleteFilter(datum: Spectrum, id?: string) {
function deleteFilter(datum: Spectrum, id?: string, logger?: Logger) {
const filters = datum.filters.slice(0);

let removedFilter;
if (!id) {
datum.filters = filters.filter((filter) =>

Check failure on line 1207 in src/component/reducer/actions/FiltersActions.ts

View workflow job for this annotation

GitHub Actions / nodejs / lint-check-types

Type '(Filter1DEntry | Filter2DEntry)[]' is not assignable to type 'Filter1DEntry[] | Filter2DEntry[]'.
nonRemovableFilters.has(filter.name),
) as typeof filters;
);
} else {
removedFilter = datum.filters.find((filter) => filter.id === id);
datum.filters = filters.filter(
(filter) => filter.id !== id,
) as typeof filters;
datum.filters = filters.filter((filter) => filter.id !== id);
}

// do not reprocess the filters when the deleted filter is inactive
if (removedFilter && !removedFilter.enabled) return;

if (isSpectrum1D(datum)) {
Filters1DManager.reapplyFilters(datum);
Filters1DManager.reapplyFilters(datum, { logger });
} else {
Filters2DManager.reapplyFilters(datum);
Filters2DManager.reapplyFilters(datum, { logger });
}
}

Expand All @@ -1213,9 +1230,9 @@ function handleDeleteFilter(draft: Draft<State>, action: DeleteFilterAction) {
return;
}

const filterID = action?.payload?.id;
const { id, logger } = action?.payload || {};
const datum = draft.data[activeSpectrum.index];
deleteFilter(datum, filterID);
deleteFilter(datum, id, logger);

draft.toolOptions.data.activeFilterID = null;
resetSelectedTool(draft);
Expand All @@ -1228,7 +1245,7 @@ function handleDeleteSpectraFilter(
draft: Draft<State>,
action: DeleteSpectraFilterAction,
) {
const filterName = action.payload.filterName;
const { filterName, logger } = action.payload;

if (draft.view.spectra.activeTab) {
for (const datum of draft.data) {
Expand All @@ -1240,11 +1257,11 @@ function handleDeleteSpectraFilter(

for (const filter of filtersResult) {
if (isSpectrum1D(datum)) {
Filters1DManager.deleteFilter(datum, filter.id);
Filters1DManager.deleteFilter(datum, filter.id, logger);
}

if (isSpectrum2D(datum)) {
Filters2DManager.deleteFilter(datum, filter.id);
Filters2DManager.deleteFilter(datum, filter.id, logger);
}
}
}
Expand Down Expand Up @@ -1379,7 +1396,7 @@ function handleDeleteExclusionZone(
draft: Draft<State>,
action: DeleteExclusionZoneAction,
) {
const { zone, spectrumId } = action.payload;
const { zone, spectrumId, logger } = action.payload;

// if spectrum id exists, remove the selected exclusion zone in the spectrum
if (spectrumId) {
Expand All @@ -1392,10 +1409,10 @@ function handleDeleteExclusionZone(
);
if (filter && isSpectrum1D(spectrum)) {
if (filter.value.length === 1) {
Filters1DManager.deleteFilter(spectrum, filter.id);
Filters1DManager.deleteFilter(spectrum, filter.id, logger);
} else {
filter.value = filter.value.filter((_zone) => _zone.id !== zone?.id);
Filters1DManager.reapplyFilters(spectrum);
Filters1DManager.reapplyFilters(spectrum, { logger });
}
}
} else {
Expand All @@ -1407,7 +1424,7 @@ function handleDeleteExclusionZone(
filter.value = filter.value.filter(
(_zone) => zone.from !== _zone.from && zone.to !== _zone.to,
);
Filters1DManager.reapplyFilters(datum);
Filters1DManager.reapplyFilters(datum, { logger });
}
}
}
Expand Down
Loading

0 comments on commit 176128e

Please sign in to comment.