Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions docs/docs/api/reaction.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,19 @@ dispose();

## when

#### `ReactionDisposer when(bool Function(Reaction) predicate, void Function() effect)`
#### `ReactionDisposer when(bool Function(Reaction) predicate, void Function() effect, {String? name, ReactiveContext? context, int? timeout, void Function(Object, Reaction)? onError})`

Monitors the observables used inside `predicate()` and runs the `effect()`
_when_ it returns `true`. After the `effect()` is run, `when` automatically
disposes itself. So you can think of _when_ as a _one-time_ `reaction`. You can
also dispose `when()` pre-maturely.

**Optional parameters:**
- **`name`**: Debug name for this reaction
- **`context`**: The `ReactiveContext` to use. By default the `mainContext` is used.
- **`timeout`**: Number of milliseconds to wait for the predicate to become true. If the timeout elapses first, `when` disposes itself and throws a `MobXException`, unless `onError` is set.
- **`onError`**: By default, any exception thrown inside a reaction will be logged but not further thrown. This option allows overriding that behavior.

```dart
import 'package:mobx/mobx.dart';

Expand All @@ -121,12 +127,17 @@ greeting.value = 'Hello MobX'; // Causes a change, runs effect and disposes

## asyncWhen

#### `Future<void> asyncWhen(bool Function(Reaction) predicate)`
#### `Future<void> asyncWhen(bool Function(Reaction) predicate, {String? name, int? timeout, ReactiveContext? context})`

Similar to `when` but returns a `Future`, which is fulfilled when the
`predicate()` returns _true_. This is a convenient way of waiting for the
`predicate()` to turn `true`.

**Optional parameters:**
- **`name`**: Debug name for this reaction
- **`timeout`**: Number of milliseconds to wait for the predicate to become true. If the timeout elapses first, the returned `Future` completes with a `MobXException`.
- **`context`**: The `ReactiveContext` to use. By default the `mainContext` is used.

```dart
final completed = Observable(false);

Expand Down