|
| 1 | +import 'dart:async'; |
| 2 | + |
| 3 | +import 'package:gsy_github_app_flutter/common/redux/middleware/epic_store.dart'; |
| 4 | + |
| 5 | + |
| 6 | +/// A function that transforms one stream of actions into another |
| 7 | +/// stream of actions. |
| 8 | +/// |
| 9 | +/// Actions in, actions out. |
| 10 | +/// |
| 11 | +/// The best part: Epics are based on Dart Streams. This makes routine tasks |
| 12 | +/// easy, and complex tasks such as asynchronous error handling, cancellation, |
| 13 | +/// and debouncing a breeze. Once you're inside your Epic, use any stream |
| 14 | +/// patterns you desire as long as anything output from the final, returned |
| 15 | +/// stream, is an action. The actions you emit will be immediately dispatched |
| 16 | +/// through the rest of the middleware chain. |
| 17 | +/// |
| 18 | +/// Epics run alongside the normal Redux dispatch channel, meaning you cannot |
| 19 | +/// accidentally "swallow" an incoming action. Actions always run through the |
| 20 | +/// rest of your middleware chain to your reducers before your Epics even |
| 21 | +/// receive the next action. |
| 22 | +/// |
| 23 | +/// Note: Since the Actions you emit from your Epics are dispatched to your |
| 24 | +/// store, writing an Epic that simply returns the original actions Stream will |
| 25 | +/// result in an infinite loop. Do not do this! |
| 26 | +/// |
| 27 | +/// ## Example |
| 28 | +/// |
| 29 | +/// Let's say your app has a search box. When a user submits a search term, |
| 30 | +/// you dispatch a `PerformSearchAction` which contains the term. In order to |
| 31 | +/// actually listen for the `PerformSearchAction` and make a network request |
| 32 | +/// for the results, we can create an Epic! |
| 33 | +/// |
| 34 | +/// In this instance, our Epic will need to filter all incoming actions it |
| 35 | +/// receives to only the `Action` it is interested in: the `PerformSearchAction`. |
| 36 | +/// Then, we need to make a network request using the provided search term. |
| 37 | +/// Finally, we need to transform those results into an action that contains |
| 38 | +/// the search results. If an error has occurred, we'll want to return an |
| 39 | +/// error action so our app can respond accordingly. |
| 40 | +/// |
| 41 | +/// ### Code |
| 42 | +/// |
| 43 | +/// Stream<dynamic> exampleEpic( |
| 44 | +/// Stream<dynamic> actions, |
| 45 | +/// EpicStore<State> store, |
| 46 | +/// ) { |
| 47 | +/// return actions |
| 48 | +/// .where((action) => action is PerformSearchAction) |
| 49 | +/// .asyncMap((action) => |
| 50 | +/// // Pseudo api that returns a Future of SearchResults |
| 51 | +/// api.search((action as PerformSearch).searchTerm) |
| 52 | +/// .then((results) => new SearchResultsAction(results)) |
| 53 | +/// .catchError((error) => new SearchErrorAction(error))); |
| 54 | +/// } |
| 55 | +typedef Stream<dynamic> Epic<State>( |
| 56 | + Stream<dynamic> actions, EpicStore<State> store); |
| 57 | + |
| 58 | +/// A class that acts as an [Epic], transforming one stream of actions into |
| 59 | +/// another stream of actions. Generally, [Epic] functions are simpler, but |
| 60 | +/// you may have advanced use cases that require a type-safe class. |
| 61 | +/// |
| 62 | +/// ### Example |
| 63 | +/// |
| 64 | +/// class ExampleEpic extends EpicClass<State> { |
| 65 | +/// @override |
| 66 | +/// Stream<dynamic> map(Stream<dynamic> actions, EpicStore<State> store) { |
| 67 | +/// return actions |
| 68 | +/// .where((action) => action is PerformSearchAction) |
| 69 | +/// .asyncMap((action) => |
| 70 | +/// // Pseudo api that returns a Future of SearchResults |
| 71 | +/// api.search((action as PerformSearch).searchTerm) |
| 72 | +/// .then((results) => new SearchResultsAction(results)) |
| 73 | +/// .catchError((error) => new SearchErrorAction(error))); |
| 74 | +/// } |
| 75 | +/// } |
| 76 | +abstract class EpicClass<State> { |
| 77 | + Stream<dynamic> call( |
| 78 | + Stream<dynamic> actions, |
| 79 | + EpicStore<State> store, |
| 80 | + ); |
| 81 | +} |
| 82 | + |
| 83 | +/// An wrapper that allows you to create Epics which handle actions of a |
| 84 | +/// specific type, rather than all actions. |
| 85 | +/// |
| 86 | +/// ### Example |
| 87 | +/// |
| 88 | +/// Stream<dynamic> searchEpic( |
| 89 | +/// // Note: this epic only works with PerformSearchActions |
| 90 | +/// Stream<PerformSearchAction> actions, |
| 91 | +/// EpicStore<State> store, |
| 92 | +/// ) { |
| 93 | +/// return actions |
| 94 | +/// .asyncMap((action) => |
| 95 | +/// api.search(action.searchTerm) |
| 96 | +/// .then((results) => new SearchResultsAction(results)) |
| 97 | +/// .catchError((error) => new SearchErrorAction(error))); |
| 98 | +/// } |
| 99 | +/// |
| 100 | +/// final epic = new TypedEpic<State, PerformSearchAction>(typedSearchEpic); |
| 101 | +/// |
| 102 | +/// ### Combining Typed Epics |
| 103 | +/// |
| 104 | +/// final epic = combineEpics([ |
| 105 | +/// new TypedEpic<State, SearchAction>(searchEpic), |
| 106 | +/// new TypedEpic<State, ProfileAction>(profileEpic), |
| 107 | +/// new TypedEpic<State, ChatAction>(chatEpic), |
| 108 | +/// ]); |
| 109 | +class TypedEpic<State, Action> extends EpicClass<State> { |
| 110 | + final Stream<dynamic> Function( |
| 111 | + Stream<Action> actions, |
| 112 | + EpicStore<State> store, |
| 113 | + ) epic; |
| 114 | + |
| 115 | + TypedEpic(this.epic); |
| 116 | + |
| 117 | + @override |
| 118 | + Stream<dynamic> call(Stream<dynamic> actions, EpicStore<State> store) { |
| 119 | + return epic( |
| 120 | + actions.transform(new StreamTransformer<dynamic, Action>.fromHandlers( |
| 121 | + handleData: (dynamic action, EventSink<Action> sink) { |
| 122 | + if (action is Action) { |
| 123 | + sink.add(action); |
| 124 | + } |
| 125 | + }, |
| 126 | + )), |
| 127 | + store, |
| 128 | + ); |
| 129 | + } |
| 130 | +} |
0 commit comments