Skip to content

Commit 4476d20

Browse files
docs(website): improve running side effects doc
1 parent bb0943f commit 4476d20

File tree

5 files changed

+66
-26
lines changed

5 files changed

+66
-26
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
export default (ts) =>
2+
ts
3+
? [
4+
{
5+
fileName: 'overmind/actions.ts',
6+
code: `
7+
import { Action } from 'overmind'
8+
9+
export const getCurrentUser: Action = async ({ api, state }) => {
10+
state.currentUser = await api.getCurrentUser()
11+
}
12+
`,
13+
},
14+
]
15+
: [
16+
{
17+
fileName: 'overmind/actions.js',
18+
code: `
19+
export const getCurrentUser = async ({ api, state }) => {
20+
state.currentUser = await api.getCurrentUser()
21+
}
22+
`,
23+
},
24+
]

packages/overmind-website/examples/guide/runningsideeffects/class.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,24 @@ export default (ts) =>
77
import axios from 'axios'
88
import { User } from './state'
99
10-
interface IHttp {
10+
interface IRequest {
1111
get<T>(url: string): Promise<T>
1212
}
1313
14-
class Http {
14+
export class Api {
1515
private baseUrl: string
16-
private request: IHttp
17-
constructor (baseUrl: string, request: IHttp) {
16+
private request: IRequest
17+
constructor (baseUrl: string, request: IRequest) {
1818
this.baseUrl = baseUrl
1919
this.request = request
2020
}
21-
getUser(): Promise<User> {
21+
getCurrentUser(): Promise<User> {
2222
return this.request.get(\`\${this.baseUrl}/user\`)
2323
}
2424
}
2525
26-
export const http =
27-
new Http(IS_PRODUCTION ? '/api/v1' : 'http://localhost:4321', axios)
26+
export const api =
27+
new Api(IS_PRODUCTION ? '/api/v1' : 'http://localhost:4321', axios)
2828
`,
2929
},
3030
]
@@ -34,7 +34,7 @@ export const http =
3434
code: `
3535
import axios from 'axios'
3636
37-
class Http {
37+
export class Api {
3838
constructor (baseUrl, request) {
3939
this.baseUrl = baseUrl
4040
this.request = request
@@ -45,7 +45,7 @@ class Http {
4545
}
4646
4747
export const http =
48-
new Http(IS_PRODUCTION ? '/api/v1' : 'http://localhost:4321', axios)
48+
new Api(IS_PRODUCTION ? '/api/v1' : 'http://localhost:4321', axios)
4949
`,
5050
},
5151
]

packages/overmind-website/examples/guide/runningsideeffects/getuser.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,24 @@ export default (ts) =>
22
ts
33
? [
44
{
5-
fileName: 'overmind/operations.ts',
5+
fileName: 'overmind/actions.ts',
66
code: `
7-
import { Operation } from 'overmind'
7+
import { Action } from 'overmind'
88
import { User } from './state'
99
10-
export const getUser: Operation.Map<any, Promise<User>> = ({ http }) =>
11-
http.get<User>('/user')
10+
export const getCurrentUser: Action = async ({ http, state }) => {
11+
state.currentUser = await http.get<User>('/user')
12+
}
1213
`,
1314
},
1415
]
1516
: [
1617
{
17-
fileName: 'overmind/operations.js',
18+
fileName: 'overmind/actions.js',
1819
code: `
19-
export const getUser = ({ http }) =>
20-
http.get('/user')
20+
export const getCurrentUser = async ({ http, state }) => {
21+
state.currentUser = await http.get('/user')
22+
}
2123
`,
2224
},
2325
]

packages/overmind-website/examples/guide/runningsideeffects/object.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ export default (ts) =>
77
import * as axios from 'axios'
88
import { User } from './state'
99
10-
export const http = {
11-
getUser() {
10+
export const api = {
11+
getCurrentUser() {
1212
return axios.get<User>('/user')
1313
}
1414
}
@@ -22,7 +22,7 @@ export const http = {
2222
import axios from 'axios'
2323
2424
export const http = {
25-
getUser() {
25+
getCurrentUser() {
2626
return axios.get('/user')
2727
}
2828
}
Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,46 @@
11
# Running side effects
22

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:
44

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.
614

715
```marksy
816
h(Example, { name: "guide/runningsideeffects/axios" })
917
```
1018

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.
1220

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.
1422

1523
```marksy
1624
h(Example, { name: "guide/runningsideeffects/getuser" })
1725
```
1826

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
2030

2131
```marksy
2232
h(Example, { name: "guide/runningsideeffects/object" })
2333
```
2434

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+
2640

2741
```marksy
2842
h(Example, { name: "guide/runningsideeffects/class" })
2943
```
3044

3145
## 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

Comments
 (0)