Adding an onCleanup hook to cancel dispatches and watches for Wpt

This commit is contained in:
Eric van der Vlist 2022-12-06 14:54:59 +01:00
parent 5829f93c91
commit cc121fea27
2 changed files with 22 additions and 6 deletions

View File

@ -3,9 +3,10 @@ import {
createEffect, createEffect,
createResource, createResource,
createSignal, createSignal,
onCleanup,
} from 'solid-js'; } from 'solid-js';
import dispatch from '../../workers/dispatcher-main'; import dispatch, { cancelDispatch } from '../../workers/dispatcher-main';
import VectorSource from 'ol/source/Vector'; import VectorSource from 'ol/source/Vector';
import GeoJSON from 'ol/format/GeoJSON'; import GeoJSON from 'ol/format/GeoJSON';
import { setBranch, tree } from '../map'; import { setBranch, tree } from '../map';
@ -18,14 +19,18 @@ interface Props {
export const Wpt: Component<Props> = ({ vectorSource, wptId: wptId }) => { export const Wpt: Component<Props> = ({ vectorSource, wptId: wptId }) => {
const [wpt, setWpt] = createSignal<Wpt>(); const [wpt, setWpt] = createSignal<Wpt>();
const [dispatchId, setDispatchId] = createSignal<number>(-1);
const wptCallBack = (error: any, result: any) => { const wptCallBack = (error: any, result: any, id?: number) => {
if (error) { if (error) {
console.error({ caller: 'wptCallBack', error }); console.error({ caller: 'wptCallBack', error });
} else { } else {
console.log({ caller: 'wptCallBack', result }); console.log({ caller: 'wptCallBack', result });
setWpt(result); setWpt(result);
} }
if (id) {
setDispatchId(id);
}
}; };
dispatch( dispatch(
@ -40,6 +45,17 @@ export const Wpt: Component<Props> = ({ vectorSource, wptId: wptId }) => {
true true
); );
onCleanup(() => {
dispatch({
action: 'cancelWatch',
params: {
id: wptId,
method: 'getWpt',
},
});
cancelDispatch(dispatchId());
});
createEffect(() => { createEffect(() => {
console.log({ caller: 'Wpt', vectorSource, wptId, wpt: wpt() }); console.log({ caller: 'Wpt', vectorSource, wptId, wpt: wpt() });

View File

@ -22,9 +22,9 @@ export const init = () => {
payload, payload,
dispatcherQueue: JSON.parse(JSON.stringify(dispatcherQueue)), dispatcherQueue: JSON.parse(JSON.stringify(dispatcherQueue)),
}); });
dispatcherQueue.queue.get(id).callBack(null, payload); dispatcherQueue.queue.get(id).callBack(null, payload, id);
if (!dispatcherQueue.queue.get(id).live) { if (!dispatcherQueue.queue.get(id).live) {
cancel(id); cancelDispatch(id);
} }
}; };
@ -33,7 +33,7 @@ export const init = () => {
const dispatch = ( const dispatch = (
payload: any, payload: any,
callBack?: (error: any, result: any) => void, callBack?: (error: any, result: any, id?: number|undefined) => void,
live?: boolean live?: boolean
) => { ) => {
console.log({ caller: 'dispatcher-main / dispatch', payload }); console.log({ caller: 'dispatcher-main / dispatch', payload });
@ -66,7 +66,7 @@ const dispatch = (
}); });
}; };
export const cancel = (id: number) => { export const cancelDispatch = (id: number) => {
dispatcherQueue.queue.delete(id); dispatcherQueue.queue.delete(id);
}; };