Skip to content

Commit 222092b

Browse files
feat(overmind): add AsyncAction type and update docs
1 parent dcd1cd5 commit 222092b

File tree

25 files changed

+199
-114
lines changed

25 files changed

+199
-114
lines changed

packages/node_modules/overmind/src/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,12 @@ export interface Config {}
6767

6868
export interface Context extends IContext<Config> {}
6969

70-
export interface Action<Value = void, ReturnValue = void | Promise<void>>
70+
export interface Action<Value = void, ReturnValue = void>
7171
extends IAction<Config, Value, ReturnValue> {}
7272

73+
export interface AsyncAction<Value = void, ReturnValue = void>
74+
extends IAction<Config, Value, Promise<ReturnValue>> {}
75+
7376
export interface Derive<Parent extends IState, Value>
7477
extends IDerive<Config, Parent, Value> {}
7578

packages/overmind-website/examples/guide/getstarted/changestate.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { tsAppIndex } from '../../templates'
1+
import { tsAppIndex, tsSimpleAppIndex } from '../../templates'
22

33
export default (ts) =>
44
ts
@@ -15,8 +15,7 @@ export const changeCount: Action<number> = ({ state }, countChange) => {
1515
},
1616
{
1717
fileName: 'overmind/index.ts',
18-
code: tsAppIndex(
19-
'angular',
18+
code: tsSimpleAppIndex(
2019
`
2120
import { state } from './state'
2221
import * as actions from './actions'

packages/overmind-website/examples/guide/getstarted/createapp.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { tsAppIndex } from '../../templates'
1+
import { tsAppIndex, tsSimpleAppIndex } from '../../templates'
22

33
export default (ts) =>
44
ts
@@ -17,8 +17,7 @@ export const state: State = {
1717
},
1818
{
1919
fileName: 'overmind/index.ts',
20-
code: tsAppIndex(
21-
'react',
20+
code: tsSimpleAppIndex(
2221
`
2322
import { state } from './state'
2423

packages/overmind-website/examples/guide/getstarted/effect.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { tsAppIndex } from '../../templates'
1+
import { tsAppIndex, tsSimpleAppIndex } from '../../templates'
22

33
export default (ts) =>
44
ts
@@ -9,7 +9,7 @@ export default (ts) =>
99
export const bip = (() => {
1010
const context = new AudioContext()
1111
12-
return (factor) => {
12+
return (factor: number) => {
1313
const o = context.createOscillator()
1414
const g = context.createGain()
1515
@@ -22,15 +22,13 @@ export const bip = (() => {
2222
)
2323
2424
o.start(0)
25-
2625
}
2726
})()
2827
`,
2928
},
3029
{
3130
fileName: 'overmind/index.ts',
32-
code: tsAppIndex(
33-
'angular',
31+
code: tsSimpleAppIndex(
3432
`
3533
import { state } from './state'
3634
import * as actions from './actions'
@@ -87,7 +85,6 @@ export const config = {
8785
)
8886
8987
o.start(0)
90-
9188
}
9289
})()
9390
}

packages/overmind-website/examples/guide/goingfunctional/clean.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,19 @@ export default (ts) =>
44
{
55
fileName: 'overmind/actions.ts',
66
code: `
7-
import { Operator, pipe, debounce } from 'overmind'
8-
import * as o from './operators'
7+
import { Operator, pipe, debounce, mutate, filter } from 'overmind'
98
109
export const search: Operator<string> = pipe(
11-
o.setQuery(),
12-
o.lengthGreaterThan(2),
10+
mutate(({ state }, value) => {
11+
state.query = value
12+
}),
13+
filter(({ state }) => state.query.length > 2),
1314
debounce(200),
14-
o.getSearchResult()
15+
mutate(({ state, effects }) => {
16+
state.isSearching = true
17+
state.searchResult = await effects.api.search(state.query)
18+
state.isSearching = false
19+
})
1520
)
1621
`,
1722
},
@@ -20,14 +25,19 @@ export const search: Operator<string> = pipe(
2025
{
2126
fileName: 'overmind/actions.js',
2227
code: `
23-
import { pipe, debounce } from 'overmind'
24-
import * as o from './operators'
28+
import { pipe, debounce, mutate, filter } from 'overmind'
2529
2630
export const search = pipe(
27-
o.setQuery(),
28-
o.lengthGreaterThan(2),
31+
mutate(({ state }, value) => {
32+
state.query = value
33+
}),
34+
filter(({ state }) => state.query.length > 2),
2935
debounce(200),
30-
o.getSearchResult()
36+
mutate(({ state, effects }) => {
37+
state.isSearching = true
38+
state.searchResult = await effects.api.search(state.query)
39+
state.isSearching = false
40+
})
3141
)
3242
`,
3343
},

packages/overmind-website/examples/guide/routing/compose_imperative.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ export default (ts) =>
44
{
55
fileName: 'overmind/actions.ts',
66
code: `
7-
import { Action } from 'overmind'
7+
import { Action, AsyncAction } from 'overmind'
88
import { Page } from './types'
99
1010
export const showHomePage: Action = ({ state }) => {
1111
state.currentPage = Page.HOME
1212
}
1313
14-
export const showUsersPage: Action = ({ state, effects }) => {
14+
export const showUsersPage: AsyncAction = async ({ state, effects }) => {
1515
state.currentPage = Page.USERS
1616
state.modalUser = null
1717
@@ -22,7 +22,7 @@ export const showUsersPage: Action = ({ state, effects }) => {
2222
}
2323
}
2424
25-
export const showUserModal: Action<{ id: string }> = ({ state, actions }, params) => {
25+
export const showUserModal: AsyncAction<{ id: string }> = async ({ state, actions }, params) => {
2626
actions.showUsersPage()
2727
state.isLoadingUserDetails = true
2828
state.modalUser = await effects.api.getUserWithDetails(params.id)

packages/overmind-website/examples/guide/routing/setup.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@ export default (ts) =>
44
{
55
fileName: 'overmind/actions.ts',
66
code: `
7-
import { Action } from 'overmind'
7+
import { Action, AsyncAction } from 'overmind'
88
99
export const showHomePage: Action = ({ state }) => {
1010
state.currentPage = 'home'
1111
}
1212
13-
export const showUsersPage: Action = async ({ state, effects }) => {
13+
export const showUsersPage: AsyncAction = async ({ state, effects }) => {
1414
state.modalUser = null
1515
state.currentPage = 'users'
1616
state.isLoadingUsers = true
1717
state.users = await effects.api.getUsers()
1818
state.isLoadingUsers = false
1919
}
2020
21-
export const showUserModal: Action<{ id: string }> = async ({ state, effects }, params) => {
21+
export const showUserModal: AsyncAction<{ id: string }> = async ({ state, effects }, params) => {
2222
state.isLoadingUserDetails = true
2323
state.modalUser = await effects.api.getUserWithDetails(params.id)
2424
state.isLoadingUserDetails = false

packages/overmind-website/examples/guide/routing/tab_imperative.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ export default (ts) =>
44
{
55
fileName: 'overmind/actions.ts',
66
code: `
7-
import { Action } from 'overmind'
7+
import { Action, AsyncAction } from 'overmind'
88
import { Page } from './types'
99
1010
export const showHomePage: Action = ({ state }) => {
1111
state.currentPage = Page.HOME
1212
}
1313
14-
export const showUsersPage: Action = ({ state, effects }) => {
14+
export const showUsersPage: AsyncAction = async ({ state, effects }) => {
1515
state.currentPage = Page.USERS
1616
state.modalUser = null
1717
@@ -22,7 +22,7 @@ export const showUsersPage: Action = ({ state, effects }) => {
2222
}
2323
}
2424
25-
export const showUserModal: Action<{ id: string, tab: string }> = ({ state, actions }, params) => {
25+
export const showUserModal: AsyncAction<{ id: string, tab: string }> = async ({ state, actions }, params) => {
2626
actions.showUsersPage()
2727
state.currentUserModalTabIndex = Number(params.tab)
2828
state.isLoadingUserDetails = true

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ export default (ts) =>
44
{
55
fileName: 'overmind/actions.ts',
66
code: `
7-
import { Action } from 'overmind'
7+
import { AsyncAction } from 'overmind'
88
9-
export const loadApp: Action = async ({ effects, state }) => {
9+
export const loadApp: AsyncAction = async ({ effects, state }) => {
1010
state.currentUser = await effects.api.getCurrentUser()
1111
}
1212
`,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ export default (ts) =>
44
{
55
fileName: 'overmind/actions.ts',
66
code: `
7-
import { Action } from 'overmind'
7+
import { AsyncAction } from 'overmind'
88
import { User } from './state'
99
10-
export const getCurrentUser: Action = async ({ effects, state }) => {
10+
export const getCurrentUser: AsyncAction = async ({ effects, state }) => {
1111
state.currentUser = await effects.http.get<User>('/user')
1212
}
1313
`,

0 commit comments

Comments
 (0)