Skip to content

Commit 4814c56

Browse files
christianalfonigitbook-bot
authored andcommitted
GitBook: [master] 3 pages modified
1 parent a5d3eb2 commit 4814c56

File tree

3 files changed

+147
-10
lines changed

3 files changed

+147
-10
lines changed

api/action.md

Lines changed: 77 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,99 @@
11
# action
22

3+
```javascript
4+
import App from 'overmind/$VIEW'
5+
6+
const app = new App({
7+
actions: action => ({
8+
doThis: action()
9+
.do((context, value) => {})
10+
})
11+
})
12+
```
13+
14+
The actions is defined with a callback receiving the applications action factory. The function is expected to return an object with actions.
15+
316
### map
417

5-
yeah
18+
Returns a new value to the chain.
19+
20+
```javascript
21+
export default action => ({
22+
getEventValue: action()
23+
.map((_, event) => event.target.value)
24+
})
25+
```
26+
27+
```javascript
28+
export default action => ({
29+
getPosts: action()
30+
.map(({ api }) => api.getPosts())
31+
})
32+
```
633

734
### mutation
835

9-
yeah
36+
Change the state of the application. State changes are only allowed in this operator, which is why it only receives the state as the first argument, not the other providers.
37+
38+
```javascript
39+
export default action => ({
40+
changeInputValue: action()
41+
.mutation((state, value) => state.inputValue = value)
42+
})
43+
```
1044

1145
### do
1246

13-
yeah
47+
Perform some side effect without returning a new value to the chain. Any returned value will be ignored.
48+
49+
```javascript
50+
export default action => ({
51+
onClick: action()
52+
.do(({ track }) => track.click('someClick'))
53+
})
54+
```
1455

1556
### debounce
1657

17-
yeah
58+
If the action has not been called again within the number of milliseconds, continue execution.
59+
60+
```javascript
61+
export default action => ({
62+
search: action()
63+
.mutation((state, value) => state.searchInputValue = value)
64+
.debounce(200)
65+
.map(({ api}, value) => api.search(value)
66+
})
67+
```
1868
1969
### when
2070
21-
yeah
71+
Fork execution into **true** and **false** actions based on an expression.
72+
73+
```javascript
74+
export default action => ({
75+
openProfile: action()
76+
.when(({ state }) => state.user.isLoggedIn, {
77+
true: action(),
78+
false: action()
79+
})
80+
})
81+
```
2282
2383
### fork
2484
25-
yeah
85+
Fork execution into any number of actions. Note that fork does not return a new value to the chain.
86+
87+
```javascript
88+
export default action => ({
89+
openAdmin: action()
90+
.fork(({ state }) => state.user.role, {
91+
admin: action(),
92+
superuser: action(),
93+
user: action()
94+
})
95+
})
96+
```
2697
2798
2899

api/app.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,68 @@
11
# app
22

3+
```javascript
4+
import App from 'overmind/$view'
5+
6+
const app = new App({
7+
state,
8+
actions,
9+
providers,
10+
reactions
11+
}, {
12+
// Connect to devtools with host and port. The host
13+
// might need to be your local IP, depending on environment
14+
devtools: 'localhost:1234'
15+
})
16+
```
17+
18+
Overmind currently supports **react** as view layer.
19+
20+
### Extend App
21+
22+
You can extend the Overmind application class to create a custom connector to other view layers.
23+
24+
```javascript
25+
import App from 'overmind'
26+
27+
class MyApp extends App {
28+
connect(component) {
29+
// Starts tracking state access
30+
const trackId = this.trackState()
31+
32+
// Stops tracking state access
33+
const paths = this.clearTrackstate(trackId)
34+
35+
// Creates a mutation subscription
36+
const listener = this.addMutationListener(paths, () => {
37+
// This callback is called whenever a mutation
38+
// affects the tracked paths. Typically used
39+
// to re-render the component
40+
})
41+
42+
// You will typically track the paths again when re-rendering
43+
// the component. Update the listener by passing the new paths
44+
listener.update(paths)
45+
46+
// Typically when component unmounts you want to dispose
47+
// the listener
48+
listener.dispose()
49+
50+
// A factory for creating reactions in components
51+
const reaction = this.createReactionFactory(component.name)
52+
53+
// Pass this function to the component, which allows reactions
54+
// to be added with the following signature:
55+
// this.props.reaction('name', state => state.foo, () => {
56+
// do something on state change
57+
// })
58+
reaction.add
59+
60+
// Disposes the reactions registered. Typically do this when
61+
// component unmounts
62+
reaction.dispose
63+
}
64+
}
65+
```
66+
67+
Take a look at the existing view implementations for inspiration and reference.
68+

untitled.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Side effects
22

3-
Overmind helps you expose side effects to your application logic. It does this with a concept called **Providers**. A provider is nothing more than an object with methods or a class instance. For example exposing an http library like [axios](https://github.com/axios/axios) could be done like this:
3+
Overmind helps you expose side effects to your application logic. It does this with a concept called **Effects**. An effect is nothing more than an object with methods or a class instance. For example exposing an http library like [axios](https://github.com/axios/axios) could be done like this:
44

55
{% code-tabs %}
66
{% code-tabs-item title="app/providers.js" %}
@@ -29,20 +29,20 @@ export const api = new Api('/api/v1')
2929

3030
Using a class is useful when you want to pass in options based on the environment, or maybe the library exposes a class in itself.
3131

32-
The providers are added to the configuration of the app:
32+
The effects are added to the configuration of the app:
3333

3434
{% code-tabs %}
3535
{% code-tabs-item title="app/index.js" %}
3636
```javascript
3737
import App from 'overmind/$VIEW'
3838
import state from './state'
3939
import actions from './actions'
40-
import providers from './providers'
40+
import effects from './effects'
4141

4242
const app = new App({
4343
state,
4444
actions,
45-
providers
45+
effects
4646
})
4747

4848
export const connect = app.connect

0 commit comments

Comments
 (0)