Skip to content

Commit

Permalink
effect docs
Browse files Browse the repository at this point in the history
  • Loading branch information
mstniy committed Dec 25, 2023
1 parent db8b410 commit 60c5aa7
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 18 deletions.
18 changes: 14 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Computed:

- [Here is how it works](#here-is-how-it-works)
- [A larger example](#a-larger-example)
- [Effects](#effects)
- [Testing](#testing)
- [Looking at the past](#looking-at-the-past)
- [Computed queries](#computed-queries)
Expand Down Expand Up @@ -65,10 +66,7 @@ Assume for the sake of example that your business logic is to multiply the recei
Here is how you can do this using Computed:

```
final c = $(() => s.use * 2);
final sub = c.asStream.listen((res){
db.write(res);
});
$(() => s.use * 2).asStream.listen(db.write);
```

That's it. Computed will take care of re-running the computation and calling the listener as needed. Note how you did not need to specify a dependency list for the computation, Computed discovers it automatically. You don't even have any mutable state in your app code.
Expand Down Expand Up @@ -141,6 +139,18 @@ $(() {
}).asStream.listen(db.write);
```

## <a name='effects'></a>Effects

Effects allow you to define computations with side effects. Like `.listen` and getters which convert computations to data sources (such as `.asStream`), effects ultimately trigger the computation graph.
Effects are particularly useful if you wish to define side effects depending on multiple data sources or computations:

```
Stream<PageType> activePage;
Stream<bool> isDarkMode;
Computed.effect(() => sendAnalytics(activePage.use, isDarkMode.use));
```

## <a name='testing'></a>Testing

Computed lets you mock any computation or data source, allowing you to easily test downstream behaviour.
Expand Down
2 changes: 1 addition & 1 deletion example/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ void main() async {
return maybeReversed.use.rebuild((p0) => p0.add(0));
});

append0.listen((value) => print(value), () => print('Exception!'));
Computed.effect(() => print(append0.use));

// ignore: unused_local_variable
final unused = $(() {
Expand Down
9 changes: 4 additions & 5 deletions example/temporal_accumulator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ import 'package:computed/computed.dart';
void main() async {
final cont = StreamController<int>.broadcast(sync: true);
final s = cont.stream;
Computed<int>.withPrev((prev) {
final c = Computed<int>.withPrev((prev) {
var res = prev;
s.react((val) => res += val);
return res;
}, initialPrev: 0)
.listen((event) => print(event), (e) {
print('Exception: $e');
});
}, initialPrev: 0);

Computed.effect(() => print(c.use));

await Future.value();
cont.add(1);
Expand Down
9 changes: 4 additions & 5 deletions example/temporal_difference.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ import 'package:computed/computed.dart';
void main() {
final cont = StreamController<int>.broadcast(sync: true);
final s = cont.stream;
$(() {
final c = $(() {
s.use; // Make sure it has a value
late int res;
s.react((val) => res = val - s.prevOr(0));
return res;
}, memoized: false)
.listen((event) => print(event), (e) {
print('Exception: $e');
});
}, memoized: false);

Computed.effect(() => print(c.use));

cont.add(1);
cont.add(1);
Expand Down
6 changes: 3 additions & 3 deletions example/todo.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ void main() async {
(prev) => Tuple2(prev == null, appState.use),
initialPrev: null);

stateIsInitial.listen((state) {
Computed.effect(() {
// Do not save the initial state
if (!state!.item1) db.saveState(state.item2);
}, (e) => print('Exception: ${e.toString()}'));
if (!stateIsInitial.use!.item1) db.saveState(stateIsInitial.use!.item2);
});

uiCreateController.add('new todo');
await Future.delayed(Duration.zero);
Expand Down

0 comments on commit 60c5aa7

Please sign in to comment.