Assessor_2.0/src/compliance/ui/auto-mapping-engine-link.ts

22 lines
1020 B
TypeScript

import { AssessorApiClient } from '../../api/assessor-api-client';
import { readEnginePreferences, subscribeEnginePreferences } from '../../app/engine-preferences';
let unlink: (() => void) | undefined;
/**
* Switching the Compliance Engine off also switches off the server-side automatic ISO mapping, in whichever
* view the Engines popup is used. Switching it on again leaves automatic mapping off until the Data Hub
* switch is used. Batches that already started are not cancelled.
*/
export function linkAutoMappingToEngineSwitch(api: Pick<AssessorApiClient, 'setIsoAutoMapping'> = new AssessorApiClient()): () => void {
unlink?.();
let active = readEnginePreferences().compliance;
const unsubscribe = subscribeEnginePreferences(() => {
const next = readEnginePreferences().compliance;
if (active && !next) void Promise.resolve().then(() => api.setIsoAutoMapping(false)).catch(() => undefined);
active = next;
});
unlink = () => { unsubscribe(); unlink = undefined; };
return unlink;
}