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

Multiple observables in reaction

Open vytautas-pranskunas- opened this issue 3 years ago • 1 comments

Hello,

is there a way to wathch multiple observables in reaction similar to combineLatest? I see in your documentation

reaction
ReactionDisposer reaction<T>(T Function(Reaction) fn, void Function(T) effect)
**Monitors the observables** used inside the fn() tracking function and runs the effect() when the tracking function returns a different value. Only the observables inside fn() are tracked.

but i do not see any example how to add multiple observables into function. I need it because i have to trigger changes on initial items load and after on subject change.

vytautas-pranskunas- avatar Apr 27 '22 12:04 vytautas-pranskunas-

@vytautas-pranskunas-

  final greeting = Observable('Hello World');
  final greeting2 = Observable('Hello World');

  // 1, reaction
  final dispose =
      reaction((_) => [greeting.value, greeting2.value], (values) => print(values));
  // 2. when
  final dispose2 = when(
      (_) => greeting.value == 'Hello MobX' || greeting2.value == 'Hello MobX',
      () => print('Someone greeted MobX'));

  runInAction(() => greeting.value =
      'Hello MobX'); // Causes a change, runs effect and disposes
// Prints:
// Someone greeted MobX
// [Hello MobX, Hello World]

amondnet avatar Jun 15 '22 10:06 amondnet