mobxjs / mobx.dart

MobX for the Dart language. Hassle-free, reactive state-management for your Dart and Flutter apps.
https://mobx.netlify.app
MIT License
2.4k stars 310 forks source link

`when` optional disposal #716

Open subzero911 opened 2 years ago

subzero911 commented 2 years ago

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.

O4epegb commented 2 years ago

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

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

RodolfoSilva commented 11 months ago

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,
  );
}