|
| 1 | +library over_react.component_declaration.flux_component; |
| 2 | + |
| 3 | +import 'dart:async'; |
| 4 | +import 'package:w_flux/w_flux.dart'; |
| 5 | +import './annotations.dart' as annotations; |
| 6 | +import './transformer_helpers.dart'; |
| 7 | + |
| 8 | +/// Builds on top of [UiProps], adding typed props for [Action]s and [Store]s in order to integrate with w_flux. |
| 9 | +/// |
| 10 | +/// Use with the over_react transformer via the `@Props()` ([annotations.Props]) annotation. |
| 11 | +abstract class FluxUiProps<ActionsT, StoresT> extends UiProps { |
| 12 | + String get _actionsPropKey => '${propKeyNamespace}actions'; |
| 13 | + String get _storePropKey => '${propKeyNamespace}store'; |
| 14 | + |
| 15 | + /// The prop defined by [ActionsT] that holds all [Action]s that |
| 16 | + /// this component needs access to. |
| 17 | + /// |
| 18 | + /// There is no strict rule on the [ActionsT] type. Depending on application |
| 19 | + /// structure, there may be [Action]s available directly on this object, or |
| 20 | + /// this object may represent a hierarchy of actions. |
| 21 | + ActionsT get actions => props[_actionsPropKey]; |
| 22 | + set actions(ActionsT value) => props[_actionsPropKey] = value; |
| 23 | + |
| 24 | + /// The prop defined by [StoresT]. |
| 25 | + /// |
| 26 | + /// This object should either be an instance of [Store] or should provide access to one or more [Store]s. |
| 27 | + /// |
| 28 | + /// __Instead of storing state within this component via `setState`, it is recommended that data be |
| 29 | + /// pulled directly from these stores.__ This ensures that the data being used is always up to date |
| 30 | + /// and leaves the state management logic to the stores. |
| 31 | + /// |
| 32 | + /// If this component only needs data from a single [Store], then [StoresT] |
| 33 | + /// should be an instance of [Store]. This allows the default implementation |
| 34 | + /// of [redrawOn] to automatically subscribe to the store. |
| 35 | + /// |
| 36 | + /// If this component needs data from multiple [Store] instances, then |
| 37 | + /// [StoresT] should be a class that provides access to these multiple stores. |
| 38 | + /// Then, you can explicitly select the [Store] instances that should be |
| 39 | + /// listened to by overriding [_FluxComponentMixin.redrawOn]. |
| 40 | + StoresT get store => props[_storePropKey]; |
| 41 | + set store(StoresT value) => props[_storePropKey] = value; |
| 42 | +} |
| 43 | + |
| 44 | +/// Builds on top of [UiComponent], adding w_flux integration, much like the [FluxComponent] in w_flux. |
| 45 | +/// |
| 46 | +/// * Flux components are responsible for rendering application views and turning |
| 47 | +/// user interactions and events into [Action]s. |
| 48 | +/// * Flux components can use data from one or many [Store] instances to define |
| 49 | +/// the resulting component. |
| 50 | +/// |
| 51 | +/// Use with the over_react transformer via the `@Component()` ([annotations.Component]) annotation. |
| 52 | +abstract class FluxUiComponent<TProps extends FluxUiProps> extends UiComponent<TProps> |
| 53 | + with _FluxComponentMixin<TProps>, BatchedRedraws {} |
| 54 | + |
| 55 | +/// Builds on top of [UiStatefulComponent], adding `w_flux` integration, much like the [FluxComponent] in w_flux. |
| 56 | +/// |
| 57 | +/// * Flux components are responsible for rendering application views and turning |
| 58 | +/// user interactions and events into [Action]s. |
| 59 | +/// * Flux components can use data from one or many [Store] instances to define |
| 60 | +/// the resulting component. |
| 61 | +/// |
| 62 | +/// Use with the over_react transformer via the `@Component()` ([annotations.Component]) annotation. |
| 63 | +abstract class FluxUiStatefulComponent<TProps extends FluxUiProps, TState extends UiState> |
| 64 | + extends UiStatefulComponent<TProps, TState> |
| 65 | + with _FluxComponentMixin<TProps>, BatchedRedraws {} |
| 66 | + |
| 67 | +/// Helper mixin to keep [FluxUiComponent] and [FluxUiStatefulComponent] clean/DRY. |
| 68 | +/// |
| 69 | +/// Private so it will only get used in this file, since having lifecycle methods in a mixin is risky. |
| 70 | +abstract class _FluxComponentMixin<TProps extends FluxUiProps> { |
| 71 | + TProps get props; |
| 72 | + bool shouldBatchRedraw; |
| 73 | + redraw([callback()]); |
| 74 | + |
| 75 | + /// List of store subscriptions created when the component mounts. |
| 76 | + /// |
| 77 | + /// These subscriptions are canceled when the component is unmounted. |
| 78 | + List<StreamSubscription> _subscriptions = []; |
| 79 | + |
| 80 | + void componentWillMount() { |
| 81 | + /// Subscribe to all applicable stores. |
| 82 | + /// |
| 83 | + /// [Store]s returned by [redrawOn] will have their triggers mapped directly to this components |
| 84 | + /// redraw function. |
| 85 | + /// |
| 86 | + /// [Store]s included in the [getStoreHandlers] result will be listened to and wired up to their |
| 87 | + /// respective handlers. |
| 88 | + Map<Store, Function> handlers = new Map.fromIterable(redrawOn(), |
| 89 | + value: (_) => (_) => redraw())..addAll(getStoreHandlers()); |
| 90 | + handlers.forEach((store, handler) { |
| 91 | + StreamSubscription subscription = store.listen(handler); |
| 92 | + _subscriptions.add(subscription); |
| 93 | + }); |
| 94 | + } |
| 95 | + |
| 96 | + void componentWillUnmount() { |
| 97 | + // Ensure that unmounted components don't batch render |
| 98 | + shouldBatchRedraw = false; |
| 99 | + |
| 100 | + // Cancel all store subscriptions. |
| 101 | + _subscriptions.forEach((StreamSubscription subscription) { |
| 102 | + if (subscription != null) { |
| 103 | + subscription.cancel(); |
| 104 | + } |
| 105 | + }); |
| 106 | + } |
| 107 | + |
| 108 | + /// Define the list of [Store] instances that this component should listen to. |
| 109 | + /// |
| 110 | + /// When any of the returned [Store]s update their state, this component will |
| 111 | + /// redraw. |
| 112 | + /// |
| 113 | + /// If [store] is of type [Store] (in other words, if this component has a |
| 114 | + /// single Store passed in), this will return a list with said store as the |
| 115 | + /// only element by default. Otherwise, an empty list is returned. |
| 116 | + /// |
| 117 | + /// If [store] is actually a composite object with multiple stores, this |
| 118 | + /// method should be overridden to return a list with the stores that should |
| 119 | + /// be listened to. |
| 120 | + /// |
| 121 | + /// @override |
| 122 | + /// redrawOn() => [store.tasks, store.users]; |
| 123 | + List<Store> redrawOn() { |
| 124 | + if (props.store is Store) { |
| 125 | + return [props.store]; |
| 126 | + } else { |
| 127 | + return []; |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + /// If you need more fine-grained control over store trigger handling, |
| 132 | + /// override this method to return a Map of stores to handlers. |
| 133 | + /// |
| 134 | + /// Whenever a store in the returned map triggers, the respective handler will be called. |
| 135 | + /// |
| 136 | + /// Handlers defined here take precedence over the [redrawOn] handling. |
| 137 | + /// If possible, however, [redrawOn] should be used instead of this in order |
| 138 | + /// to avoid keeping additional state within this component and manually |
| 139 | + /// managing redraws. |
| 140 | + Map<Store, Function> getStoreHandlers() { |
| 141 | + return {}; |
| 142 | + } |
| 143 | + |
| 144 | + /// Register a [subscription] that should be canceled when the component unmounts. |
| 145 | + /// |
| 146 | + /// Cancellation will be handled automatically by [componentWillUnmount]. |
| 147 | + void addSubscription(StreamSubscription subscription) { |
| 148 | + _subscriptions.add(subscription); |
| 149 | + } |
| 150 | +} |
0 commit comments