autorun function

ReactionDisposer autorun(
  1. dynamic fn(
    1. Reaction
    ), {
  2. String? name,
  3. int? delay,
  4. ReactiveContext? context,
  5. Timer scheduler(
    1. void ()
    )?,
  6. void onError(
    1. Object,
    2. Reaction
    )?,
})

Executes the specified fn, whenever the dependent observables change. It returns a disposer that can be used to dispose the autorun.

Optional configuration:

  • name: debug name for this reaction
  • delay: Number of milliseconds that can be used to throttle the effect function. If zero (default), no throttling happens.
  • context: the ReactiveContext to use. By default the mainContext is used.
  • scheduler: Set a custom scheduler to determine how re-running the autorun function should be scheduled. It takes a function that should be invoked at some point in the future.
  • onError: By default, any exception thrown inside an reaction will be logged, but not further thrown. This is to make sure that an exception in one reaction does not prevent the scheduled execution of other, possibly unrelated reactions. This also allows reactions to recover from exceptions. Throwing an exception does not break the tracking done by MobX, so subsequent runs of the reaction might complete normally again if the cause for the exception is removed. This option allows overriding that behavior. It is possible to set a global error handler or to disable catching errors completely using ReactiveConfig.
var x = Observable(10);
var y = Observable(20);
var total = Observable(0);

var dispose = autorun((_){
  print('x = ${x}, y = ${y}, total = ${total}');
});

x.value = 20; // will cause autorun() to re-trigger.

dispose(); // This disposes the autorun() and will not be triggered again

x.value = 30; // Will not cause autorun() to re-trigger as it's disposed.

Implementation

ReactionDisposer autorun(Function(Reaction) fn,
        {String? name,
        int? delay,
        ReactiveContext? context,
        Timer Function(void Function())? scheduler,
        void Function(Object, Reaction)? onError}) =>
    createAutorun(context ?? mainContext, fn,
        name: name, delay: delay, scheduler: scheduler, onError: onError);