Skip to content

Commit 8288890

Browse files
Merge pull request cerebral#153 from cerebral/typescript
Typescript
2 parents cdcee9e + 6857feb commit 8288890

File tree

19 files changed

+232
-23
lines changed

19 files changed

+232
-23
lines changed

packages/node_modules/overmind/src/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
TOperator,
2020
TReaction,
2121
TValueContext,
22+
TOnInitialize,
2223
} from './types'
2324

2425
export * from './types'
@@ -41,9 +42,7 @@ export type Derive<Parent extends object, Value> = TDerive<
4142

4243
export type Reaction = TReaction<TheConfig>
4344

44-
export type OnInitialize = (
45-
context: TValueContext<TheConfig, ResolveActions<TheConfig['actions']>>
46-
) => void
45+
export type OnInitialize = TOnInitialize<TheConfig>
4746

4847
const isPlainObject = require('is-plain-object')
4948
const IS_PRODUCTION = process.env.NODE_ENV === 'production'

packages/node_modules/overmind/src/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,7 @@ export type TReaction<Config extends Configuration> = (
5656
action: TAction<Config, void> | TOperator<Config, void, any>
5757
) => any
5858
) => any
59+
60+
export type TOnInitialize<Config extends Configuration> = (
61+
context: TValueContext<Config, ResolveActions<Config['actions']>>
62+
) => void

packages/overmind-website/api/derive.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,13 @@ The function defining your derived state receives two arguments. The first argum
1212
```marksy
1313
h(Notice, null, "Accessing **rootState** might cause unnecessary updates to the derived function as it will track more state, though typically not an issue")
1414
```
15+
16+
An other use case for derived is to return a function. This allows you to insert functions into your state tree which can execute logic, even based on existing state. Even the function itself might be changed out based on the state of the application.
17+
18+
```marksy
19+
h(Example, { name: "api/derive_function" })
20+
```
21+
22+
```marksy
23+
h(Notice, null, "Using state inside the returned function will not be tracked. You have to access the state in the scope of the derived function")
24+
```

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export default (ts, view) =>
44
{
55
fileName: 'app.ts',
66
code: `
7-
import { Overmind, TApp } from 'overmind'
7+
import { Overmind, TConfig } from 'overmind'
88
import { merge } from 'overmind/config'
99
import * as moduleA from './moduleA'
1010
import * as moduleB from './moduleB'
@@ -15,7 +15,7 @@ const config = merge({
1515
})
1616
1717
declare module 'overmind' {
18-
interface IApp extends TApp<typeof config> {}
18+
interface IConfig extends TConfig<typeof config> {}
1919
}
2020
2121
const app = new Overmind(config)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export default (ts, view) =>
44
{
55
fileName: 'app.ts',
66
code: `
7-
import { Overmind, TApp } from 'overmind'
7+
import { Overmind, TConfig } from 'overmind'
88
import { namespaced } from 'overmind/config'
99
import * as moduleA from './moduleA'
1010
import * as moduleB from './moduleB'
@@ -15,7 +15,7 @@ const config = namespaced({
1515
})
1616
1717
declare module 'overmind' {
18-
interface IApp extends TApp<typeof config> {}
18+
interface IConfig extends TConfig<typeof config> {}
1919
}
2020
2121
const app = new Overmind(config)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
export default (ts) =>
2+
ts
3+
? [
4+
{
5+
fileName: 'app/state.ts',
6+
code: `
7+
import { Derive } from 'overmind'
8+
9+
export type User = {
10+
name: string
11+
}
12+
13+
export type State = {
14+
user: User
15+
tellUser: Derive<State, (message: string) => {}>
16+
}
17+
18+
export const state: State = {
19+
user: {
20+
name: 'John'
21+
},
22+
tellUser: (state) =>
23+
(message) => console.log(message, ', ' + state.user.name)
24+
}
25+
`,
26+
},
27+
]
28+
: [
29+
{
30+
fileName: 'app/state.js',
31+
code: `
32+
export const state = {
33+
user: {
34+
name: 'John'
35+
},
36+
tellUser: (state) =>
37+
(message) => console.log(message, ', ' + state.user.name)
38+
}
39+
`,
40+
},
41+
]
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
export default (ts) =>
2+
ts
3+
? [
4+
{
5+
fileName: 'app/state.ts',
6+
code: `
7+
import { Derive } from 'overmind'
8+
9+
export type State = {
10+
title: string
11+
upperTitle: Derive<State, string>
12+
}
13+
14+
export const state: State = {
15+
title: 'My awesome title',
16+
upperTitle: state => state.title.toUpperCase()
17+
}
18+
`,
19+
},
20+
]
21+
: [
22+
{
23+
fileName: 'app/state.js',
24+
code: `
25+
export const state = {
26+
title: 'My awesome title',
27+
upperTitle: state => state.title.toUpperCase()
28+
}
29+
`,
30+
},
31+
]

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export default (ts, view) =>
44
{
55
fileName: 'app/actions.ts',
66
code: `
7-
import { Overmind, TApp } from 'overmind'
7+
import { Overmind, TConfig } from 'overmind'
88
import { createConnect, TConnect } from 'overmind-${view}'
99
import * as page from 'page'
1010
import { state } from './state'
@@ -18,7 +18,7 @@ const config = {
1818
}
1919
2020
declare module 'overmind' {
21-
interface App extends TApp<typeof config> {}
21+
interface IConfig extends TConfig<typeof config> {}
2222
}
2323
2424
export const app = new Overmind(config)

packages/overmind-website/examples/guide/structuringtheapp/namespaced.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export default (ts, view) =>
44
{
55
fileName: 'app/index.ts',
66
code: `
7-
import { Overmind, TApp } from 'overmind'
7+
import { Overmind, TConfig } from 'overmind'
88
import { namespaced } from 'overmind/config'
99
import * as posts from './posts'
1010
import * as admin from './admin'
@@ -15,7 +15,7 @@ const config = namespaced({
1515
})
1616
1717
declare module 'overmind' {
18-
interface IApp extends TApp<typeof config> {}
18+
interface IConfig extends TConfig<typeof config> {}
1919
}
2020
2121
const app = new Overmind(config)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export default () => [
2+
{
3+
code: `
4+
import { TConfig } from 'overmind'
5+
6+
const config = {}
7+
8+
declare module 'overmind' {
9+
interface IConfig extends TConfig<typeof config> {}
10+
}
11+
`,
12+
},
13+
]

0 commit comments

Comments
 (0)