Skip to content

Commit

Permalink
Fix naming of tracking function argument in reaction (#398)
Browse files Browse the repository at this point in the history
* correct naming of tracking function argument

* update docs with corrected naming of tracking function

* update reaction tests to mention tracking function instead of predicate
  • Loading branch information
MaikuB authored and pavanpodila committed Jan 16, 2020
1 parent a08a9a5 commit 6c90b66
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 26 deletions.
6 changes: 3 additions & 3 deletions docs/src/api/reaction.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ dispose();

## reaction

### `ReactionDisposer reaction<T>(T Function(Reaction) predicate, void Function(T) effect)`
### `ReactionDisposer reaction<T>(T Function(Reaction) fn, void Function(T) effect)`

Monitors the observables used inside the `predicate()` function and runs the `effect()` when
the predicate returns a different value. Only the observables inside `predicate()` are tracked.
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.

```dart
import 'package:mobx/mobx.dart';
Expand Down
6 changes: 3 additions & 3 deletions docs/src/core-concepts.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,10 @@ dispose();
// Hello MobX
```

**`ReactionDisposer reaction<T>(T Function(Reaction) predicate, void Function(T) effect)`**
**`ReactionDisposer reaction<T>(T Function(Reaction) fn, void Function(T) effect)`**

Monitors the observables used inside the `predicate()` function and runs the `effect()` when
the predicate returns a different value. Only the observables inside `predicate()` are tracked.
Monitors the observables used inside the `fn()` function and runs the `effect()` when
the tracking function returns a different value. Only the observables inside `fn()` are tracked.

```dart
import 'package:mobx/mobx.dart';
Expand Down
6 changes: 3 additions & 3 deletions mobx/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,10 @@ dispose();
// Hello MobX
```

**`ReactionDisposer reaction<T>(T Function(Reaction) predicate, void Function(T) effect)`**
**`ReactionDisposer reaction<T>(T Function(Reaction) fn, void Function(T) effect)`**

Monitors the observables used inside the `predicate()` function and runs the `effect()` when
the predicate returns a different value. Only the observables inside `predicate()` are tracked.
Monitors the observables used inside the `fn()` function and runs the `effect()` when
the `fn()` function returns a different value. Only the observables inside `fn()` are tracked.

```dart
import 'package:mobx/mobx.dart';
Expand Down
15 changes: 7 additions & 8 deletions mobx/lib/src/api/reaction.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,28 +31,27 @@ ReactionDisposer autorun(Function(Reaction) fn,
createAutorun(context ?? mainContext, fn,
name: name, delay: delay, onError: onError);

/// Executes the [predicate] function and tracks the observables used in it. Returns
/// Executes the [fn] function and tracks the observables used in it. Returns
/// a function to dispose the reaction.
///
/// The [predicate] is supposed to return a value of type T. When it changes, the
/// The [fn] is supposed to return a value of type T. When it changes, the
/// [effect] function is executed.
///
/// *Note*: Only the [predicate] function is tracked and not the [effect].
/// *Note*: Only the [fn] function is tracked and not the [effect].
///
/// You can also pass in an optional [name], a debouncing [delay] in milliseconds. Use
/// [fireImmediately] if you want to invoke the effect immediately without waiting for
/// the [predicate] to change its value. It is possible to define a custom [equals] function
/// to override the default comparison for the value returned by [predicate], to have fined
/// the [fn] to change its value. It is possible to define a custom [equals] function
/// to override the default comparison for the value returned by [fn], to have fined
/// grained control over when the reactions should run.
ReactionDisposer reaction<T>(
T Function(Reaction) predicate, void Function(T) effect,
ReactionDisposer reaction<T>(T Function(Reaction) fn, void Function(T) effect,
{String name,
int delay,
bool fireImmediately,
EqualityComparator<T> equals,
ReactiveContext context,
void Function(Object, Reaction) onError}) =>
createReaction(context ?? mainContext, predicate, effect,
createReaction(context ?? mainContext, fn, effect,
name: name,
delay: delay,
equals: equals,
Expand Down
6 changes: 3 additions & 3 deletions mobx/lib/src/core/reaction_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ ReactionDisposer createAutorun(
}

/// An internal helper function to create a [reaction]
ReactionDisposer createReaction<T>(ReactiveContext context,
T Function(Reaction) predicate, void Function(T) effect,
ReactionDisposer createReaction<T>(
ReactiveContext context, T Function(Reaction) fn, void Function(T) effect,
{String name,
int delay,
bool fireImmediately,
Expand All @@ -93,7 +93,7 @@ ReactionDisposer createReaction<T>(ReactiveContext context,
var changed = false;

rxn.track(() {
final nextValue = predicate(rxn);
final nextValue = fn(rxn);

// Use the equality-comparator if provided
final isEqual =
Expand Down
2 changes: 1 addition & 1 deletion mobx/test/autorun_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ void main() {
dispose();
});

test('with pre-mature disposal in predicate', () {
test('with pre-mature disposal in tracking function', () {
final x = Observable(10);

final d = autorun((reaction) {
Expand Down
10 changes: 5 additions & 5 deletions mobx/test/reaction_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ void main() {
fakeAsync((async) {
x.value = 11;

// Even though predicate has changed, effect should not be executed
// Even though tracking function has changed, effect should not be executed
expect(executed, isFalse);
async.elapse(const Duration(milliseconds: 500));
expect(
Expand Down Expand Up @@ -127,7 +127,7 @@ void main() {
// Effect should be executed, as we are forcing an immediate change even though there is a delay
expect(executed, isTrue);

x.value = 11; // predicate goes from false -> true
x.value = 11; // tracking function goes from false -> true

executed = false;
async.elapse(const Duration(milliseconds: 500));
Expand All @@ -139,7 +139,7 @@ void main() {
expect(executed, isTrue);

executed = false;
// predicate goes from true -> false, but effect should not run for next 1s
// tracking function goes from true -> false, but effect should not run for next 1s
x.value = 9;

expect(executed, isFalse);
Expand All @@ -151,7 +151,7 @@ void main() {
});
});

test('with pre-mature disposal in predicate', () {
test('with pre-mature disposal in tracking function', () {
final x = Observable(10);
var executed = false;

Expand All @@ -175,7 +175,7 @@ void main() {
d();
});

test('fires onError on exception inside predicate', () {
test('fires onError on exception inside tracking function', () {
var thrown = false;
final dispose = reaction(
(_) {
Expand Down

0 comments on commit 6c90b66

Please sign in to comment.