Skip to content

Commit ee1d3de

Browse files
fix(overmind): fix onInitialize typing
1 parent b7de117 commit ee1d3de

File tree

3 files changed

+43
-8
lines changed

3 files changed

+43
-8
lines changed

packages/node_modules/overmind/src/index.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { ActionChain } from 'action-chain'
22
import { EventEmitter } from 'betsy'
33
import { ProxyStateTree } from 'proxy-state-tree'
44

5-
import { createOperators } from './Action'
5+
import { createOperators, ActionClass, ValueAction } from './Action'
66
import { Derived } from './derived'
77
import { Devtools, Message, safeValue } from './Devtools'
88
import {
@@ -35,6 +35,28 @@ export interface App {}
3535

3636
type IApp = BaseApp & App
3737

38+
export type OnInitialize = (
39+
action: ActionClass<
40+
{
41+
state: IApp['state']
42+
} & IApp['effects'],
43+
{
44+
state: IApp['state']
45+
actions: IApp['actions']
46+
},
47+
any
48+
>
49+
) => ValueAction<
50+
{
51+
state: IApp['state']
52+
} & IApp['effects'],
53+
{
54+
state: IApp['state']
55+
actions: IApp['actions']
56+
},
57+
any
58+
>
59+
3860
export type Action<Value = void, ReturnValue = any> = TAction<
3961
IApp,
4062
Value,
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# OnInitialize
22

3-
If you need to run logic as the application initializes you can use the **onInitialize** hook. This is defined as a callback and it receives the instance of the application. You can do whatever you want here. Run an action, configure a router etc.
3+
If you need to run logic as the application initializes you can use the **onInitialize** hook. This is defined as an action and it receives the instance of the application as the input value. You can do whatever you want here. Run an action, configure a router etc.
4+
5+
Typically you would conceptually think of **onInitialize** as "where you bind outside events to actions". So any effect you have configured would typically be configured with what actions to trigger related to events in the effect.
46

57
```marksy
68
h(Example, { name: "api/onInitialize" })
79
```
810

9-
Overmind will run this logic as a promise, meaning that you can return an action you want to run and can later outside of the application detect when the initialization is done. This promise is named **initialized** and can be used as such:
10-
1111
When you use **modules** all the *onInitialize* will run in parallel.

packages/overmind-website/examples/api/onInitialize.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,13 @@ export default (ts, view) =>
77
fileName: 'app/onInitialize.ts',
88
code: `
99
import { OnInitialize } from 'overmind'
10+
import * as operations from './operations'
11+
import * as mutations from './mutations'
1012
11-
const onInitialize: OnInitialize = (app) => app.actions.loadData()
13+
const onInitialize: OnInitialize = action =>
14+
action
15+
.map(operations.getInitialData)
16+
.mutate(mutations.setInitialData)
1217
1318
export default onInitialize
1419
`,
@@ -35,18 +40,26 @@ const config = {
3540
{
3641
fileName: 'app/onInitialize.ts',
3742
code: `
38-
export default = (app) => app.actions.loadData()
43+
import * as operations from './operations'
44+
import * as mutations from './mutations'
45+
46+
const onInitialize = action =>
47+
action
48+
.map(operations.getInitialData)
49+
.mutate(mutations.setInitialData)
50+
51+
export default onInitialize
3952
`,
4053
},
4154
{
4255
fileName: 'app/index.js',
4356
code: `
44-
import App from 'overmind-${view}'
57+
import { Overmind } from 'overmind'
4558
import onInitialize from './onInitialize'
4659
import * as state from './state'
4760
import * as actions from './actions'
4861
49-
const app = new App({
62+
const app = new Overmind({
5063
onInitialize,
5164
state,
5265
actions

0 commit comments

Comments
 (0)