mobx.dart icon indicating copy to clipboard operation
mobx.dart copied to clipboard

`when` optional disposal

Open subzero911 opened this issue 2 years ago • 2 comments

Currently when and asyncWhen always dispose itself after it fires. But sometimes that's not what I need. I may want to fire the when reaction multiple times. Please add a parameter autodispose: true/false, to control this behaviour. It could be optional and true by default, to not ruin the current applications.

subzero911 avatar Dec 09 '21 14:12 subzero911

Just use reaction then? https://mobx.netlify.app/api/reaction/#reaction

Or write custom wrapper around it, probably just couple lines of code

O4epegb avatar Dec 30 '21 11:12 O4epegb

You could do something like this.

import 'package:mobx/mobx.dart';

ReactionDisposer reactionWhere(
  bool Function(Reaction) where,
  void Function() effect, {
  String? name,
  int? delay,
  bool? fireImmediately,
  ReactiveContext? context,
  void Function(Object, Reaction)? onError,
}) {
  return reaction<bool>(
    (rxn) => where(rxn),
    (value) {
      if (value) effect();
    },
    name: name,
    delay: delay,
    fireImmediately: fireImmediately,
    context: context,
    onError: onError,
  );
}

RodolfoSilva avatar Nov 26 '23 10:11 RodolfoSilva