|
| 1 | +# apps |
| 2 | + |
| 3 | +_module_ |
| 4 | +A simple, uni-directional data flow architecture. |
| 5 | +All global functions inside this module can be used on all statefuls. |
| 6 | + |
| 7 | +## Example |
| 8 | + |
| 9 | +We start by defining a model. A model is a record with an initial state and an update function. |
| 10 | +The update function takes a state and a message and returns an `Update` record. |
| 11 | + |
| 12 | +In this example we have a simple counter model. |
| 13 | + |
| 14 | +```lithia |
| 15 | +import apps |
| 16 | +
|
| 17 | +enum Message { |
| 18 | + data Inc |
| 19 | + data Dec |
| 20 | +} |
| 21 | +
|
| 22 | +let model = apps.defineModel 0, { state, msg => |
| 23 | + with msg, type apps.Message { |
| 24 | + Inc: { _ => apps.Update state + 1, Nil }, |
| 25 | + Dec: { _ => apps.Update state - 1, Nil }, |
| 26 | + } |
| 27 | +} |
| 28 | +
|
| 29 | +let view = apps.render model, { dispatch, state => |
| 30 | + print strings.join ["Current state: ", state] |
| 31 | +} |
| 32 | +
|
| 33 | +import tests |
| 34 | +
|
| 35 | +tests.test "apps", { fail => |
| 36 | + rx.await (apps.send view, Inc) |
| 37 | +
|
| 38 | + unless (apps.currentState view) == 1, fail "should be 1" |
| 39 | +} |
| 40 | +``` |
| 41 | + |
| 42 | +- _data_ [Batch](#Batch) |
| 43 | +- _enum_ [Command](#Command) |
| 44 | +- _enum_ [Message](#Message) |
| 45 | +- _data_ [Model](#Model) |
| 46 | +- _data_ [Quit](#Quit) |
| 47 | +- _enum_ [Stateful](#Stateful) |
| 48 | +- _data_ [Store](#Store) |
| 49 | +- _data_ [Update](#Update) |
| 50 | +- _data_ [View](#View) |
| 51 | +- _func_ [currentState](#currentState) stateful |
| 52 | +- _func_ [defineModel](#defineModel) initial, update |
| 53 | +- _func_ [render](#render) model, view |
| 54 | +- _func_ [send](#send) stateful, cmd |
| 55 | +- _func_ [storeFrom](#storeFrom) model |
| 56 | +- _func_ [subscribe](#subscribe) observer, stateful |
| 57 | +- _func_ [update](#update) stateful, msg |
| 58 | + |
| 59 | +## Batch |
| 60 | + |
| 61 | +_data_ A batch of Commands |
| 62 | + |
| 63 | +### Properties |
| 64 | + |
| 65 | +- `cmds` |
| 66 | + |
| 67 | +## Command |
| 68 | + |
| 69 | +_enum_ |
| 70 | +A command can be either a message, a batch of messages or an async message. |
| 71 | + |
| 72 | +### Cases |
| 73 | + |
| 74 | +- [Async](#Async) |
| 75 | +- [Batch](#Batch) |
| 76 | +- [Message](#Message) |
| 77 | + |
| 78 | +## Message |
| 79 | + |
| 80 | +_enum_ |
| 81 | +The messages any stateful can handle. |
| 82 | +`prelude.Nil` and `prelude.None` are used to indicate that no message should be send. |
| 83 | +`prelude.Any` is used to indicate that any message should be send. |
| 84 | + |
| 85 | +### Cases |
| 86 | + |
| 87 | +- [Quit](#Quit) |
| 88 | +- [Nil](#Nil) |
| 89 | +- [None](#None) |
| 90 | +- [Any](#Any) |
| 91 | + |
| 92 | +## Model |
| 93 | + |
| 94 | +_data_ A model defines the abstract and underlaying behaviour of a stateful. |
| 95 | +It has an initial state and an update function. |
| 96 | + |
| 97 | +### Properties |
| 98 | + |
| 99 | +- `initial` - The initial state |
| 100 | +- `update state, msg` - The update function. |
| 101 | + Takes a state and a message and returns an `Update` record. |
| 102 | + |
| 103 | +## Quit |
| 104 | + |
| 105 | +_data_ Quits the whole program |
| 106 | + |
| 107 | +## Stateful |
| 108 | + |
| 109 | +_enum_ |
| 110 | +Represents all statefuls like Stores and Views. |
| 111 | + |
| 112 | +### Cases |
| 113 | + |
| 114 | +- [Store](#Store) |
| 115 | +- [View](#View) |
| 116 | + |
| 117 | +## Store |
| 118 | + |
| 119 | +_data_ A stateful which holds a state and can be observed. |
| 120 | + |
| 121 | +### Properties |
| 122 | + |
| 123 | +- `model` - The Model |
| 124 | +- `states` - A rx.Variable of a State |
| 125 | +- `observers` - A rx.Variable of functions to be notified. |
| 126 | + |
| 127 | +## Update |
| 128 | + |
| 129 | +_data_ The update record includes the new state after applying a message and a command to be send to the store. |
| 130 | + |
| 131 | +### Properties |
| 132 | + |
| 133 | +- `state` - The new state |
| 134 | +- `cmd` - A command to be send to the store. |
| 135 | + |
| 136 | +## View |
| 137 | + |
| 138 | +_data_ A stateful which renders a view. |
| 139 | +The result of the view differs from use case to use case. |
| 140 | + |
| 141 | +Whenever you have one representation of your whole state, the `View` is the right choice. |
| 142 | + |
| 143 | +### Properties |
| 144 | + |
| 145 | +- `store` - The underlying and observed store. |
| 146 | +- `view dispatch, state` - A function which renders the current state. |
| 147 | + Might `dispatch cmd`. |
| 148 | + |
| 149 | +## currentState |
| 150 | + |
| 151 | +_func_ `currentState stateful` |
| 152 | + |
| 153 | +Returns the current state of a stateful. |
| 154 | + |
| 155 | +## defineModel |
| 156 | + |
| 157 | +_func_ `defineModel initial, update` |
| 158 | + |
| 159 | +Creates a new model. |
| 160 | + |
| 161 | +## render |
| 162 | + |
| 163 | +_func_ `render model, view` |
| 164 | + |
| 165 | +Creates a new view by rendering a store on every change. |
| 166 | + |
| 167 | +## send |
| 168 | + |
| 169 | +_func_ `send stateful, cmd` |
| 170 | + |
| 171 | +Sends an eventually async Command to a stateful. |
| 172 | +Always returns a Future. |
| 173 | + |
| 174 | +## storeFrom |
| 175 | + |
| 176 | +_func_ `storeFrom model` |
| 177 | + |
| 178 | +Creates a new store from a model. |
| 179 | +This essentially adds dynamic behaviour to a model. |
| 180 | + |
| 181 | +## subscribe |
| 182 | + |
| 183 | +_func_ `subscribe observer, stateful` |
| 184 | + |
| 185 | +Observes changes of a stateful. |
| 186 | + |
| 187 | +## update |
| 188 | + |
| 189 | +_func_ `update stateful, msg` |
| 190 | + |
| 191 | +Directly applies a message to a stateful. |
0 commit comments