Skip to content

Commit 79a4b07

Browse files
feat(overmind): deprecate action for mutate and fix devtool bugs
1 parent df1d6cf commit 79a4b07

File tree

4 files changed

+94
-47
lines changed

4 files changed

+94
-47
lines changed

packages/node_modules/overmind-devtools/src/components/Action/index.tsx

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,28 +25,34 @@ const Action: FunctionComponent = () => {
2525
const traversePath = operator.path.slice()
2626
traversePath.unshift('')
2727
traversePath.reduce((children, key, index) => {
28+
const isLastKey = index === traversePath.length - 1
2829
let child = children.reduce(
2930
(hit, child) => (child.path === key ? child : hit),
3031
null
3132
)
3233

33-
if (index === traversePath.length - 1) {
34+
if (isLastKey) {
3435
if (child && !child.children.length) {
3536
child.operators.push(operator)
3637
} else {
3738
child = {
3839
path: key,
40+
parent: child,
3941
operators: [operator],
4042
children: [],
41-
value: child
42-
? child.operators[child.operators.length - 1].result
43-
: currentValue,
43+
value:
44+
child && child.operators.length
45+
? child.operators[child.operators.length - 1].result
46+
: child
47+
? child.value
48+
: currentValue,
4449
}
4550
children.push(child)
4651
}
47-
} else {
48-
currentValue = child.value
4952
}
53+
54+
currentValue = child.value
55+
5056
return child.children
5157
}, aggr)
5258

@@ -103,6 +109,7 @@ const Action: FunctionComponent = () => {
103109
}
104110

105111
console.log(operatorsByPath)
112+
106113
return (
107114
<div className={styles.wrapper}>
108115
{renderOperators(operatorsByPath)}

packages/node_modules/overmind/src/index.ts

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,10 @@ export class Overmind<ThisConfig extends IConfiguration>
394394
emit: this.eventHub.emit.bind(this.eventHub),
395395
send: this.devtools ? this.devtools.send.bind(this.devtools) : () => {},
396396
trackEffects: this.trackEffects.bind(this, this.effects),
397+
getNextOperatorId: (() => {
398+
let currentOperatorId = 0
399+
return () => ++currentOperatorId
400+
})(),
397401
flush: () => {
398402
return this.proxyStateTree.flush(mutationTrees)
399403
},
@@ -966,7 +970,17 @@ export function forEach<
966970

967971
if (!evaluatingCount) {
968972
stopDebugOperator(context, context.value)
969-
next(null, createContext(context, context.value))
973+
next(
974+
null,
975+
createContext(
976+
lastContext,
977+
context.value,
978+
lastContext.execution.path.slice(
979+
0,
980+
lastContext.execution.path.length - 1
981+
)
982+
)
983+
)
970984
}
971985
}
972986
startDebugOperator('forEach', '', context)
@@ -1015,7 +1029,17 @@ export function parallel<Input, ThisConfig extends IConfiguration = Config>(
10151029

10161030
if (!evaluatingCount) {
10171031
stopDebugOperator(context, context.value)
1018-
next(null, createContext(context, context.value))
1032+
next(
1033+
null,
1034+
createContext(
1035+
lastContext,
1036+
context.value,
1037+
lastContext.execution.path.slice(
1038+
0,
1039+
lastContext.execution.path.length - 1
1040+
)
1041+
)
1042+
)
10191043
}
10201044
}
10211045
startDebugOperator('parallel', '', context)
@@ -1067,6 +1091,9 @@ export function filter<Input, ThisConfig extends IConfiguration = Config>(
10671091
export function action<Input, ThisConfig extends IConfiguration = Config>(
10681092
operation: (context: IContext<ThisConfig>, value: Input) => void
10691093
): IOperator<ThisConfig, Input, Input> {
1094+
console.warn(
1095+
'DEPRECATION - The action operator is deprecated in favor of "mutate". The reason is to avoid confusion between actions and operators.'
1096+
)
10701097
return createMutationOperator<ThisConfig>(
10711098
'action',
10721099
getFunctionName(operation),
@@ -1085,6 +1112,27 @@ export function action<Input, ThisConfig extends IConfiguration = Config>(
10851112
)
10861113
}
10871114

1115+
export function mutate<Input, ThisConfig extends IConfiguration = Config>(
1116+
operation: (context: IContext<ThisConfig>, value: Input) => void
1117+
): IOperator<ThisConfig, Input, Input> {
1118+
return createMutationOperator<ThisConfig>(
1119+
'mutate',
1120+
getFunctionName(operation),
1121+
(err, context, value, next) => {
1122+
if (err) next(err, value)
1123+
else {
1124+
const result = operation(context, value) as any
1125+
1126+
if (result instanceof Promise) {
1127+
next(null, result.then(() => value))
1128+
} else {
1129+
next(null, value)
1130+
}
1131+
}
1132+
}
1133+
)
1134+
}
1135+
10881136
export function catchError<Input, ThisConfig extends IConfiguration = Config>(
10891137
operation: (context: IContext<ThisConfig>, value: Error) => Input
10901138
): IOperator<ThisConfig, Input, Input> {

packages/node_modules/overmind/src/operator.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export function createContext(context, value, path?) {
5858

5959
const newExecution = {
6060
...context.execution,
61-
operatorId: context.execution.operatorId + 1,
61+
operatorId: context.execution.getNextOperatorId(),
6262
path: path || context.execution.path,
6363
}
6464

@@ -125,8 +125,10 @@ export function createOperator<ThisConfig extends IConfiguration>(
125125
context.execution.path && context.execution.path.concat(path.name)
126126
)
127127
const nextWithPath = createNextPath(next)
128-
stopDebugOperator(context, value)
129-
path.operator(err, newContext, nextWithPath)
128+
path.operator(err, newContext, (...args) => {
129+
stopDebugOperator(context, args[1].value)
130+
nextWithPath(...args)
131+
})
130132
} else {
131133
stopDebugOperator(context, value)
132134
next(err, createContext(context, value))

packages/overmind-website/src/overmind/actions.ts

Lines changed: 26 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import {
99
when,
1010
fork,
1111
parallel,
12+
catchError,
13+
map,
1214
} from 'overmind'
1315
import { Page, RouteContext, GuideParams, VideoParams } from './types'
1416
import { ensureViewAndTypescript } from './operators'
@@ -113,60 +115,48 @@ export const viewHelpGotIt: Action = ({ state }) => {
113115
state.showViewHelp = false
114116
}
115117

116-
export const test: Operator = pipe(
118+
export const test: Operator<string> = pipe(
117119
when(
118120
function isTrue() {
119121
return true
120122
},
121123
{
122124
true: pipe(
123-
action(function truePath({ state }) {
124-
state.test = 'truePath'
125+
map(function truePath({ state }) {
126+
return 'truePath'
125127
}),
126-
action(function truePath2({ state }) {
127-
state.test = 'truePath2'
128+
map(function truePath2({ state }) {
129+
return 'truePath2'
128130
}),
129131
fork(() => 'foo', {
130-
foo: action(function fooPath({ state }) {
131-
state.test = 'fooPath'
132-
}),
132+
foo: pipe(
133+
map(function fooPath({ state }) {
134+
return 'fooPath'
135+
}),
136+
map(function fooPath2() {
137+
return 'fooPath2'
138+
})
139+
),
133140
})
134141
),
135142
false: action(function falsePath() {}),
136143
}
137144
),
138145
parallel(
139146
pipe(
140-
action(function pa1({ state }) {
141-
state.test = 'endStuff'
142-
state.test = 'endStuff'
143-
state.test = 'endStuff'
144-
state.test = 'endStuff'
147+
map(function pa1({ state }, val) {
148+
console.log('VAL', val)
149+
return 'pa1'
145150
}),
146-
action(function pa2({ state }) {
147-
state.test = 'endStuff'
148-
state.test = 'endStuff'
149-
state.test = 'endStuff'
150-
state.test = 'endStuff'
151+
map(function pa2({ state }) {
152+
return 'pa2'
151153
})
152154
),
153-
action(function endStuff({ state }) {
154-
state.test = 'endStuff'
155-
state.test = 'endStuff'
156-
state.test = 'endStuff'
157-
state.test = 'endStuff'
158-
}),
159-
action(function endStuff({ state }) {
160-
state.test = 'endStuff'
161-
state.test = 'endStuff'
162-
state.test = 'endStuff'
163-
state.test = 'endStuff'
164-
}),
165-
action(function endStuff({ state }) {
166-
state.test = 'endStuff'
167-
state.test = 'endStuff'
168-
state.test = 'endStuff'
169-
state.test = 'endStuff'
155+
map(function endStuff({ state }) {
156+
return 'endStuff'
170157
})
171-
)
158+
),
159+
action(function catchError() {
160+
console.log('NOW I RUN CATCH!')
161+
})
172162
)

0 commit comments

Comments
 (0)