Skip to content

Commit 3220642

Browse files
committed
fix: enhanced documentation
1 parent 818f968 commit 3220642

File tree

6 files changed

+74
-20
lines changed

6 files changed

+74
-20
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,15 @@ class MyAction extends PayloadAction<Payload, Meta, Error> {
204204
}
205205
```
206206

207+
Another, simpler, example is this class I took from the example. It's the action I dispatch when I want to complete a TODO item when I tap on one:
208+
209+
```dart
210+
@immutable
211+
class CompleteTodo extends PayloadAction<Todo, dynamic, dynamic> {
212+
const CompleteTodo(Todo todo) : super(payload: todo);
213+
}
214+
```
215+
207216
### `AsyncThunk` abstract class
208217

209218
Again, no `createAsyncThunk` like in the original but an abstract class. This is an application of the template method design pattern so I'll allow you to specify your operation that returns a `Future` by overriding the `run` method and I'll take care of dispatching actions as the state of your `Future` evolves.

lib/src/action.dart

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,23 @@
11
import 'package:meta/meta.dart';
22

3-
/// Interface to use for your custom actions
3+
/// Base abstract class to make your custom actions more uniform
4+
///
5+
/// ### Examples
6+
///
7+
/// ```dart
8+
/// @immutable
9+
/// class CompleteTodo extends PayloadAction<Todo, void, void> {
10+
/// const CompleteTodo(Todo todo) : super(payload: todo);
11+
/// }
12+
/// ```
13+
///
14+
/// ```dart
15+
/// @immutable
16+
/// class Fulfilled<T, P, M> extends PayloadAction<P, Meta<M>, dynamic> {
17+
/// Fulfilled(P payload, M meta, String requestId) :
18+
/// super(payload: payload, meta: Meta(meta, requestId));
19+
/// }
20+
/// ```
421
@immutable
522
abstract class PayloadAction<Payload, Meta, Error> {
623
final Payload payload;

lib/src/async_thunk.dart

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,49 +17,50 @@ class Meta<T> {
1717

1818
/// Action that gets dispatched when an `AsyncThunk`
1919
/// starts being processed.
20-
///
20+
///
2121
/// Will come with the `payload` passed to the thunk
2222
/// within its `meta` member.
2323
@immutable
24-
class Pending<T, M> extends PayloadAction<dynamic, Meta<M>, dynamic> {
24+
class Pending<T, M> extends PayloadAction<void, Meta<M>, void> {
2525
Pending(M meta, String requestId) : super(meta: Meta(meta, requestId));
2626
}
2727

2828
/// Action that gets dispatched when an `AsyncThunk`
2929
/// finishes successfully.
30-
///
30+
///
3131
/// Will have the `payload` that was initially passed
3232
/// to the thunk within its `meta` and the result
3333
/// of the operation in its `payload`.
3434
@immutable
35-
class Fulfilled<T, P, M> extends PayloadAction<P, Meta<M>, dynamic> {
36-
Fulfilled(P payload, M meta, String requestId) : super(payload: payload, meta: Meta(meta, requestId));
35+
class Fulfilled<T, P, M> extends PayloadAction<P, Meta<M>, void> {
36+
Fulfilled(P payload, M meta, String requestId)
37+
: super(payload: payload, meta: Meta(meta, requestId));
3738
}
3839

39-
4040
/// Action that gets dispatched when an `AsyncThunk`
4141
/// finishes with an error.
42-
///
42+
///
4343
/// Will have the error that occurred in its `error`
4444
/// member and within its `meta` you'll find the
4545
/// `payload` that was initially passed to the thunk.
4646
@immutable
47-
class Rejected<T, M, E> extends PayloadAction<dynamic, Meta<M>, E> {
48-
Rejected(M meta, E error, String requestId) : super(meta: Meta(meta, requestId), error: error);
47+
class Rejected<T, M, E> extends PayloadAction<void, Meta<M>, E> {
48+
Rejected(M meta, E error, String requestId)
49+
: super(meta: Meta(meta, requestId), error: error);
4950
}
5051

5152
/// Abstraction to make thunks that just deal with a `Future`
5253
/// adhere to a standard.
53-
///
54+
///
5455
/// Before the `Future` starts processing this will dispatch a `Pending` action.
55-
///
56+
///
5657
/// After the `Future` resolves successfully this will dispatch a `Fulfilled` action.
57-
///
58+
///
5859
/// After the `Future` fails this will dispatch a `Rejected` action.
59-
///
60-
///
60+
///
61+
///
6162
/// ### Example
62-
///
63+
///
6364
/// ```dart
6465
/// @immutable
6566
/// class FetchTodos extends AsyncThunk<FetchTodos, AppState, void, List<Todo>> {
@@ -72,9 +73,10 @@ class Rejected<T, M, E> extends PayloadAction<dynamic, Meta<M>, E> {
7273
/// }
7374
/// ```
7475
@immutable
75-
abstract class AsyncThunk<Self, State, Payload, Result> implements CallableThunkAction<State> {
76+
abstract class AsyncThunk<Self, State, Payload, Result>
77+
implements CallableThunkAction<State> {
7678
final Payload payload;
77-
79+
7880
const AsyncThunk([this.payload]);
7981

8082
Future<Result> run();

lib/src/create_reducer.dart

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,31 @@ class _ActionReducerMapBuilder<State>
9090

9191
/// Abstraction that allows to create a reducer without
9292
/// writing tons of `if` statements one after the other.
93+
///
94+
/// ### Examples
95+
///
96+
/// ```dart
97+
/// final todosReducer = createReducer<List<Todo>>(
98+
/// List.unmodifiable([]),
99+
/// (builder) => builder
100+
/// .addCase<Fulfilled<FetchTodos, List<Todo>, void>>(
101+
/// (state, action) => action.payload)
102+
/// .addCase<CompleteTodo>((state, action) => List.unmodifiable(state
103+
/// .map<Todo>((e) =>
104+
/// e.id == action.payload.id ? e.copyWith(completed: true) : e)
105+
/// .toList())));
106+
/// ```
107+
///
108+
/// ```dart
109+
/// final statusReducer = createReducer(
110+
/// Map<String, String>.unmodifiable({}),
111+
/// (builder) => builder.addMatcher(
112+
/// (action) =>
113+
/// _isGeneric(action) &&
114+
/// ['Pending', 'Fulfilled', 'Rejected'].contains(_typeOfAction(action)),
115+
/// (state, action) =>
116+
/// Map.unmodifiable({...state, _typeOfThunk(action): _typeOfAction(action)})));
117+
/// ```
93118
Reducer<State> createReducer<State>(
94119
State initialState, BuilderCallback<State> builderCallback) {
95120
final builder = _ActionReducerMapBuilder<State>();

lib/src/nanoid.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import 'dart:math';
33
// Borrowed from https://github.com/pd4d10/nanoid-dart/blob/master/lib/non_secure/generate.dart
44

55
final _random = Random();
6-
final _alphabet = 'ModuleSymbhasOwnPr-0123456789ABCDEFGHNRVfgctiUvz_KqYTJkLxpZXIjQW';
6+
final _alphabet =
7+
'ModuleSymbhasOwnPr-0123456789ABCDEFGHNRVfgctiUvz_KqYTJkLxpZXIjQW';
78

89
/// An inlined copy of nanoid/nonsecure. Generates a non-cryptographically-secure
910
/// random ID string. Automatically used by createAsyncThunk for request IDs, but

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ dependencies:
1313
redux_thunk: ^0.3.0
1414
redux_dev_tools: ^0.5.2
1515
redux_remote_devtools: ^2.0.0
16-
meta: ^1.2.2
16+
meta: '>=1.1.8 <=1.2.2'
1717

1818
dev_dependencies:
1919
test: ^1.3.0

0 commit comments

Comments
 (0)