Skip to content

Commit aec24e8

Browse files
Merge pull request cerebral#485 from cerebral/simplify-typing
Simplify typing
2 parents 19738cf + 00c1893 commit aec24e8

File tree

348 files changed

+5808
-56579
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

348 files changed

+5808
-56579
lines changed

.cz-config.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ module.exports = {
2929
],
3030

3131
scopes: [
32-
{ name: 'website' },
3332
{ name: 'overmind' },
3433
{ name: 'overmind-devtools' },
3534
{ name: 'overmind-devtools-client' },

package-lock.json

Lines changed: 3011 additions & 35696 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@
114114
"ts-loader": "4.4.2",
115115
"tslib": "1.10.0",
116116
"tslint": "5.12.1",
117-
"typescript": "^4.0.2",
117+
"typescript": "^4.1.3",
118118
"url-loader": "1.0.1",
119119
"vue": "3.0.0-rc.10",
120120
"webpack": "4.35.0",
@@ -140,4 +140,4 @@
140140
"config": ".cz-config.js"
141141
}
142142
}
143-
}
143+
}
Lines changed: 97 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,49 @@
1-
import {
2-
Action,
3-
AsyncAction,
4-
Operator,
5-
debounce,
6-
mutate,
7-
pipe,
8-
wait,
9-
} from 'overmind'
1+
import { debounce, pipe, wait, fork } from 'overmind'
2+
import { Context } from './'
103

114
import * as o from './operators'
125
import {
136
ActionsListItemType,
147
Component,
158
ExecutionType,
16-
Message,
9+
AppMessageType,
1710
Tab,
1811
} from './types'
1912
import { createApp, getActionId, isValidJson } from './utils'
2013

21-
export const onMessage: Operator<Message> = pipe(
22-
o.ensureCurrentApp(),
23-
o.ensureApp(),
24-
o.addClientMessage(),
25-
o.getMessage(),
26-
o.forkMessage({
27-
[ExecutionType.INIT]: o.addStateAndActions(),
28-
[ExecutionType.RE_INIT]: o.addStateAndActions(),
29-
[ExecutionType.FLUSH]: o.addFlush(),
30-
[ExecutionType.DERIVED]: o.updateDerived(),
31-
[ExecutionType.MUTATIONS]: o.addMutations(),
32-
[ExecutionType.EFFECT]: o.updateEffect(),
33-
[ExecutionType.STATE]: o.updateState(),
34-
[ExecutionType.COMPONENT_ADD]: o.addComponent(),
35-
[ExecutionType.COMPONENT_UPDATE]: o.updateComponent(),
36-
[ExecutionType.COMPONENT_REMOVE]: o.removeComponent(),
37-
[ExecutionType.DERIVED_DIRTY]: o.updateFlushWithDerived(),
38-
[ExecutionType.ACTION_START]: o.addAction(),
39-
[ExecutionType.OPERATOR_START]: o.addOperator(),
40-
[ExecutionType.OPERATOR_END]: o.updateOperator(),
41-
[ExecutionType.ACTION_END]: o.updateAction(),
42-
[ExecutionType.OPERATOR_ASYNC]: o.updateOperatorAsync(),
43-
[ExecutionType.GETTER]: o.runGetterMutation(),
44-
[ExecutionType.CHART]: o.addChart(),
14+
export const onMessage = pipe(
15+
o.ensureCurrentApp,
16+
o.ensureApp,
17+
o.addClientMessage,
18+
o.getMessage,
19+
fork('type', {
20+
[ExecutionType.INIT]: o.addStateAndActions,
21+
[ExecutionType.RE_INIT]: o.addStateAndActions,
22+
[ExecutionType.FLUSH]: o.addFlush,
23+
[ExecutionType.DERIVED]: o.updateDerived,
24+
[ExecutionType.MUTATIONS]: o.addMutations,
25+
[ExecutionType.EFFECT]: o.updateEffect,
26+
[ExecutionType.STATE]: o.updateState,
27+
[ExecutionType.COMPONENT_ADD]: o.addComponent,
28+
[ExecutionType.COMPONENT_UPDATE]: o.updateComponent,
29+
[ExecutionType.COMPONENT_REMOVE]: o.removeComponent,
30+
[ExecutionType.DERIVED_DIRTY]: o.updateFlushWithDerived,
31+
[ExecutionType.ACTION_START]: o.addAction,
32+
[ExecutionType.OPERATOR_START]: o.addOperator,
33+
[ExecutionType.OPERATOR_END]: o.updateOperator,
34+
[ExecutionType.ACTION_END]: o.updateAction,
35+
[ExecutionType.OPERATOR_ASYNC]: o.updateOperatorAsync,
36+
[ExecutionType.GETTER]: o.runGetterMutation,
37+
[ExecutionType.CHART]: o.addChart,
38+
[AppMessageType.PORT_EXISTS]: () => {},
4539
})
4640
)
4741

48-
export const setError: Action<string> = ({ state }, error) => {
42+
export const setError = ({ state }: Context, error: string) => {
4943
state.error = error
5044
}
5145

52-
export const onDisconnect: Action<string> = ({ state }, appName) => {
46+
export const onDisconnect = ({ state }: Context, appName: string) => {
5347
if (appName in state.apps) {
5448
state.apps[appName] = createApp({
5549
name: appName,
@@ -58,16 +52,16 @@ export const onDisconnect: Action<string> = ({ state }, appName) => {
5852
}
5953
}
6054

61-
export const changeNewPortValue: Action<string> = ({ state }, port) => {
55+
export const changeNewPortValue = ({ state }: Context, port: string) => {
6256
state.newPortValue = String(Number(port))
6357
}
6458

65-
export const changeTab: Action<Tab> = ({ state, effects }, tab) => {
59+
export const changeTab = ({ state, effects }: Context, tab: Tab) => {
6660
state.currentTab = tab
6761
effects.storage.set(`${state.currentApp.name}.currentTab`, tab)
6862
}
6963

70-
export const toggleExpandState: Action<string[]> = ({ state }, path) => {
64+
export const toggleExpandState = ({ state }: Context, path: string[]) => {
7165
const pathString = path.join(state.currentApp.delimiter)
7266

7367
if (state.currentApp.expandedStatePaths.indexOf(pathString) >= 0) {
@@ -82,7 +76,7 @@ export const toggleExpandState: Action<string[]> = ({ state }, path) => {
8276
}
8377
}
8478

85-
export const selectAction: Action<string> = ({ state }, actionId) => {
79+
export const selectAction = ({ state }: Context, actionId: string) => {
8680
for (let index in state.currentApp.actionsList) {
8781
const item = state.currentApp.actionsList[index]
8882
if (
@@ -97,7 +91,7 @@ export const selectAction: Action<string> = ({ state }, actionId) => {
9791
state.currentApp.currentActionId = actionId
9892
}
9993

100-
export const toggleGroupedComponent: Action<string> = ({ state }, name) => {
94+
export const toggleGroupedComponent = ({ state }: Context, name: string) => {
10195
const index = state.currentApp.expandedComponents.indexOf(name)
10296

10397
if (index === -1) {
@@ -107,24 +101,24 @@ export const toggleGroupedComponent: Action<string> = ({ state }, name) => {
107101
}
108102
}
109103

110-
export const selectApp: Action<string> = ({ state }, appName) => {
104+
export const selectApp = ({ state }: Context, appName: string) => {
111105
state.currentAppName = appName
112106
state.showApps = false
113107
}
114108

115-
export const toggleShowApps: Action = ({ state }) => {
109+
export const toggleShowApps = ({ state }: Context) => {
116110
state.showApps = !state.showApps
117111
}
118112

119-
export const toggleCollapsedComponent: Action<Component> = (_, component) => {
113+
export const toggleCollapsedComponent = (_: Context, component: Component) => {
120114
component.isCollapsed = !component.isCollapsed
121115
}
122116

123-
export const toggleQueryingAction: Action = ({ state }) => {
117+
export const toggleQueryingAction = ({ state }: Context) => {
124118
state.currentApp.isQueryingAction = !state.currentApp.isQueryingAction
125119
}
126120

127-
export const changeActionQuery: Action<string> = ({ state }, query) => {
121+
export const changeActionQuery = ({ state }: Context, query: string) => {
128122
state.currentApp.actionQuery = query
129123

130124
const hit = state.currentApp.actionPaths.find((path) =>
@@ -138,9 +132,9 @@ export const changeActionQuery: Action<string> = ({ state }, query) => {
138132
}
139133
}
140134

141-
export const selectQueryAction: Action<string> = (
142-
{ state, effects, actions },
143-
path
135+
export const selectQueryAction = (
136+
{ state, effects, actions }: Context,
137+
path: string
144138
) => {
145139
const existingActionQuery = state.currentApp.selectedActionQuery
146140

@@ -154,7 +148,7 @@ export const selectQueryAction: Action<string> = (
154148
}
155149
}
156150

157-
export const submitQueryAction: Action = ({ state, effects }) => {
151+
export const submitQueryAction = ({ state, effects }: Context) => {
158152
if (!state.currentApp.actionQuerySuggestion) {
159153
return
160154
}
@@ -169,8 +163,8 @@ export const submitQueryAction: Action = ({ state, effects }) => {
169163
)
170164
}
171165

172-
export const executeAction: Operator = pipe(
173-
mutate(({ state, effects }) => {
166+
export const executeAction = pipe(
167+
({ state, effects }: Context) => {
174168
state.isExecutingAction = true
175169

176170
const payload = state.currentApp.actionQueryPayload
@@ -181,36 +175,42 @@ export const executeAction: Operator = pipe(
181175

182176
effects.connector.sendMessage(state.currentApp.name, 'executeAction', {
183177
name: state.currentApp.selectedActionQuery,
184-
// eslint-disable-next-line
185-
payload: JSON.stringify(eval(`(function () { return ${state.currentApp.actionQueryPayload} })()`)),
178+
// eslint-disable-next-line
179+
payload: JSON.stringify(
180+
eval(
181+
`(function () { return ${state.currentApp.actionQueryPayload} })()`
182+
)
183+
),
186184
})
187-
}),
185+
},
188186
wait(500),
189-
mutate(({ state, effects }) => {
187+
({ state, effects }: Context) => {
190188
state.currentApp.actionQueryPayload = ''
191189
effects.storage.set(`${state.currentApp.name}.actionQueryPayload`, '')
192190
state.isExecutingAction = false
193-
})
191+
}
194192
)
195193

196-
export const setActionQueryPayload: Action<string> = (
197-
{ state, effects },
198-
payload
194+
export const setActionQueryPayload = (
195+
{ state, effects }: Context,
196+
payload: string
199197
) => {
200198
state.currentApp.actionQueryPayload = payload
201199
effects.storage.set(`${state.currentApp.name}.actionQueryPayload`, payload)
202200
}
203201

204-
export const setState: Action<string[]> = ({ state }, path) => {
202+
export const setState = ({ state }: Context, path: string[]) => {
205203
state.currentApp.selectedStatePath = path.join(state.currentApp.delimiter)
206204
}
207205

208-
export const undoSettingState: Action = ({ state }) => {
206+
export const undoSettingState = ({ state }: Context) => {
209207
state.currentApp.selectedStatePath = null
210208
}
211209

212-
export const submitState: Action<string> = ({ state, effects }, newState) => {
213-
const path = state.currentApp.selectedStatePath.split(state.currentApp.delimiter)
210+
export const submitState = ({ state, effects }: Context, newState: string) => {
211+
const path = state.currentApp.selectedStatePath.split(
212+
state.currentApp.delimiter
213+
)
214214

215215
effects.connector.sendMessage(state.currentApp.name, 'mutation', {
216216
path,
@@ -221,61 +221,69 @@ export const submitState: Action<string> = ({ state, effects }, newState) => {
221221
state.currentApp.selectedStatePath = null
222222
}
223223

224-
export const toggleRuntimeConfig: Action = ({ state }) => {
224+
export const toggleRuntimeConfig = ({ state }: Context) => {
225225
state.isShowingRuntime = !state.isShowingRuntime
226226
}
227227

228-
export const onAppHostConnected: Action = ({ state, effects }) => {
228+
export const onAppHostConnected = ({ state, effects }: Context) => {
229229
effects.connector.openChromeDevtools()
230230
}
231231

232-
export const refreshApp: Action = ({ state, effects }) => {
232+
export const refreshApp = ({ state, effects }: Context) => {
233233
effects.connector.sendMessage(state.currentAppName, 'refresh')
234234
}
235235

236-
export const setAppDataFromStorage: AsyncAction<{
237-
appName: string
238-
actions: string[]
239-
}> = async ({ state, effects }, { appName, actions }) =>
236+
export const setAppDataFromStorage = async (
237+
{ state, effects }: Context,
238+
{
239+
appName,
240+
actions,
241+
}: {
242+
appName: string
243+
actions: string[]
244+
}
245+
) =>
240246
Promise.all([
241247
effects.storage.get<string>(`${appName}.selectedActionQuery`),
242248
effects.storage.get<string>(`${appName}.actionQueryPayload`),
243249
effects.storage.get<Tab>(`${appName}.currentTab`),
244-
]).then(([selectedActionQuery, actionQueryPayload, tab]: [string, string, Tab]) => {
245-
const actionQuery =
246-
selectedActionQuery && actions.includes(selectedActionQuery)
247-
? selectedActionQuery
250+
]).then(
251+
([selectedActionQuery, actionQueryPayload, tab]: [string, string, Tab]) => {
252+
const actionQuery =
253+
selectedActionQuery && actions.includes(selectedActionQuery)
254+
? selectedActionQuery
255+
: ''
256+
state.apps[appName].selectedActionQuery = actionQuery
257+
state.apps[appName].actionQueryPayload = actionQuery
258+
? actionQueryPayload
248259
: ''
249-
state.apps[appName].selectedActionQuery = actionQuery
250-
state.apps[appName].actionQueryPayload = actionQuery
251-
? actionQueryPayload
252-
: ''
253-
state.currentTab = tab || state.currentTab
254-
})
260+
state.currentTab = tab || state.currentTab
261+
}
262+
)
255263

256-
export const updateActionsSplitSize: Operator<number> = pipe(
264+
export const updateActionsSplitSize = pipe(
257265
debounce(200),
258-
mutate(async ({ state, effects }, size) => {
266+
async ({ state, effects }: Context, size: number) => {
259267
state.actionsSplitSize = size
260268

261269
await effects.storage.set('devtool.actionsSplitSize', size)
262-
})
270+
}
263271
)
264272

265-
export const updateChartsSplitSize: Operator<number> = pipe(
273+
export const updateChartsSplitSize = pipe(
266274
debounce(200),
267-
mutate(async ({ state, effects }, size) => {
275+
async ({ state, effects }: Context, size: number) => {
268276
state.chartsSplitSize = size
269277

270278
await effects.storage.set('devtool.chartsSplitSize', size)
271-
})
279+
}
272280
)
273281

274-
export const clearActions: Action = ({ state }) => {
282+
export const clearActions = ({ state }: Context) => {
275283
state.currentApp.actionsList = []
276284
state.currentApp.currentActionId = null
277285
}
278286

279-
export const selectChart: Action<string> = ({ state }, id) => {
287+
export const selectChart = ({ state }: Context, id: string) => {
280288
state.currentApp.currentChartId = id
281289
}
Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { IConfig, Overmind } from 'overmind'
2-
import { createHook } from 'overmind-react'
1+
import { IContext } from 'overmind'
2+
import { createStateHook, createActionsHook } from 'overmind-react'
33

44
import * as actions from './actions'
55
import * as effects from './effects'
@@ -13,8 +13,11 @@ export const config = {
1313
state,
1414
}
1515

16-
declare module 'overmind' {
17-
interface Config extends IConfig<typeof config> {}
18-
}
16+
export type Context = IContext<{
17+
state: typeof config.state
18+
actions: typeof config.actions
19+
effects: typeof config.effects
20+
}>
1921

20-
export const useOvermind = createHook<typeof config>()
22+
export const useAppState = createStateHook<Context>()
23+
export const useActions = createActionsHook<Context>()

0 commit comments

Comments
 (0)