Skip to content

Commit 808a821

Browse files
refactor(overmind): refactor types to work with override and simplify operations
1 parent 3919d49 commit 808a821

File tree

13 files changed

+149
-163
lines changed

13 files changed

+149
-163
lines changed

packages/demos/react-todomvc/src/app/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import App from 'overmind'
1+
import Overmind from 'overmind'
22
import createConnect from 'overmind-react'
33
import * as effects from './effects'
44
import * as actions from './actions'
55
import * as state from './state'
66

7-
const app = new App({
7+
const app = new Overmind({
88
state,
99
actions,
1010
effects,

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import * as React from 'react'
2-
3-
import { Action } from './'
2+
import { Action } from 'overmind'
43
import * as mutations from './mutations'
54
import * as operations from './operations'
65
import { Todo } from './state'
Lines changed: 6 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
1-
import App, {
2-
TAction,
3-
TApp,
4-
TContext,
5-
TDerive,
6-
TMutate,
7-
TReaction,
8-
} from 'overmind'
1+
import Overmind, { TApp } from 'overmind'
92
import createConnect, { TConnect } from 'overmind-react'
103

114
import * as actions from './actions'
@@ -18,38 +11,14 @@ const config = {
1811
state,
1912
}
2013

21-
const app = new App(config)
14+
declare module 'overmind' {
15+
interface App extends TApp<typeof config> {}
16+
}
17+
18+
const app = new Overmind(config)
2219

2320
export type Connect = TConnect<typeof app>
2421

2522
export const connect = createConnect(app)
2623

27-
type IApp = TApp<typeof config>
28-
2924
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>
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
import { Mutate } from './'
1+
import { Operation } from 'overmind'
22
import { Todo } from './state'
33

44
let nextTodoId = 0
55

6-
export const setNewTodoTitle: Mutate<string> = ({ state, value }) =>
6+
export const setNewTodoTitle: Operation.Mutate<string> = ({ state, value }) =>
77
(state.newTodoTitle = value)
88

9-
export const addTodo: Mutate = ({ state }) =>
9+
export const addTodo: Operation.Mutate = ({ state }) =>
1010
state.todos.unshift({
1111
id: String(nextTodoId++),
1212
title: state.newTodoTitle,
1313
completed: false,
1414
})
1515

16-
export const clearNewTodoTitle: Mutate = ({ state }) =>
16+
export const clearNewTodoTitle: Operation.Mutate = ({ state }) =>
1717
(state.newTodoTitle = '')
1818

19-
export const toggleCompleted: Mutate<Todo> = ({ value: todo }) =>
19+
export const toggleCompleted: Operation.Mutate<Todo> = ({ value: todo }) =>
2020
(todo.completed = !todo.completed)
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
import { Map, Run } from './'
1+
import { Operation } from 'overmind'
22

33
type ChangeEvent = React.ChangeEvent<HTMLInputElement>
44

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

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

packages/demos/vue-todomvc/src/app/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import App from 'overmind'
1+
import Overmind from 'overmind'
22
import createConnect from 'overmind-vue'
33
import * as effects from './effects'
44
import * as actions from './actions'
55
import * as state from './state'
66

7-
const app = new App(
7+
const app = new Overmind(
88
{
99
state,
1010
actions,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Action } from './'
1+
import { Action } from 'overmind'
22
import * as mutations from './mutations'
33
import * as operations from './operations'
44
import { Message, Tab } from './types'

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

Lines changed: 4 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
1-
import Overmind, {
2-
TAction,
3-
TApp,
4-
TContext,
5-
TDerive,
6-
TMutate,
7-
TReaction,
8-
} from 'overmind'
1+
import Overmind, { TApp } from 'overmind'
92
import createConnect, { TConnect } from 'overmind-react'
103

114
import * as actions from './actions'
@@ -18,35 +11,9 @@ const config = {
1811
state,
1912
}
2013

21-
type App = TApp<typeof config>
22-
23-
export type Action<Value = void, ReturnValue = any> = TAction<
24-
App,
25-
Value,
26-
ReturnValue
27-
>
28-
29-
export type Mutate<Value = any> = TMutate<App, Value>
30-
31-
export type Context<Value> = TContext<App, Value>
32-
33-
export type Map<Value, ReturnValue = Value> = (
34-
ctx: Context<Value>
35-
) => ReturnValue
36-
37-
export type Filter<Value = any> = (ctx: Context<Value>) => boolean
38-
39-
export type When<Value = any> = (ctx: Context<Value>) => boolean
40-
41-
export type Run<Value = any> = (ctx: Context<Value>) => void
42-
43-
export type Fork<Value = any> = (ctx: Context<Value>) => string
44-
45-
export type Attempt<Value, ReturnValue> = (ctx: Context<Value>) => ReturnValue
46-
47-
export type Derive<Value> = TDerive<App, Value>
48-
49-
export type Reaction = TReaction<App>
14+
declare module 'overmind' {
15+
interface App extends TApp<typeof config> {}
16+
}
5017

5118
const app = new Overmind(config, {
5219
devtools: false,

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

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Mutate } from './'
1+
import { Operation } from 'overmind'
22
import {
33
ActionGroupItem,
44
ActionItem,
@@ -14,24 +14,26 @@ import {
1414
runMutation,
1515
} from './utils'
1616

17-
export const setPort: Mutate<string> = ({ state, value: port }) => {
17+
export const setPort: Operation.Mutate<string> = ({ state, value: port }) => {
1818
if (port) {
1919
state.port = port
2020
}
2121
}
2222

23-
export const selectApp: Mutate<string> = ({ state, value: appName }) =>
24-
(state.currentAppName = appName)
23+
export const selectApp: Operation.Mutate<string> = ({
24+
state,
25+
value: appName,
26+
}) => (state.currentAppName = appName)
2527

26-
export const setConnecting: Mutate = ({ state }) => {
28+
export const setConnecting: Operation.Mutate = ({ state }) => {
2729
state.error = null
2830
state.isConnecting = true
2931
}
3032

31-
export const setPortExists: Mutate = ({ state }) =>
33+
export const setPortExists: Operation.Mutate = ({ state }) =>
3234
(state.error = 'PORT_EXISTS')
3335

34-
export const ensureCurrentApp: Mutate<Message> = ({
36+
export const ensureCurrentApp: Operation.Mutate<Message> = ({
3537
state,
3638
value: message,
3739
}) => {
@@ -40,27 +42,27 @@ export const ensureCurrentApp: Mutate<Message> = ({
4042
}
4143
}
4244

43-
export const setError: Mutate<string> = ({ state, value: error }) =>
45+
export const setError: Operation.Mutate<string> = ({ state, value: error }) =>
4446
(state.error = error)
4547

46-
export const setNewPortValue: Mutate<string> = ({ state, value }) =>
48+
export const setNewPortValue: Operation.Mutate<string> = ({ state, value }) =>
4749
(state.newPortValue = value)
4850

49-
export const resetNewPortValue: Mutate = ({ state }) =>
51+
export const resetNewPortValue: Operation.Mutate = ({ state }) =>
5052
(state.newPortValue = '')
5153

52-
export const addMessagesFromClient: Mutate<Message> = ({
54+
export const addMessagesFromClient: Operation.Mutate<Message> = ({
5355
state,
5456
value: message,
5557
}) =>
5658
(state.apps[message.appName].messages = message.messages
5759
.reverse()
5860
.concat(state.apps[message.appName].messages))
5961

60-
export const changeTab: Mutate<Tab> = ({ state, value: tab }) =>
62+
export const changeTab: Operation.Mutate<Tab> = ({ state, value: tab }) =>
6163
(state.currentTab = tab)
6264

63-
export const toggleExpandStatePath: Mutate<string[]> = ({
65+
export const toggleExpandStatePath: Operation.Mutate<string[]> = ({
6466
state,
6567
value: path,
6668
}) => {
@@ -76,7 +78,7 @@ export const toggleExpandStatePath: Mutate<string[]> = ({
7678
}
7779
}
7880

79-
export const performMutationsByMessageType: Mutate<Message> = ({
81+
export const performMutationsByMessageType: Operation.Mutate<Message> = ({
8082
state,
8183
value: message,
8284
}) => {
@@ -325,7 +327,7 @@ export const performMutationsByMessageType: Mutate<Message> = ({
325327
})
326328
}
327329

328-
export const toggleActionItemCollapse: Mutate<string> = ({
330+
export const toggleActionItemCollapse: Operation.Mutate<string> = ({
329331
state,
330332
value: actionId,
331333
}) => {
@@ -342,21 +344,25 @@ export const toggleActionItemCollapse: Mutate<string> = ({
342344
}
343345
}
344346

345-
export const selectAction: Mutate<string> = ({ state, value: actionId }) =>
346-
(state.currentApp.currentActionId = actionId)
347+
export const selectAction: Operation.Mutate<string> = ({
348+
state,
349+
value: actionId,
350+
}) => (state.currentApp.currentActionId = actionId)
347351

348352
type Collapse = {
349353
isCollapsed: boolean
350354
}
351355

352-
export const toggleCollapsed: Mutate<Collapse> = ({ value: item }) => {
356+
export const toggleCollapsed: Operation.Mutate<Collapse> = ({
357+
value: item,
358+
}) => {
353359
item.isCollapsed = !item.isCollapsed
354360
}
355361

356-
export const toggleExpandAllActions: Mutate = ({ state }) =>
362+
export const toggleExpandAllActions: Operation.Mutate = ({ state }) =>
357363
(state.expandAllActionDetails = !state.expandAllActionDetails)
358364

359-
export const toggleGroupedComponent: Mutate<string> = ({
365+
export const toggleGroupedComponent: Operation.Mutate<string> = ({
360366
state,
361367
value: name,
362368
}) => {
Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,32 @@
1-
import { When, Filter, Map, Run } from './'
1+
import { Operation } from 'overmind'
22
import { Message } from './types'
33

4-
export const isPortExistsMessage: When<Message> = ({ value: message }) =>
5-
message.messages[0].type === 'PORT_EXISTS'
4+
export const isPortExistsMessage: Operation.When<Message> = ({
5+
value: message,
6+
}) => message.messages[0].type === 'PORT_EXISTS'
67

7-
export const confirm: (text: string) => Filter = (text) => ({ utils }) =>
8-
utils.confirmDialog(text)
8+
export const confirm: (text: string) => Operation.Filter = (text) => ({
9+
utils,
10+
}) => utils.confirmDialog(text)
911

10-
export const getCurrentPortFromStorage: Map<any, Promise<string>> = ({
12+
export const getCurrentPortFromStorage: Operation.Map<any, Promise<string>> = ({
1113
storage,
1214
}) => storage.get<string>('currentPort')
1315

14-
export const getNewPortFromState: Map<any, string> = ({ state }) =>
16+
export const getNewPortFromState: Operation.Map<any, string> = ({ state }) =>
1517
state.newPortValue
1618

17-
export const toNumber: Map<string, string> = ({ value }) =>
19+
export const toNumber: Operation.Map<string, string> = ({ value }) =>
1820
String(Number(value))
1921

20-
export const connectCurrentPort: (action: (message: any) => void) => Run = (
21-
action
22-
) => ({ state, connector }) => {
22+
export const connectCurrentPort: (
23+
action: (message: any) => void
24+
) => Operation.Run = (action) => ({ state, connector }) => {
2325
connector.connect(
2426
state.port,
2527
action
2628
)
2729
}
2830

29-
export const isNotExpandingAllActions: Filter = ({ state }) =>
31+
export const isNotExpandingAllActions: Operation.Filter = ({ state }) =>
3032
!state.expandAllActionDetails

0 commit comments

Comments
 (0)