Skip to content

Commit c85dc03

Browse files
Merge pull request cerebral#112 from cerebral/fixEffects
Fix effects
2 parents ece40b4 + 0bca557 commit c85dc03

File tree

36 files changed

+542
-293
lines changed

36 files changed

+542
-293
lines changed

.eslintrc.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
"no-unused-expressions": "off",
3737
"no-use-before-define": "off",
3838
"no-useless-constructor": "off",
39-
"typescript/no-unused-vars": "error"
39+
"typescript/no-unused-vars": "error",
40+
"standard/no-callback-literal": "off"
4041
}
4142
}
4243
]
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
let nextTodoId = 0
22

3-
export const setNewTodoTitle = (state, value) => (state.newTodoTitle = value)
3+
export const setNewTodoTitle = ({ state, value }) =>
4+
(state.newTodoTitle = value)
45

56
export const addTodo = (state) =>
67
state.todos.unshift({
@@ -9,6 +10,7 @@ export const addTodo = (state) =>
910
completed: false,
1011
})
1112

12-
export const clearNewTodoTitle = (state) => (state.newTodoTitle = '')
13+
export const clearNewTodoTitle = ({ state }) => (state.newTodoTitle = '')
1314

14-
export const toggleCompleted = (_, todo) => (todo.completed = !todo.completed)
15+
export const toggleCompleted = ({ value: todo }) =>
16+
(todo.completed = !todo.completed)
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
export const getEventValue = (_, event) => event.currentTarget.value
1+
export const getEventValue = ({ value: event }) => event.currentTarget.value
22

3-
export const preventEventDefault = (_, event) => event.preventDefault()
3+
export const preventEventDefault = ({ value: event }) => event.preventDefault()

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,18 @@ import { Todo } from './state'
33

44
let nextTodoId = 0
55

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

9-
export const addTodo: Mutate = (state) =>
9+
export const addTodo: 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) => (state.newTodoTitle = '')
16+
export const clearNewTodoTitle: Mutate = ({ state }) =>
17+
(state.newTodoTitle = '')
1718

18-
export const toggleCompleted: Mutate<Todo> = (_, todo) =>
19+
export const toggleCompleted: Mutate<Todo> = ({ value: todo }) =>
1920
(todo.completed = !todo.completed)

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ import { Operation } from 'overmind'
22

33
type ChangeEvent = React.ChangeEvent<HTMLInputElement>
44

5-
export const getEventValue: Operation.Map<ChangeEvent, string> = (_, 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: Operation.Run<React.FormEvent> = (_, event) =>
9-
event.preventDefault()
9+
export const preventEventDefault: Operation.Run<React.FormEvent> = ({
10+
value: event,
11+
}) => event.preventDefault()
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
let nextTodoId = 0
22

3-
export const setNewTodoTitle = (state, value) => (state.newTodoTitle = value)
3+
export const setNewTodoTitle = ({ state, value }) =>
4+
(state.newTodoTitle = value)
45

5-
export const addTodo = (state) =>
6+
export const addTodo = ({ state }) =>
67
state.todos.unshift({
78
id: String(nextTodoId++),
89
title: state.newTodoTitle,
910
completed: false,
1011
})
1112

12-
export const clearNewTodoTitle = (state) => (state.newTodoTitle = '')
13+
export const clearNewTodoTitle = ({ state }) => (state.newTodoTitle = '')
1314

14-
export const toggleCompleted = (_, todo) => (todo.completed = !todo.completed)
15+
export const toggleCompleted = ({ value: todo }) =>
16+
(todo.completed = !todo.completed)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export const preventEventDefault = (_, event) => event.preventDefault()
1+
export const preventEventDefault = ({ value: event }) => event.preventDefault()

packages/node_modules/action-chain/src/index.test.ts

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,20 @@ class Action<Context, InitialValue, Value = InitialValue> extends ActionBase<
1414
Context
1515
> {
1616
test: (
17-
cb: (context: Context, value: Value) => void
17+
cb: (
18+
arg: {
19+
context: Context
20+
value: Value
21+
}
22+
) => void
1823
) => InitialValue extends void
1924
? INoValueAction<Context, InitialValue, Value>
2025
: IAction<Context, InitialValue, Value> = (cb) => {
2126
const operator = (context, value) => {
22-
return cb(context, value)
27+
return cb({
28+
context,
29+
value,
30+
})
2331
}
2432

2533
const [chain, initialActionId, runOperators] = this.createOperatorResult(
@@ -110,18 +118,18 @@ describe('VALUE', () => {
110118
describe('CONTEXT', () => {
111119
test('should pass default context', () => {
112120
expect.assertions(2)
113-
const test = action().test(({ __execution, __path }: any) => {
114-
expect(__execution).toBeTruthy()
115-
expect(__path).toBeTruthy()
121+
const test = action().test(({ context }: any) => {
122+
expect(context.__execution).toBeTruthy()
123+
expect(context.__path).toBeTruthy()
116124
})
117125

118126
test()
119127
})
120128

121129
test('should be able to extend context', () => {
122130
expect.assertions(1)
123-
const fn = action().test(({ foo }) => {
124-
expect(foo.bar()).toBe('baz')
131+
const fn = action().test(({ context }) => {
132+
expect(context.foo.bar()).toBe('baz')
125133
})
126134

127135
fn()
@@ -131,8 +139,8 @@ describe('CONTEXT', () => {
131139
describe('EFFECTS', () => {
132140
test('should track execution of effects', () => {
133141
expect.assertions(2)
134-
const fn = action().test(({ foo }) => {
135-
expect(foo.bar()).toBe('baz')
142+
const fn = action().test(({ context }) => {
143+
expect(context.foo.bar()).toBe('baz')
136144
})
137145

138146
actionChain.once('effect', (task) => {
@@ -150,8 +158,8 @@ describe('EFFECTS', () => {
150158
})
151159
test('should track execution of namespaced effects', () => {
152160
expect.assertions(2)
153-
const fn = action().test(({ foo }) => {
154-
expect(foo.nestedProvider.bar()).toBe('baz')
161+
const fn = action().test(({ context }) => {
162+
expect(context.foo.nestedProvider.bar()).toBe('baz')
155163
})
156164

157165
actionChain.once('effect', (task) => {
@@ -169,8 +177,8 @@ describe('EFFECTS', () => {
169177
})
170178
test('should track execution of class instance effects', () => {
171179
expect.assertions(2)
172-
const fn = action().test(({ test }) => {
173-
expect(test.foo()).toBe('bar')
180+
const fn = action().test(({ context }) => {
181+
expect(context.test.foo()).toBe('bar')
174182
})
175183

176184
actionChain.once('effect', (task) => {
@@ -188,8 +196,8 @@ describe('EFFECTS', () => {
188196
})
189197
test('should track execution of constructor instantiation', () => {
190198
expect.assertions(3)
191-
const fn = action().test(({ foo }) => {
192-
const instance = new foo.Constr()
199+
const fn = action().test(({ context }) => {
200+
const instance = new context.foo.Constr()
193201
expect(instance.foo()).toBe('bar')
194202
expect(instance).toBeInstanceOf(TestProvider)
195203
})

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

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,43 +15,56 @@ import {
1515
getOperatorId,
1616
} from './utils'
1717

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

24-
export const selectApp: Mutate<string> = (state, appName) =>
24+
export const selectApp: Mutate<string> = ({ state, value: appName }) =>
2525
(state.currentAppName = appName)
2626

27-
export const setConnecting: Mutate = (state) => {
27+
export const setConnecting: Mutate = ({ state }) => {
2828
state.error = null
2929
state.isConnecting = true
3030
}
3131

32-
export const setPortExists: Mutate = (state) => (state.error = 'PORT_EXISTS')
32+
export const setPortExists: Mutate = ({ state }) =>
33+
(state.error = 'PORT_EXISTS')
3334

34-
export const ensureCurrentApp: Mutate<Message> = (state, message) => {
35+
export const ensureCurrentApp: Mutate<Message> = ({
36+
state,
37+
value: message,
38+
}) => {
3539
if (!state.currentAppName) {
3640
state.currentAppName = message.appName
3741
}
3842
}
3943

40-
export const setError: Mutate<string> = (state, error) => (state.error = error)
44+
export const setError: Mutate<string> = ({ state, value: error }) =>
45+
(state.error = error)
4146

42-
export const setNewPortValue: Mutate<string> = (state, value) =>
47+
export const setNewPortValue: Mutate<string> = ({ state, value }) =>
4348
(state.newPortValue = value)
4449

45-
export const resetNewPortValue: Mutate = (state) => (state.newPortValue = '')
50+
export const resetNewPortValue: Mutate = ({ state }) =>
51+
(state.newPortValue = '')
4652

47-
export const addMessagesFromClient: Mutate<Message> = (state, message) =>
53+
export const addMessagesFromClient: Mutate<Message> = ({
54+
state,
55+
value: message,
56+
}) =>
4857
(state.apps[message.appName].messages = message.messages
4958
.reverse()
5059
.concat(state.apps[message.appName].messages))
5160

52-
export const changeTab: Mutate<Tab> = (state, tab) => (state.currentTab = tab)
61+
export const changeTab: Mutate<Tab> = ({ state, value: tab }) =>
62+
(state.currentTab = tab)
5363

54-
export const toggleExpandStatePath: Mutate<string[]> = (state, path) => {
64+
export const toggleExpandStatePath: Mutate<string[]> = ({
65+
state,
66+
value: path,
67+
}) => {
5568
const pathString = path.join('.')
5669

5770
if (state.expandedStatePaths.indexOf(pathString) >= 0) {
@@ -64,10 +77,10 @@ export const toggleExpandStatePath: Mutate<string[]> = (state, path) => {
6477
}
6578
}
6679

67-
export const performMutationsByMessageType: Mutate<Message> = (
80+
export const performMutationsByMessageType: Mutate<Message> = ({
6881
state,
69-
message
70-
) => {
82+
value: message,
83+
}) => {
7184
message.messages.forEach((clientMessage) => {
7285
if (clientMessage.type === 'PORT_EXISTS') {
7386
state.error = clientMessage.type
@@ -313,7 +326,10 @@ export const performMutationsByMessageType: Mutate<Message> = (
313326
})
314327
}
315328

316-
export const toggleActionItemCollapse: Mutate<string> = (state, actionId) => {
329+
export const toggleActionItemCollapse: Mutate<string> = ({
330+
state,
331+
value: actionId,
332+
}) => {
317333
for (let index in state.currentApp.actionsList) {
318334
const item = state.currentApp.actionsList[index]
319335
if (
@@ -327,21 +343,24 @@ export const toggleActionItemCollapse: Mutate<string> = (state, actionId) => {
327343
}
328344
}
329345

330-
export const selectAction: Mutate<string> = (state, actionId) =>
346+
export const selectAction: Mutate<string> = ({ state, value: actionId }) =>
331347
(state.currentApp.currentActionId = actionId)
332348

333349
type Collapse = {
334350
isCollapsed: boolean
335351
}
336352

337-
export const toggleCollapsed: Mutate<Collapse> = (_, item) => {
353+
export const toggleCollapsed: Mutate<Collapse> = ({ value: item }) => {
338354
item.isCollapsed = !item.isCollapsed
339355
}
340356

341-
export const toggleExpandAllActions: Mutate = (state) =>
357+
export const toggleExpandAllActions: Mutate = ({ state }) =>
342358
(state.expandAllActionDetails = !state.expandAllActionDetails)
343359

344-
export const toggleGroupedComponent: Mutate<string> = (state, name) => {
360+
export const toggleGroupedComponent: Mutate<string> = ({
361+
state,
362+
value: name,
363+
}) => {
345364
const index = state.expandedComponents.indexOf(name)
346365
if (index === -1) {
347366
state.expandedComponents.push(name)

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
11
import { Operation } from 'overmind'
22
import { Message } from './types'
33

4-
export const isPortExistsMessage: Operation.When<Message> = (_, 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

78
export const confirm: (text: string) => Operation.Filter = (text) => ({
8-
utils,
9+
effects: { utils },
910
}) => utils.confirmDialog(text)
1011

1112
export const getCurrentPortFromStorage: Operation.Map<any, Promise<string>> = ({
12-
storage,
13+
effects: { storage },
1314
}) => storage.get<string>('currentPort')
1415

1516
export const getNewPortFromState: Operation.Map<any, string> = ({ state }) =>
1617
state.newPortValue
1718

18-
export const toNumber: Operation.Map<string, string> = (_, value) =>
19+
export const toNumber: Operation.Map<string, string> = ({ value }) =>
1920
String(Number(value))
2021

2122
export const connectCurrentPort: (
2223
action: (message: any) => void
23-
) => Operation.Run = (action) => ({ state, connector }) => {
24+
) => Operation.Run = (action) => ({ state, effects: { connector } }) => {
2425
connector.connect(
2526
state.port,
2627
action

0 commit comments

Comments
 (0)