Skip to content

Commit 9b60841

Browse files
Merge pull request cerebral#32 from cerebral/flipValueContext
refactor(overmind): flip the value and context arguments of operators
2 parents d38c492 + ad9f235 commit 9b60841

File tree

12 files changed

+79
-68
lines changed

12 files changed

+79
-68
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
export const getEventValue = (event: React.ChangeEvent<HTMLInputElement>) =>
1+
export const getEventValue = (_, event: React.ChangeEvent<HTMLInputElement>) =>
22
event.currentTarget.value
33

4-
export const preventEventDefault = (event: React.FormEvent) =>
4+
export const preventEventDefault = (_, event: React.FormEvent) =>
55
event.preventDefault()

packages/demos/todomvc/src/app/mutations.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ import { State, Todo } from './state'
22

33
let nextTodoId = 0
44

5-
export const setNewTodoTitle = (value: string, state: State) =>
5+
export const setNewTodoTitle = (state: State, value: string) =>
66
(state.newTodoTitle = value)
77

8-
export const addTodo = (_, state: State) =>
8+
export const addTodo = (state: State) =>
99
state.todos.unshift({
1010
id: String(nextTodoId++),
1111
title: state.newTodoTitle,
1212
completed: false,
1313
})
1414

15-
export const clearNewTodoTitle = (_, state: State) => (state.newTodoTitle = '')
15+
export const clearNewTodoTitle = (state: State) => (state.newTodoTitle = '')
1616

17-
export const toggleCompleted = (todo: Todo) =>
17+
export const toggleCompleted = (_, todo: Todo) =>
1818
(todo.completed = !todo.completed)

packages/node_modules/action-chain/src/ActionBase.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ export class ActionBase<Context, InitialValue, Value = InitialValue> {
114114
...context.__execution,
115115
operatorId,
116116
})
117-
const result = cb(currentValue, context)
117+
const result = cb(context, currentValue)
118118

119119
if (result instanceof Promise) {
120120
this.actionChain.emit('operator:async', {

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ class Action<Context, InitialValue, Value = InitialValue> extends ActionBase<
1616
Value
1717
> {
1818
test: (
19-
cb: (value: Value, context: Context) => void
19+
cb: (context: Context, value: Value) => void
2020
) => InitialValue extends void
2121
? INoValueAction<Context, InitialValue, Value>
2222
: IAction<Context, InitialValue, Value> = (cb) => {
23-
const operator = (value, context) => {
24-
return cb(value, context)
23+
const operator = (context, value) => {
24+
return cb(context, value)
2525
}
2626

2727
const [chain, initialActionId, runOperators] = this.createOperatorResult(
@@ -37,7 +37,7 @@ class Action<Context, InitialValue, Value = InitialValue> extends ActionBase<
3737
) => InitialValue extends void
3838
? INoValueAction<Context, InitialValue, Value>
3939
: IAction<Context, InitialValue, Value> = (action) => {
40-
const operator = (value, context) => {
40+
const operator = (context, value) => {
4141
return (action as any)(value, context, 'fork')
4242
}
4343

@@ -93,7 +93,7 @@ describe('CONTEXT', () => {
9393
expect.assertions(2)
9494
const wut = action()
9595
console.log(wut.test)
96-
const test = action().test((_, { __execution, __path }: any) => {
96+
const test = action().test(({ __execution, __path }: any) => {
9797
expect(__execution).toBeTruthy()
9898
expect(__path).toBeTruthy()
9999
})
@@ -103,7 +103,7 @@ describe('CONTEXT', () => {
103103

104104
test('should be able to extend context', () => {
105105
expect.assertions(1)
106-
const fn = action().test((_, { foo }) => {
106+
const fn = action().test(({ foo }) => {
107107
expect(foo.bar()).toBe('baz')
108108
})
109109

@@ -114,7 +114,7 @@ describe('CONTEXT', () => {
114114
describe('PROVIDER', () => {
115115
test('should track execution of providers', () => {
116116
expect.assertions(2)
117-
const fn = action().test((_, { foo }) => {
117+
const fn = action().test(({ foo }) => {
118118
expect(foo.bar()).toBe('baz')
119119
})
120120

@@ -132,7 +132,7 @@ describe('PROVIDER', () => {
132132
})
133133
test('should track execution of clas instance providers', () => {
134134
expect.assertions(2)
135-
const fn = action().test((_, { test }) => {
135+
const fn = action().test(({ test }) => {
136136
expect(test.foo()).toBe('bar')
137137
})
138138

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
import { Context } from './'
22

3-
export const getAppsFromStorage = (_, { storage }: Context) =>
4-
storage.get('apps')
3+
export const getAppsFromStorage = ({ storage }: Context) => storage.get('apps')
54

6-
export const getCurrentPortFromStorage = (_, { storage }: Context) =>
5+
export const getCurrentPortFromStorage = ({ storage }: Context) =>
76
storage.get('currentPort')
87

9-
export const getNewPortFromState = (_, { state }: Context) => state.newPortValue
8+
export const getNewPortFromState = ({ state }: Context) => state.newPortValue
109

11-
export const storeApps = (_, { storage, state }: Context) =>
10+
export const storeApps = ({ storage, state }: Context) =>
1211
storage.set('apps', state.apps)
1312

14-
export const toNumber = (value: string) => String(Number(value))
13+
export const toNumber = (_, value: string) => String(Number(value))
1514

16-
export const connectCurrentPort = (action: (message: any) => void) => (
17-
_,
18-
{ state, connector }: Context
19-
) => connector.addPort(state.currentPort, action)
15+
export const connectCurrentPort = (action: (message: any) => void) => ({
16+
state,
17+
connector,
18+
}: Context) => connector.addPort(state.currentPort, action)

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

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
import { Apps, State, Message, Tab } from './state'
22
import { runMutation } from './utils'
33

4-
export const setApps = (apps: Apps, state: State) => (state.apps = apps || {})
4+
export const setApps = (state: State, apps: Apps) => (state.apps = apps || {})
55

6-
export const setCurrentPort = (currentPort: string, state: State) => {
6+
export const setCurrentPort = (state: State, currentPort: string) => {
77
if (currentPort) {
88
state.currentPort = currentPort
99
} else if (Object.keys(state.apps).length) {
1010
state.currentPort = Object.keys(state.apps)[0]
1111
}
1212
}
1313

14-
export const setError = (error: string, state: State) => (state.error = error)
14+
export const setError = (state: State, error: string) => (state.error = error)
1515

16-
export const setAppLoaded = (_, state: State) => (state.isLoading = false)
16+
export const setAppLoaded = (state: State) => (state.isLoading = false)
1717

18-
export const setNewPortValue = (value: string, state: State) =>
18+
export const setNewPortValue = (state: State, value: string) =>
1919
(state.newPortValue = value)
2020

21-
export const addNewApp = (_, state: State) =>
21+
export const addNewApp = (state: State) =>
2222
(state.apps[state.newPortValue] = {
2323
name: null,
2424
port: state.newPortValue,
@@ -33,17 +33,17 @@ export const addNewApp = (_, state: State) =>
3333
],
3434
})
3535

36-
export const resetNewPortValue = (_, state: State) => (state.newPortValue = '')
36+
export const resetNewPortValue = (state: State) => (state.newPortValue = '')
3737

38-
export const addMessagesFromClient = (message: Message, state: State) => {
38+
export const addMessagesFromClient = (state: State, message: Message) => {
3939
state.apps[message.port].messages = state.apps[message.port].messages.concat(
4040
message.message
4141
)
4242
}
4343

44-
export const changeTab = (tab: Tab, state: State) => (state.currentTab = tab)
44+
export const changeTab = (state: State, tab: Tab) => (state.currentTab = tab)
4545

46-
export const toggleExpandStatePath = (path: string[], state: State) => {
46+
export const toggleExpandStatePath = (state: State, path: string[]) => {
4747
const pathString = path.join('.')
4848

4949
if (state.expandedStatePaths.indexOf(pathString) >= 0) {
@@ -57,13 +57,25 @@ export const toggleExpandStatePath = (path: string[], state: State) => {
5757
}
5858

5959
export const performMutationsByMessageType = (
60-
message: Message,
61-
state: State
60+
state: State,
61+
message: Message
6262
) => {
6363
message.message.forEach((clientMessage) => {
6464
switch (clientMessage.type) {
6565
case 'init': {
66-
state.apps[message.port].state = clientMessage.data.state
66+
state.apps[message.port] = {
67+
name: '',
68+
port: message.port,
69+
state: clientMessage.data.state,
70+
messages: [],
71+
components: {},
72+
flushes: [
73+
{
74+
mutations: [],
75+
components: [],
76+
},
77+
],
78+
}
6779
break
6880
}
6981
case 'flush': {

packages/node_modules/overmind/src/Action.ts

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import ProxyStateTree from 'proxy-state-tree'
22
import { ActionBase, StopExecution } from 'action-chain'
33

44
type OperatorCallback<Context, Value, NewValue = Value> = (
5-
value: Value,
6-
context: Context
5+
context: Context,
6+
value: Value
77
) => NewValue | Promise<NewValue>
88

99
export interface IValueAction<
@@ -36,13 +36,13 @@ export default class Action<
3636
this.proxyStateTree = proxyStateTree
3737
}
3838
fork: <Paths>(
39-
cb: (value: Value, context: Context) => keyof Paths,
39+
cb: (context: Context, value: Value) => keyof Paths,
4040
paths: Paths
4141
) => [InitialValue] extends [void]
4242
? INoValueAction<State, Context, InitialValue, Value>
4343
: IValueAction<State, Context, InitialValue, Value> = (cb, paths) => {
44-
const operator = (value, context) => {
45-
const path = cb(value, context)
44+
const operator = (context, value) => {
45+
const path = cb(context, value)
4646

4747
return (paths[path] as any).map(() => value)(value, context, path)
4848
}
@@ -60,13 +60,13 @@ export default class Action<
6060
) as any
6161
}
6262
mutation: (
63-
cb: (value: Value, state: State) => any
63+
cb: (state: State, value: Value) => any
6464
) => [InitialValue] extends [void]
6565
? INoValueAction<State, Context, InitialValue, Value>
6666
: IValueAction<State, Context, InitialValue, Value> = (cb) => {
67-
const operator = (value, context) => {
67+
const operator = (context, value) => {
6868
this.proxyStateTree.startMutationTracking()
69-
cb(value, context.state)
69+
cb(context.state, value)
7070
const mutations = this.proxyStateTree.clearMutationTracking()
7171
this.getActionChain().emit('mutations', {
7272
mutations,
@@ -90,12 +90,12 @@ export default class Action<
9090
) as any
9191
}
9292
do: (
93-
cb: (value: Value, context: Context) => void
93+
cb: (context: Context, value: Value) => void
9494
) => [InitialValue] extends [void]
9595
? INoValueAction<State, Context, InitialValue, Value>
9696
: IValueAction<State, Context, InitialValue, Value> = (cb) => {
97-
const operator = (value, context) => {
98-
cb(value, context)
97+
const operator = (context, value) => {
98+
cb(context, value)
9999
return value
100100
}
101101

@@ -113,7 +113,7 @@ export default class Action<
113113
) as any
114114
}
115115
map: <NewValue>(
116-
cb: (value: Value, context: Context) => NewValue | Promise<NewValue>
116+
cb: (context: Context, value: Value) => NewValue | Promise<NewValue>
117117
) => [InitialValue] extends [void]
118118
? INoValueAction<State, Context, InitialValue, NewValue>
119119
: IValueAction<State, Context, InitialValue, NewValue> = (cb) => {
@@ -156,8 +156,8 @@ export default class Action<
156156
cb,
157157
paths
158158
) => {
159-
const operator = (value, context) => {
160-
return (cb(value, context) as any)
159+
const operator = (context, value) => {
160+
return (cb(context, value) as any)
161161
.then((promiseValue) => {
162162
return (paths.success as any)(promiseValue, context, 'success')
163163
})
@@ -179,7 +179,7 @@ export default class Action<
179179
) as any
180180
}
181181
when: <TrueValue, FalseValue>(
182-
cb: (value: Value, context: Context) => boolean,
182+
cb: (context: Context, value: Value) => boolean,
183183
paths: {
184184
true: Action<State, Context, Value, TrueValue>
185185
false: Action<State, Context, Value, FalseValue>
@@ -190,8 +190,8 @@ export default class Action<
190190
cb,
191191
paths
192192
) => {
193-
const operator = (value, context) => {
194-
const isTrue = cb(value, context)
193+
const operator = (context, value) => {
194+
const isTrue = cb(context, value)
195195
const path = isTrue ? paths.true : (paths.false as any)
196196

197197
return path(value, context, isTrue ? 'true' : 'false')
@@ -210,12 +210,12 @@ export default class Action<
210210
) as any
211211
}
212212
filter: (
213-
cb: (value: Value, context: Context) => boolean
213+
cb: (context: Context, value: Value) => boolean
214214
) => [InitialValue] extends [void]
215215
? INoValueAction<State, Context, InitialValue, Value>
216216
: IValueAction<State, Context, InitialValue, Value> = (cb) => {
217-
const operator = (value, context) => {
218-
const result = cb(value, context)
217+
const operator = (context, value) => {
218+
const result = cb(context, value)
219219

220220
if (result === true) {
221221
return value
@@ -243,7 +243,7 @@ export default class Action<
243243
: IValueAction<State, Context, InitialValue, Value> = (timer) => {
244244
let currentTimeout = null
245245

246-
const operator = (value) => {
246+
const operator = (_, value) => {
247247
return new Promise((resolve) => {
248248
if (currentTimeout) {
249249
currentTimeout()

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ describe('Computed', () => {
7171
const app = new App({
7272
state,
7373
actions: (action) => ({
74-
changeFoo: action().mutation((_, state) => (state.foo = 'bar2')),
74+
changeFoo: action().mutation((state) => (state.foo = 'bar2')),
7575
}),
7676
})
7777
app.state.test(123)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ describe('Derived', () => {
2323
upperFoo: derived((state: State) => state.foo.toUpperCase()),
2424
},
2525
actions: (action) => ({
26-
changeFoo: action().mutation((_, state) => (state.foo = 'bar2')),
26+
changeFoo: action().mutation((state) => (state.foo = 'bar2')),
2727
}),
2828
})
2929
function render() {

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ describe('Overmind', () => {
6363
state,
6464
actions: (action: Action) => ({
6565
foo: action<string>().map(
66-
(value, { state }) => value + state[namespace].foo
66+
({ state }, value) => value + state[namespace].foo
6767
),
6868
}),
6969
}
@@ -104,7 +104,7 @@ describe('OPERATORS', () => {
104104
foo: 'bar',
105105
},
106106
actions: (action) => ({
107-
doThis: action().mutation((_, state) => (state.foo = 'bar2')),
107+
doThis: action().mutation((state) => (state.foo = 'bar2')),
108108
}),
109109
})
110110

@@ -124,7 +124,7 @@ describe('OPERATORS', () => {
124124
},
125125
},
126126
actions: (action) => ({
127-
doThis: action<string>().do((_, { foo }) => {
127+
doThis: action<string>().do(({ foo }) => {
128128
expect(foo.bar()).toBe('baz')
129129
}),
130130
}),
@@ -136,7 +136,7 @@ describe('OPERATORS', () => {
136136
const app = new App({
137137
state: {},
138138
actions: (action) => ({
139-
doThis: action<string>().map((value) => {
139+
doThis: action<string>().map((_, value) => {
140140
return value.toUpperCase()
141141
}),
142142
}),

0 commit comments

Comments
 (0)