Skip to content

Commit 2ac7dc2

Browse files
feat(overmind): expose operator factories instead of action factory
BREAKING CHANGE: action is no longer a function
1 parent 8a4ff02 commit 2ac7dc2

File tree

10 files changed

+164
-215
lines changed

10 files changed

+164
-215
lines changed

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,12 @@ import * as operations from './operations'
22
import * as mutations from './mutations'
33

44
export const changeNewTodoTitle = (action) =>
5-
action()
6-
.map(operations.getEventValue)
7-
.mutate(mutations.setNewTodoTitle)
5+
action.map(operations.getEventValue).mutate(mutations.setNewTodoTitle)
86

97
export const addTodo = (action) =>
10-
action()
11-
.do(operations.preventEventDefault)
8+
action
9+
.run(operations.preventEventDefault)
1210
.mutate(mutations.addTodo)
1311
.mutate(mutations.clearNewTodoTitle)
1412

15-
export const toggleCompleted = (action) =>
16-
action().mutate(mutations.toggleCompleted)
13+
export const toggleCompleted = ({ mutate }) => mutate(mutations.toggleCompleted)

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,13 @@ import * as mutations from './mutations'
77
type ChangeEvent = React.ChangeEvent<HTMLInputElement>
88

99
export const changeNewTodoTitle: Action<ChangeEvent> = (action) =>
10-
action()
11-
.map(operations.getEventValue)
12-
.mutate(mutations.setNewTodoTitle)
10+
action.map(operations.getEventValue).mutate(mutations.setNewTodoTitle)
1311

1412
export const addTodo: Action<React.FormEvent> = (action) =>
15-
action()
16-
.do(operations.preventEventDefault)
13+
action
14+
.run(operations.preventEventDefault)
1715
.mutate(mutations.addTodo)
1816
.mutate(mutations.clearNewTodoTitle)
1917

20-
export const toggleCompleted: Action<Todo> = (action) =>
21-
action().mutate(mutations.toggleCompleted)
18+
export const toggleCompleted: Action<Todo> = ({ mutate }) =>
19+
mutate(mutations.toggleCompleted)

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

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,76 +4,72 @@ import * as operations from './operations'
44
import { Message, Tab } from './types'
55

66
const onMessage: Action<Message> = (action) =>
7-
action()
7+
action
88
.mutate(mutations.performMutationsByMessageType)
99
.mutate(mutations.addMessagesFromClient)
1010

1111
export const loadDevtools: Action = (action) =>
12-
action()
13-
// .do(({ storage }) => storage.clear())
14-
// Do a check if the current app matches the keys of the ???
12+
// .do(({ storage }) => storage.clear())
13+
// Do a check if the current app matches the keys of the ???
14+
action
1515
.map(operations.getAppsFromStorage)
1616
.mutate(mutations.setApps)
1717
.map(operations.getCurrentPortFromStorage)
1818
.mutate(mutations.setCurrentPort)
1919
.mutate(mutations.setAppLoaded)
20-
.do(operations.connectCurrentPort(onMessage(action as any)))
20+
.run(operations.connectCurrentPort(onMessage(action as any)))
2121

22-
export const setError: Action<string> = (action) =>
23-
action().mutate(mutations.setError)
22+
export const setError: Action<string> = ({ mutate }) =>
23+
mutate(mutations.setError)
2424

2525
export const changeNewPortValue: Action<string> = (action) =>
26-
action()
27-
.map(operations.toNumber)
28-
.mutate(mutations.setNewPortValue)
26+
action.map(operations.toNumber).mutate(mutations.setNewPortValue)
2927

3028
export const addPort: Action = (action) =>
31-
action()
29+
action
3230
.map(operations.getNewPortFromState)
3331
.mutate(mutations.setCurrentPort)
3432
.mutate(mutations.addNewApp)
3533
.mutate(mutations.resetNewPortValue)
36-
.do(operations.storeApps)
37-
.do(operations.connectCurrentPort(onMessage))
34+
.run(operations.storeApps)
35+
.run(operations.connectCurrentPort(onMessage))
3836

39-
export const changeTab: Action<Tab> = (action) =>
40-
action().mutate(mutations.changeTab)
37+
export const changeTab: Action<Tab> = ({ mutate }) =>
38+
mutate(mutations.changeTab)
4139

42-
export const toggleExpandState: Action<string[]> = (action) =>
43-
action().mutate(mutations.toggleExpandStatePath)
40+
export const toggleExpandState: Action<string[]> = ({ mutate }) =>
41+
mutate(mutations.toggleExpandStatePath)
4442

45-
export const selectAction: Action<string> = (action) =>
46-
action()
47-
.mutate(mutations.toggleActionItemCollapse)
48-
.mutate(mutations.selectAction)
43+
export const selectAction: Action<string> = ({ mutate }) =>
44+
mutate(mutations.toggleActionItemCollapse).mutate(mutations.selectAction)
4945

5046
type Collapsed = {
5147
isCollapsed: boolean
5248
}
5349

5450
export const toggleCollapsed: Action<Collapsed> = (action) =>
55-
action()
51+
action
5652
.filter(operations.isNotExpandingAllActions)
5753
.mutate(mutations.toggleCollapsed)
5854

59-
export const configurePort: Action = (action) =>
60-
action().mutate(mutations.configurePort)
55+
export const configurePort: Action = ({ mutate }) =>
56+
mutate(mutations.configurePort)
6157

62-
export const cancelConfigurePort: Action = (action) =>
63-
action().mutate(mutations.cancelConfigurePort)
58+
export const cancelConfigurePort: Action = ({ mutate }) =>
59+
mutate(mutations.cancelConfigurePort)
6460

6561
export const removeApp: Action = (action) =>
66-
action()
62+
action
6763
.filter(operations.confirm('Are you sure you want to remove the app?'))
68-
.do(operations.removeCurrentPort)
64+
.run(operations.removeCurrentPort)
6965
.mutate(mutations.removeApp)
70-
.do(operations.storeApps)
71-
.do(operations.connectCurrentPort(onMessage(action as any)))
66+
.run(operations.storeApps)
67+
.run(operations.connectCurrentPort(onMessage(action as any)))
7268

7369
export const selectPort: Action<string> = (action) =>
74-
action()
70+
action
7571
.mutate(mutations.selectPort)
76-
.do(operations.connectCurrentPort(onMessage(action as any)))
72+
.run(operations.connectCurrentPort(onMessage(action as any)))
7773

78-
export const toggleExpandAllActions: Action = (action) =>
79-
action().mutate(mutations.toggleExpandAllActions)
74+
export const toggleExpandAllActions: Action = ({ mutate }) =>
75+
mutate(mutations.toggleExpandAllActions)

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,23 @@ export const getCurrentPortFromStorage: Operation.Map<any, Promise<string>> = ({
1616
export const getNewPortFromState: Operation.Map<any, string> = ({ state }) =>
1717
state.newPortValue
1818

19-
export const storeApps: Operation.Do = ({ storage, state }) =>
19+
export const storeApps: Operation.Run = ({ storage, state }) =>
2020
storage.set('apps', state.apps)
2121

2222
export const toNumber: Operation.Map<string, string> = (_, value) =>
2323
String(Number(value))
2424

2525
export const connectCurrentPort: (
2626
action: (message: any) => void
27-
) => Operation.Do = (action) => ({ state, connector }) => {
27+
) => Operation.Run = (action) => ({ state, connector }) => {
2828
if (!state.currentPort) {
2929
return
3030
}
3131

3232
connector.addPort(state.currentPort, action)
3333
}
3434

35-
export const removeCurrentPort: Operation.Do = ({ state, connector }) =>
35+
export const removeCurrentPort: Operation.Run = ({ state, connector }) =>
3636
connector.removePort(state.currentPort)
3737

3838
export const isNotExpandingAllActions: Operation.Filter = ({ state }) =>

packages/node_modules/overmind/src/Action.ts

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ export interface INoValueAction<
2929
(): Value
3030
}
3131

32+
export type ReturnAction<Value, State, Effects> = [Value] extends [void]
33+
? INoValueAction<State, Effects & { state: State }, Value>
34+
: IValueAction<State, Effects & { state: State }, Value>
35+
3236
export interface Compose<State, Effects, Value, ReturnValue = Value> {
33-
(
34-
action: () => [Value] extends [void]
35-
? INoValueAction<State, Effects & { state: State }, Value>
36-
: IValueAction<State, Effects & { state: State }, Value>
37-
): [Value] extends [void]
37+
(action: ActionClass<State, Effects, Value, Value>): [Value] extends [void]
3838
? INoValueAction<State, Effects & { state: State }, Value, ReturnValue>
3939
: IValueAction<State, Effects & { state: State }, Value, ReturnValue>
4040
}
@@ -45,19 +45,39 @@ interface MutationsEvents {
4545
}
4646
}
4747

48+
export const createOperators = (proxyStateTree, actionChain) => ({
49+
fork: (cb, paths) =>
50+
new ActionClass(proxyStateTree, actionChain).fork(cb, paths),
51+
mutate: (cb) => new ActionClass(proxyStateTree, actionChain).mutate(cb),
52+
run: (cb) => new ActionClass(proxyStateTree, actionChain).run(cb),
53+
map: (cb) => new ActionClass(proxyStateTree, actionChain).map(cb),
54+
attempt: (cb, paths) =>
55+
new ActionClass(proxyStateTree, actionChain).attempt(cb, paths),
56+
when: (cb, paths) =>
57+
new ActionClass(proxyStateTree, actionChain).when(cb, paths),
58+
compose: (action) =>
59+
new ActionClass(proxyStateTree, actionChain).compose(action),
60+
parallel: (actions) =>
61+
new ActionClass(proxyStateTree, actionChain).parallel(actions),
62+
filter: (cb) => new ActionClass(proxyStateTree, actionChain).filter(cb),
63+
debounce: (ms) => new ActionClass(proxyStateTree, actionChain).debounce(ms),
64+
})
65+
4866
export default class ActionClass<
4967
State,
5068
Effects,
5169
InitialValue,
5270
Value = InitialValue
5371
> extends ActionBase<Effects> {
72+
operators: any
5473
constructor(
5574
private proxyStateTree: ProxyStateTree,
5675
actionChain: ActionChain<Effects>,
5776
initialActionId?: number,
5877
runOperators?
5978
) {
6079
super(actionChain, initialActionId, runOperators)
80+
this.operators = createOperators(this.proxyStateTree, this.getActionChain())
6181
}
6282
fork: <Paths>(
6383
cb: (effects: Effects, value: Value) => keyof Paths,
@@ -66,9 +86,7 @@ export default class ActionClass<
6686
? INoValueAction<State, Effects, InitialValue, Value>
6787
: IValueAction<State, Effects, InitialValue, Value> = (cb, paths) => {
6888
const actions = Object.keys(paths).reduce((aggr, key) => {
69-
aggr[key] = paths[key](
70-
() => new ActionClass(this.proxyStateTree, this.getActionChain())
71-
)
89+
aggr[key] = paths[key](this.operators)
7290

7391
return aggr
7492
}, {})
@@ -124,7 +142,7 @@ export default class ActionClass<
124142
runOperators
125143
) as any
126144
}
127-
do: (
145+
run: (
128146
cb: (effects: Effects, value: Value) => void | Promise<any>
129147
) => [InitialValue] extends [void]
130148
? INoValueAction<State, Effects, InitialValue, Value>
@@ -140,7 +158,7 @@ export default class ActionClass<
140158
}
141159

142160
const [chain, initialActionId, runOperators] = this.createOperatorResult(
143-
'do',
161+
'run',
144162
(cb as any).displayName || cb.name,
145163
operator
146164
)
@@ -170,7 +188,7 @@ export default class ActionClass<
170188
runOperators
171189
) as any
172190
}
173-
try: <ResolveValue, RejectValue, NewValue>(
191+
attempt: <ResolveValue, RejectValue, NewValue>(
174192
cb: OperatorCallback<Effects, Value, NewValue>,
175193
paths: {
176194
success: Compose<
@@ -195,12 +213,8 @@ export default class ActionClass<
195213
cb,
196214
paths
197215
) => {
198-
const successPath = paths.success(
199-
() => new ActionClass(this.proxyStateTree, this.getActionChain())
200-
)
201-
const errorPath = paths.error(
202-
() => new ActionClass(this.proxyStateTree, this.getActionChain())
203-
)
216+
const successPath = (paths.success as any)(this.operators)
217+
const errorPath = (paths.error as any)(this.operators)
204218
const operator = (effects, value) => {
205219
return (cb(effects, value) as any)
206220
.then((promiseValue) => {
@@ -211,7 +225,7 @@ export default class ActionClass<
211225
})
212226
}
213227
const [chain, initialActionId, runOperators] = this.createOperatorResult(
214-
'try',
228+
'attempt',
215229
(cb as any).displayName || cb.name,
216230
operator
217231
)
@@ -235,12 +249,8 @@ export default class ActionClass<
235249
cb,
236250
paths
237251
) => {
238-
const trueAction = (paths.true as any)(
239-
() => new ActionClass(this.proxyStateTree, this.getActionChain())
240-
)
241-
const falseAction = (paths.false as any)(
242-
() => new ActionClass(this.proxyStateTree, this.getActionChain())
243-
)
252+
const trueAction = (paths.true as any)(this.operators)
253+
const falseAction = (paths.false as any)(this.operators)
244254
const operator = (effects, value) => {
245255
const isTrue = cb(effects, value)
246256
const path = isTrue ? trueAction : falseAction

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ describe('Computed', () => {
6868
return state.foo + foo
6969
}),
7070
}
71-
const changeFoo: Action = (action) =>
72-
action().mutate((state) => (state.foo = 'bar2'))
71+
const changeFoo: Action = ({ mutate }) =>
72+
mutate((state) => (state.foo = 'bar2'))
7373
const config = {
7474
state,
7575
actions: {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ describe('Derived', () => {
1717
})
1818
test('should track derived state', () => {
1919
let renderCount = 0
20-
const changeFoo: Action = (action) =>
21-
action().mutate((state) => (state.foo = 'bar2'))
20+
const changeFoo: Action = ({ mutate }) =>
21+
mutate((state) => (state.foo = 'bar2'))
2222
const config = {
2323
state: {
2424
foo: 'bar',

0 commit comments

Comments
 (0)