Skip to content

Commit dd0e3d2

Browse files
feat(overmind): provide action and operator payload as second argument
BREAKING CHANGE: value no longer exists, it is provided as second argument
1 parent acc9ee4 commit dd0e3d2

File tree

11 files changed

+108
-126
lines changed

11 files changed

+108
-126
lines changed

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

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,7 @@ import {
3131
runGetterMutation,
3232
} from './operators'
3333

34-
export const onInitialize: OnInitialize = async ({
35-
value: app,
36-
state,
37-
effects,
38-
}) => {
34+
export const onInitialize: OnInitialize = async ({ state, effects }, app) => {
3935
const port = await effects.storage.get<string>('currentPort')
4036
if (port) {
4137
state.port = port
@@ -76,10 +72,10 @@ export const onMessage: Operator<Message> = pipe(
7672
})
7773
)
7874

79-
export const setError: Action<string> = ({ value: error, state }) =>
75+
export const setError: Action<string> = ({ state }, error) =>
8076
(state.error = error)
8177

82-
export const changeNewPortValue: Action<string> = ({ value: port, state }) =>
78+
export const changeNewPortValue: Action<string> = ({ state }, port) =>
8379
(state.newPortValue = String(Number(port)))
8480

8581
export const addConnection: Action = ({ state, effects }) => {
@@ -90,10 +86,10 @@ export const addConnection: Action = ({ state, effects }) => {
9086
effects.connector.connect(state.port)
9187
}
9288

93-
export const changeTab: Action<Tab> = ({ value: tab, state }) =>
89+
export const changeTab: Action<Tab> = ({ state }, tab) =>
9490
(state.currentTab = tab)
9591

96-
export const toggleExpandState: Action<string[]> = ({ value: path, state }) => {
92+
export const toggleExpandState: Action<string[]> = ({ state }, path) => {
9793
const pathString = path.join('.')
9894

9995
if (state.expandedStatePaths.indexOf(pathString) >= 0) {
@@ -106,7 +102,7 @@ export const toggleExpandState: Action<string[]> = ({ value: path, state }) => {
106102
}
107103
}
108104

109-
export const selectAction: Action<string> = ({ value: actionId, state }) => {
105+
export const selectAction: Action<string> = ({ state }, actionId) => {
110106
for (let index in state.currentApp.actionsList) {
111107
const item = state.currentApp.actionsList[index]
112108
if (
@@ -121,17 +117,17 @@ export const selectAction: Action<string> = ({ value: actionId, state }) => {
121117
state.currentApp.currentActionId = actionId
122118
}
123119

124-
export const toggleCollapsedFlush: Action<number> = ({ value: id, state }) => {
120+
export const toggleCollapsedFlush: Action<number> = ({ state }, id) => {
125121
if (!state.expandAllActionDetails) {
126122
state.currentApp.flushes[id].isCollapsed = !state.currentApp.flushes[id]
127123
.isCollapsed
128124
}
129125
}
130126

131-
export const toggleCollapsedOperator: Action<number> = ({
132-
value: operatorIndex,
133-
state,
134-
}) => {
127+
export const toggleCollapsedOperator: Action<number> = (
128+
{ state },
129+
operatorIndex
130+
) => {
135131
if (!state.expandAllActionDetails) {
136132
const currentApp = state.apps[state.currentAppName]
137133
const currentAction = currentApp.actions[currentApp.currentActionId]
@@ -141,10 +137,7 @@ export const toggleCollapsedOperator: Action<number> = ({
141137
}
142138
}
143139

144-
export const toggleGroupedComponent: Action<string> = ({
145-
value: name,
146-
state,
147-
}) => {
140+
export const toggleGroupedComponent: Action<string> = ({ state }, name) => {
148141
const index = state.expandedComponents.indexOf(name)
149142

150143
if (index === -1) {
@@ -154,7 +147,7 @@ export const toggleGroupedComponent: Action<string> = ({
154147
}
155148
}
156149

157-
export const selectApp: Action<string> = ({ value: appName, state }) => {
150+
export const selectApp: Action<string> = ({ state }, appName) => {
158151
state.currentAppName = appName
159152
state.showApps = false
160153
}

packages/node_modules/overmind-devtools/src/overmind/operators.ts

Lines changed: 32 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ import {
3333
} from './utils'
3434

3535
export const ensureCurrentApp: Operator<Message> = action(
36-
({ value: message, state }) => {
36+
({ state }, message) => {
3737
if (!state.currentAppName) {
3838
state.currentAppName = message.appName
3939
}
4040
}
4141
)
4242

4343
export const runGetterMutation: Operator<GetterMessage> = action(
44-
({ value: message, state }) => {
44+
({ state }, message) => {
4545
runMutation(state.apps[message.appName].state)({
4646
method: 'set',
4747
path: message.data.path,
@@ -60,20 +60,15 @@ export const isPortExistsMessage: (
6060
false: Operator<Message, any>
6161
}
6262
) => Operator<Message> = (paths) =>
63-
when(
64-
({ value: message }) => message.messages[0].type === 'PORT_EXISTS',
65-
paths
66-
)
67-
68-
export const addState: Operator<InitMessage> = action(
69-
({ value: message, state }) => {
70-
state.isConnecting = false
71-
state.apps[message.appName].state = message.data.state
72-
}
73-
)
63+
when((_, message) => message.messages[0].type === 'PORT_EXISTS', paths)
64+
65+
export const addState: Operator<InitMessage> = action(({ state }, message) => {
66+
state.isConnecting = false
67+
state.apps[message.appName].state = message.data.state
68+
})
7469

7570
export const addFlushAndRunMutations: Operator<FlushMessage> = action(
76-
({ value: message, state }) => {
71+
({ state }, message) => {
7772
ensureFlushExists(state.apps[message.appName].flushes, message.data.flushId)
7873
state.apps[message.appName].flushes[message.data.flushId].mutations =
7974
message.data.mutations
@@ -96,21 +91,19 @@ export const addFlushAndRunMutations: Operator<FlushMessage> = action(
9691
}
9792
)
9893

99-
export const ensureApp: Operator<Message> = action(
100-
({ value: message, state }) => {
101-
if (
102-
!state.apps[message.appName] ||
103-
message.messages[0].type === ExecutionType.INIT
104-
) {
105-
state.apps[message.appName] = createApp({
106-
name: message.appName,
107-
})
108-
}
94+
export const ensureApp: Operator<Message> = action(({ state }, message) => {
95+
if (
96+
!state.apps[message.appName] ||
97+
message.messages[0].type === ExecutionType.INIT
98+
) {
99+
state.apps[message.appName] = createApp({
100+
name: message.appName,
101+
})
109102
}
110-
)
103+
})
111104

112105
export const addClientMessages: Operator<Message> = action(
113-
({ value: message, state }) => {
106+
({ state }, message) => {
114107
state.apps[message.appName].messages = JSON.parse(
115108
JSON.stringify(message.messages)
116109
)
@@ -120,7 +113,7 @@ export const addClientMessages: Operator<Message> = action(
120113
)
121114

122115
export const addComponent: Operator<AddComponentMessage> = action(
123-
({ value: message, state }) => {
116+
({ state }, message) => {
124117
const id = `${message.data.componentId}_${message.data.componentInstanceId}`
125118

126119
state.apps[message.appName].components[id] = {
@@ -134,7 +127,7 @@ export const addComponent: Operator<AddComponentMessage> = action(
134127
)
135128

136129
export const updateComponent: Operator<UpdateComponentMessage> = action(
137-
({ value: message, state }) => {
130+
({ state }, message) => {
138131
const id = `${message.data.componentId}_${message.data.componentInstanceId}`
139132

140133
state.apps[message.appName].components[id].paths = message.data.paths
@@ -155,15 +148,15 @@ export const updateComponent: Operator<UpdateComponentMessage> = action(
155148
)
156149

157150
export const removeComponent: Operator<RemoveComponentMessage> = action(
158-
({ value: message, state }) => {
151+
({ state }, message) => {
159152
const id = `${message.data.componentId}_${message.data.componentInstanceId}`
160153

161154
state.apps[message.appName].components[id].isMounted = false
162155
}
163156
)
164157

165158
export const updateDerived: Operator<DerivedMessage> = action(
166-
({ value: message, state }) => {
159+
({ state }, message) => {
167160
const appState = state.apps[message.appName].state
168161
const path = message.data.path.split('.')
169162
const key = path.pop()
@@ -175,7 +168,7 @@ export const updateDerived: Operator<DerivedMessage> = action(
175168
)
176169

177170
export const updateFlushWithDerived: Operator<DirtyDerivedMessage> = action(
178-
({ value: message, state }) => {
171+
({ state }, message) => {
179172
ensureFlushExists(state.apps[message.appName].flushes, message.data.flushId)
180173
state.apps[message.appName].flushes[message.data.flushId].derived.push(
181174
message.data.path
@@ -184,7 +177,7 @@ export const updateFlushWithDerived: Operator<DirtyDerivedMessage> = action(
184177
)
185178

186179
export const addAction: Operator<StartActionMessage> = action(
187-
({ value: message, state }) => {
180+
({ state }, message) => {
188181
const app = state.apps[message.appName]
189182
const action = message.data
190183
const actionId = getActionId(action)
@@ -228,7 +221,7 @@ export const addAction: Operator<StartActionMessage> = action(
228221
)
229222

230223
export const addOperator: Operator<StartOperatorMessage> = action(
231-
({ value: message, state }) => {
224+
({ state }, message) => {
232225
const operatorData = message.data
233226
const actionId = getActionId(operatorData)
234227
const action = state.apps[message.appName].actions[actionId]
@@ -263,7 +256,7 @@ export const addOperator: Operator<StartOperatorMessage> = action(
263256
)
264257

265258
export const updateOperator: Operator<EndOperatorMessage> = action(
266-
({ value: message, state }) => {
259+
({ state }, message) => {
267260
const operatorData = message.data
268261
const actionId = getActionId(operatorData)
269262
const action = state.apps[message.appName].actions[actionId]
@@ -278,7 +271,7 @@ export const updateOperator: Operator<EndOperatorMessage> = action(
278271
)
279272

280273
export const updateAction: Operator<EndActionMessage> = action(
281-
({ value: message, state }) => {
274+
({ state }, message) => {
282275
const app = state.apps[message.appName]
283276
const action = message.data
284277
const id = `${action.actionId}_${action.executionId}`
@@ -288,7 +281,7 @@ export const updateAction: Operator<EndActionMessage> = action(
288281
)
289282

290283
export const addMutations: Operator<MutationsMessage> = action(
291-
({ value: message, state }) => {
284+
({ state }, message) => {
292285
const mutations = message.data
293286
const id = `${mutations.actionId}_${mutations.executionId}`
294287
const operator =
@@ -304,7 +297,7 @@ export const addMutations: Operator<MutationsMessage> = action(
304297
)
305298

306299
export const updateEffect: Operator<EffectMessage> = action(
307-
({ value: message, state }) => {
300+
({ state }, message) => {
308301
const effect = message.data
309302
const id = getActionId(effect)
310303
const action = state.apps[message.appName].actions[id]
@@ -332,7 +325,7 @@ export const updateEffect: Operator<EffectMessage> = action(
332325
)
333326

334327
export const getMessages: Operator<Message, AppMessage<any>[]> = map(
335-
({ value }) =>
328+
(_, value) =>
336329
value.messages.map((message) => ({ ...message, appName: value.appName }))
337330
)
338331

@@ -341,7 +334,7 @@ export const forkEachMessage: (
341334
[key: string]: Operator<AppMessage<any>, any>
342335
}
343336
) => Operator<AppMessage<any>[]> = (paths) =>
344-
forEach(fork(({ value }) => value.type, paths) as Operator<AppMessage<any>>)
337+
forEach(fork((_, value) => value.type, paths) as Operator<AppMessage<any>>)
345338

346339
export const updateOperatorAsync: Operator<AsyncOperatorMessage> = action(
347340
() => {}

packages/node_modules/overmind/src/config/config.test.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,12 @@ describe('Config', () => {
6363
return Promise.all([
6464
app.actions.lazy.loadConfig('configA'),
6565
app.actions.loadConfigB({}),
66-
])
67-
.then(() => {
68-
// @ts-ignore
69-
expect(app.state.configA.foo).toEqual('bar')
70-
// @ts-ignore
71-
expect(app.state.configB.bar).toEqual('baz')
72-
})
73-
.catch(() => {
74-
console.log('WUUUT?')
75-
})
66+
]).then(() => {
67+
// @ts-ignore
68+
expect(app.state.configA.foo).toEqual('bar')
69+
// @ts-ignore
70+
expect(app.state.configB.bar).toEqual('baz')
71+
})
7672
})
7773

7874
test('should merge normal and namespaced', () => {

packages/node_modules/overmind/src/config/index.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ export function merge(...configurations: Configuration[]): Configuration {
130130
},
131131
{
132132
onInitialize: initializers.length
133-
? (context) => Promise.all(initializers.map((cb) => cb(context)))
133+
? (context, value) =>
134+
Promise.all(initializers.map((cb) => cb(context, value)))
134135
: undefined,
135136
state: {},
136137
effects: {},
@@ -262,7 +263,7 @@ export function lazy<T extends LazyConfiguration, B = T>(
262263
} {
263264
let app
264265
return {
265-
onInitialize({ value }) {
266+
onInitialize(_, value) {
266267
app = value
267268
},
268269
effects: {
@@ -274,7 +275,7 @@ export function lazy<T extends LazyConfiguration, B = T>(
274275
},
275276
actions: {
276277
lazy: {
277-
loadConfig({ value: key, state, ...rest }) {
278+
loadConfig({ state, ...rest }, key) {
278279
const configToLoad = configurations[key]
279280
configToLoad().then((loadedConfig) => {
280281
const newConfig = namespaced({

packages/node_modules/overmind/src/index.test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ function createDefaultOvermind() {
2727
await Promise.resolve()
2828
context.state.foo = 'bar2'
2929
}
30-
const changeValue: Action<{ isAwesome: boolean }> = (context) => {
31-
context.value.isAwesome = !context.value.isAwesome
30+
const changeValue: Action<{ isAwesome: boolean }> = (context, value) => {
31+
value.isAwesome = !value.isAwesome
3232
}
3333
const changeFormValue: Action<{
3434
key: string
3535
form: { [key: string]: any }
3636
value: any
37-
}> = (context) => {
38-
const { form, key, value } = context.value
37+
}> = (_, payload) => {
38+
const { form, key, value } = payload
3939
form[key] = value
4040
}
4141
const actions = {
@@ -84,9 +84,9 @@ describe('Overmind', () => {
8484
expect.assertions(2)
8585
let value: any
8686
const app = new Overmind({
87-
onInitialize(context) {
87+
onInitialize(context, val) {
8888
expect(context.state.foo).toBe('bar')
89-
value = context.value
89+
value = val
9090
},
9191
state: {
9292
foo: 'bar',
@@ -158,7 +158,7 @@ describe('Overmind', () => {
158158
type: 'action',
159159
})
160160
})
161-
app.actions.doThis()
161+
app.actions.doThis({})
162162
})
163163
test('should track operator start and end', () => {
164164
expect.assertions(2)
@@ -188,7 +188,7 @@ describe('Overmind', () => {
188188
type: 'action',
189189
})
190190
})
191-
app.actions.doThis()
191+
app.actions.doThis({})
192192
})
193193
test('should track mutations', () => {
194194
expect.assertions(1)

0 commit comments

Comments
 (0)