Skip to content

Commit 029f931

Browse files
committed
refactor(overmind): simplify types, rename effects to context
BREAKING CHANGES: the 'effects' field is now 'context'
1 parent afb6e81 commit 029f931

File tree

23 files changed

+405
-432
lines changed

23 files changed

+405
-432
lines changed

packages/demos/react-typescript-todomvc/src/app/actions.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import * as React from 'react'
2-
import { Action } from 'overmind'
3-
import { Todo } from './state'
4-
import * as operations from './operations'
2+
3+
import { Action } from './'
54
import * as mutations from './mutations'
5+
import * as operations from './operations'
6+
import { Todo } from './state'
67

78
type ChangeEvent = React.ChangeEvent<HTMLInputElement>
89

9-
export const changeNewTodoTitle: Action<ChangeEvent> = (action) =>
10+
export const changeNewTodoTitle: Action<ChangeEvent, string> = (action) =>
1011
action.map(operations.getEventValue).mutate(mutations.setNewTodoTitle)
1112

1213
export const addTodo: Action<React.FormEvent> = (action) =>

packages/demos/react-typescript-todomvc/src/app/effects.ts renamed to packages/demos/react-typescript-todomvc/src/app/context.ts

File renamed without changes.
Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,55 @@
1-
import App from 'overmind'
1+
import App, {
2+
TAction,
3+
TApp,
4+
TContext,
5+
TDerive,
6+
TMutate,
7+
TReaction,
8+
} from 'overmind'
29
import createConnect, { TConnect } from 'overmind-react'
3-
import * as effects from './effects'
10+
411
import * as actions from './actions'
12+
import * as context from './context'
513
import * as state from './state'
614

715
const config = {
8-
effects,
16+
context,
917
actions,
1018
state,
1119
}
1220

13-
declare module 'overmind' {
14-
interface IState extends TState<typeof config> {}
15-
interface IEffects extends TEffects<typeof config> {}
16-
}
17-
1821
const app = new App(config)
1922

2023
export type Connect = TConnect<typeof app>
2124

2225
export const connect = createConnect(app)
2326

27+
type IApp = TApp<typeof config>
28+
2429
export default app
30+
31+
// ==== The following types are copied from overmind documentation ====
32+
33+
export type Action<Value = void, ReturnValue = Value> = TAction<
34+
IApp,
35+
Value,
36+
ReturnValue
37+
>
38+
39+
export type Mutate<Value = any> = TMutate<IApp, Value>
40+
41+
export type Context<Value> = TContext<IApp, Value>
42+
43+
// Operations
44+
export type Map<Value, ReturnValue = Value> = (
45+
ctx: Context<Value>
46+
) => ReturnValue
47+
export type Filter<Value = any> = (ctx: Context<Value>) => boolean
48+
export type When<Value = any> = (ctx: Context<Value>) => boolean
49+
export type Run<Value = any> = (ctx: Context<Value>) => void
50+
export type Fork<Value = any> = (ctx: Context<Value>) => string
51+
export type Attempt<Value, ReturnValue> = (ctx: Context<Value>) => ReturnValue
52+
53+
export type Derive<Value> = TDerive<IApp, Value>
54+
55+
export type Reaction = TReaction<IApp>

packages/demos/react-typescript-todomvc/src/app/mutations.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Mutate } from 'overmind'
1+
import { Mutate } from './'
22
import { Todo } from './state'
33

44
let nextTodoId = 0
Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
import { Operation } from 'overmind'
1+
import { Map, Run } from './'
22

33
type ChangeEvent = React.ChangeEvent<HTMLInputElement>
44

5-
export const getEventValue: Operation.Map<ChangeEvent, string> = ({
6-
value: event,
7-
}) => event.currentTarget.value
5+
export const getEventValue: Map<ChangeEvent, string> = ({ value: event }) =>
6+
event.currentTarget.value
87

9-
export const preventEventDefault: Operation.Run<React.FormEvent> = ({
10-
value: event,
11-
}) => event.preventDefault()
8+
export const preventEventDefault: Run<React.FormEvent> = ({ value: event }) =>
9+
event.preventDefault()

packages/demos/react-typescript-todomvc/src/app/state.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Derive } from 'overmind'
1+
import { Derive } from './'
22

33
export type Todo = {
44
id: string

packages/node_modules/overmind-angular/src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import OvermindApp, { EventType } from 'overmind'
1+
import OvermindApp, { BaseApp, EventType } from 'overmind'
22

3-
export type TConnect<App extends OvermindApp<any, any>> = {
3+
export type TConnect<App extends BaseApp> = {
44
app: {
55
state: App['state']
66
actions: App['actions']
@@ -14,7 +14,7 @@ export type TConnect<App extends OvermindApp<any, any>> = {
1414

1515
let nextComponentId = 0
1616

17-
export default <App extends OvermindApp<any, any>>(app: App) => () => {
17+
export default <App extends OvermindApp<any>>(app: App) => () => {
1818
const componentId = nextComponentId++
1919
let componentInstanceId = 0
2020

packages/node_modules/overmind-devtools/src/app/actions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { Action } from 'overmind'
1+
import { Action } from './'
22
import * as mutations from './mutations'
33
import * as operations from './operations'
4-
import { Message, Tab, GroupedComponent } from './types'
4+
import { GroupedComponent, Message, Tab } from './types'
55

66
const handleClientMessages: Action<Message> = (action) =>
77
action

packages/node_modules/overmind-devtools/src/app/effects.ts renamed to packages/node_modules/overmind-devtools/src/app/context.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as jsonStorage from 'electron-json-storage'
2-
import BackendConnector from '../BackendConnector'
2+
import BackendConnector from 'overmind-devtools/src/BackendConnector'
33

44
export const connector = new BackendConnector()
55

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
1-
import App from 'overmind'
1+
import App, {
2+
TAction,
3+
TApp,
4+
TContext,
5+
TDerive,
6+
TMutate,
7+
TReaction,
8+
} from 'overmind'
29
import createConnect, { TConnect } from 'overmind-react'
3-
import * as effects from './effects'
10+
411
import * as actions from './actions'
12+
import * as context from './context'
513
import * as state from './state'
614

715
const config = {
8-
effects,
16+
context,
917
actions,
1018
state,
1119
}
1220

13-
declare module 'overmind' {
14-
interface IState extends TState<typeof config> {}
15-
interface IEffects extends TEffects<typeof config> {}
16-
}
17-
1821
const app = new App(config, {
1922
devtools: false,
2023
})
@@ -24,3 +27,32 @@ export type Connect = TConnect<typeof app>
2427
export const connect = createConnect(app)
2528

2629
export default app
30+
31+
// === copy/paste
32+
33+
type IApp = TApp<typeof config>
34+
export type Action<Value = void, ReturnValue = any> = TAction<
35+
IApp,
36+
Value,
37+
ReturnValue
38+
>
39+
40+
export type Mutate<Value = any> = TMutate<IApp, Value>
41+
42+
export type Context<Value> = TContext<IApp, Value>
43+
44+
// Operations
45+
export namespace Operation {
46+
export type Map<Value, ReturnValue = Value> = (
47+
ctx: Context<Value>
48+
) => ReturnValue
49+
export type Filter<Value = any> = (ctx: Context<Value>) => boolean
50+
export type When<Value = any> = (ctx: Context<Value>) => boolean
51+
export type Run<Value = any> = (ctx: Context<Value>) => void
52+
export type Fork<Value = any> = (ctx: Context<Value>) => string
53+
export type Attempt<Value, ReturnValue> = (ctx: Context<Value>) => ReturnValue
54+
}
55+
56+
export type Derive<Value> = TDerive<IApp, Value>
57+
58+
export type Reaction = TReaction<IApp>

0 commit comments

Comments
 (0)