@jbrowse/core
Version:
JBrowse 2 core libraries used by plugins
48 lines (47 loc) • 1.45 kB
JavaScript
import { addDisposer, isAlive } from '@jbrowse/mobx-state-tree';
import { reaction } from 'mobx';
import { isAbortException } from "./aborting.js";
import { createStopToken, stopStopToken } from "./stopToken.js";
export function makeAbortableReaction(self, dataFunction, asyncReactionFunction, reactionOptions, startedFunction, successFunction, errorFunction) {
let inProgress;
function handleError(error) {
if (!isAbortException(error)) {
if (isAlive(self)) {
console.error(error);
errorFunction(error);
}
}
}
addDisposer(self, reaction(() => {
try {
return dataFunction(self);
}
catch (e) {
handleError(e);
return undefined;
}
}, async (data, mobxReactionHandle) => {
if (inProgress) {
stopStopToken(inProgress);
}
if (!isAlive(self)) {
return;
}
inProgress = createStopToken();
startedFunction(inProgress);
try {
const result = await asyncReactionFunction(data, inProgress, self, mobxReactionHandle);
if (isAlive(self)) {
successFunction(result);
}
}
catch (e) {
handleError(e);
}
}, reactionOptions));
addDisposer(self, () => {
if (inProgress) {
stopStopToken(inProgress);
}
});
}