|
1 | 1 | # Running side effects |
2 | 2 |
|
3 | | -Developing applications is not only about managing state, but also managing side effects. A side effect is typically exampled with an http request or talking to local storage. In Overmind we just call this **effects**. |
| 3 | +Developing applications is not only about managing state, but also managing side effects. A side effect is typically exampled with an http request or talking to local storage. In Overmind we just call this **effects**. It is highly encouraged that you think about your effects as an application specific API that **you** design and implement. An example of this would be: |
4 | 4 |
|
5 | | -Let us start with a simple example. |
| 5 | +```marksy |
| 6 | +h(Example, { name: "guide/runningsideeffects/changestate" }) |
| 7 | +``` |
| 8 | + |
| 9 | +As you can see we are very specific about what the effect does. This will improve the readability of your actual application logic and you keep the low level generic code abstracted away. But let us start more generically and you can choose how far you want to take this encouragement. |
| 10 | + |
| 11 | +## Exposing an existing tool |
| 12 | + |
| 13 | +Let us just expose the [axios](https://github.com/axios/axios) library as an **http** effect. |
6 | 14 |
|
7 | 15 | ```marksy |
8 | 16 | h(Example, { name: "guide/runningsideeffects/axios" }) |
9 | 17 | ``` |
10 | 18 |
|
11 | | -We are just exporting an existing library from our effects file and include it in the application config. Now Overmind is aware of an **http** effect. It can track it for debugging and all operations will have it injected. |
| 19 | +We are just exporting the existing library from our effects file and include it in the application config. Now Overmind is aware of an **http** effect. It can track it for debugging and all actions and operators will have it injected. |
12 | 20 |
|
13 | | -Let us put it to use in an operation that grabs the user of the application. |
| 21 | +Let us put it to use in an action that grabs the current user of the application. |
14 | 22 |
|
15 | 23 | ```marksy |
16 | 24 | h(Example, { name: "guide/runningsideeffects/getuser" }) |
17 | 25 | ``` |
18 | 26 |
|
19 | | -That was basically it. We can take this a step further though. Maybe you want to create a more explicit API effect. |
| 27 | +That was basically it. As you can see we are exposing some low level details like the http method used and the url. Let us follow the encouraged way of doing things and create our own **api** effect. |
| 28 | + |
| 29 | +## Specific API |
20 | 30 |
|
21 | 31 | ```marksy |
22 | 32 | h(Example, { name: "guide/runningsideeffects/object" }) |
23 | 33 | ``` |
24 | 34 |
|
25 | | -Or maybe you need it to be configurable. Improving testability and environment variables. |
| 35 | +We could also make this effect configurable by defining it as a class instead. |
| 36 | +## Configurable effect |
| 37 | + |
| 38 | +By defining a class we improve testability, allow using environment variables and even change out the actual request implementation. |
| 39 | + |
26 | 40 |
|
27 | 41 | ```marksy |
28 | 42 | h(Example, { name: "guide/runningsideeffects/class" }) |
29 | 43 | ``` |
30 | 44 |
|
31 | 45 | ## Summary |
32 | | -Adding side effects is easy, but it is worth taking notice that you also get a chance to map an existing tool to something more domain specific. As we did here converting the general **get** method to a **getUser** method. If you think about it from an application standpoint it is kinda weird that it makes arbitrary requests to a string url. It is better to create an abstraction around it to keep things more maintainable and predictable. |
| 46 | +Adding side effects is easy, but it is worth taking notice that you also get a chance to map an existing tool to something more domain specific. As we did here converting the general **get** method to a **getCurrentUser** method. If you think about it from an application standpoint it is kinda weird that it makes arbitrary requests to a string url. It is better to create an abstraction around it to keep things more maintainable and predictable. |
0 commit comments