tools: fix disposableReaction
This commit is contained in:
parent
1aa4ae7752
commit
717bb0daf8
@ -1,9 +1,9 @@
|
||||
import type { IReactionDisposer, IReactionOptions, IReactionPublic } from 'mobx';
|
||||
import { autorun, reaction } from 'mobx';
|
||||
import { reaction } from 'mobx';
|
||||
import { debounce, omit } from 'radash';
|
||||
|
||||
export function disposableReaction<T, FireImmediately extends boolean = false>(
|
||||
mustBeDisposed: () => boolean,
|
||||
disposeExpression: () => boolean,
|
||||
expression: (r: IReactionPublic) => T,
|
||||
effect: (
|
||||
arg: T,
|
||||
@ -14,7 +14,7 @@ export function disposableReaction<T, FireImmediately extends boolean = false>(
|
||||
) {
|
||||
let disposer: IReactionDisposer | undefined;
|
||||
|
||||
if (!mustBeDisposed()) {
|
||||
if (!disposeExpression()) {
|
||||
disposer = reaction(expression, effect, reactionOpts);
|
||||
}
|
||||
|
||||
@ -22,13 +22,16 @@ export function disposableReaction<T, FireImmediately extends boolean = false>(
|
||||
disposer = undefined;
|
||||
}
|
||||
|
||||
autorun(() => {
|
||||
if (mustBeDisposed()) {
|
||||
reaction(disposeExpression, (mustBeDisposed) => {
|
||||
if (mustBeDisposed) {
|
||||
if (disposer !== undefined) disposer();
|
||||
cleanDisposer();
|
||||
} else {
|
||||
const opts = reactionOpts ? omit(reactionOpts, ['fireImmediately']) : undefined;
|
||||
disposer = reaction(expression, effect, opts);
|
||||
disposer = reaction(
|
||||
expression,
|
||||
effect,
|
||||
reactionOpts ? omit(reactionOpts, ['fireImmediately']) : undefined
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user