Skip to content

Commit 0c74225

Browse files
fix(overmind): fix async handling in operators
1 parent 4a5f6f4 commit 0c74225

File tree

2 files changed

+47
-41
lines changed

2 files changed

+47
-41
lines changed

packages/node_modules/overmind/src/index.ts

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ export class Overmind<ThisConfig extends IConfiguration>
281281
configuration.actions
282282
)
283283
} else {
284+
console.log(location.hostname)
284285
warning +=
285286
'\n\n - You are not running on localhost. You will have to manually define the devtools option to connect'
286287
}
@@ -987,25 +988,10 @@ export function pipe(...operators) {
987988
const run = (operatorErr, operatorContext) => {
988989
const operator = operators[operatorIndex++]
989990

990-
if (!operatorErr && operatorContext.value instanceof Promise) {
991-
operatorContext.value
992-
.then((promiseValue) =>
993-
(operator || next)(
994-
null,
995-
{ ...operatorContext, value: promiseValue },
996-
run,
997-
final
998-
)
999-
)
1000-
.catch((promiseError) =>
1001-
(operator || next)(promiseError, operatorContext, run, final)
1002-
)
1003-
} else {
1004-
try {
1005-
;(operator || next)(operatorErr, operatorContext, run, final)
1006-
} catch (operatorError) {
1007-
;(operator || next)(operatorError, operatorContext, run, final)
1008-
}
991+
try {
992+
;(operator || next)(operatorErr, operatorContext, run, final)
993+
} catch (operatorError) {
994+
;(operator || next)(operatorError, operatorContext, run, final)
1009995
}
1010996
}
1011997

packages/node_modules/overmind/src/operator.ts

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -144,24 +144,34 @@ export function createOperator<ThisConfig extends IConfiguration>(
144144
},
145145
context.value,
146146
(err, value, options = {}) => {
147-
clearTimeout(asyncTimeout)
148-
if (options.path) {
149-
const newContext = createContext(
150-
context,
151-
value,
152-
context.execution.path &&
153-
context.execution.path.concat(options.path.name)
154-
)
155-
const nextWithPath = createNextPath(next)
156-
options.path.operator(err, newContext, (...args) => {
157-
operatorStopped(context, args[1].value)
158-
nextWithPath(...args)
159-
})
147+
function run(err, value) {
148+
if (options.path) {
149+
const newContext = createContext(
150+
context,
151+
value,
152+
context.execution.path &&
153+
context.execution.path.concat(options.path.name)
154+
)
155+
const nextWithPath = createNextPath(next)
156+
options.path.operator(err, newContext, (...args) => {
157+
operatorStopped(context, args[1].value)
158+
nextWithPath(...args)
159+
})
160+
} else {
161+
operatorStopped(context, err || value, {
162+
isSkipped: err ? true : options.isSkipped,
163+
})
164+
next(err, createContext(context, value))
165+
}
166+
}
167+
168+
if (value && value instanceof Promise) {
169+
value
170+
.then((promiseValue) => run(err, promiseValue))
171+
.catch((promiseError) => run(promiseError, promiseError))
160172
} else {
161-
operatorStopped(context, err || value, {
162-
isSkipped: err ? true : options.isSkipped,
163-
})
164-
next(err, createContext(context, value))
173+
clearTimeout(asyncTimeout)
174+
run(err, value)
165175
}
166176
},
167177
(err, value) => {
@@ -234,11 +244,21 @@ export function createMutationOperator<ThisConfig extends IConfiguration>(
234244
? context.value
235245
: context.execution.scopeValue(context.value, mutationTree),
236246
(err, value, options = {}) => {
237-
clearTimeout(asyncTimeout)
238-
operatorStopped(context, err || value, {
239-
isSkipped: err ? true : options.isSkipped,
240-
})
241-
next(err, createContext(context, value))
247+
function run(err, value) {
248+
operatorStopped(context, err || value, {
249+
isSkipped: err ? true : options.isSkipped,
250+
})
251+
next(err, createContext(context, value))
252+
}
253+
254+
if (value && value instanceof Promise) {
255+
value
256+
.then((promiseValue) => run(err, promiseValue))
257+
.catch((promiseError) => run(promiseError, promiseError))
258+
} else {
259+
clearTimeout(asyncTimeout)
260+
run(err, value)
261+
}
242262
},
243263
(err, value) => {
244264
clearTimeout(asyncTimeout)

0 commit comments

Comments
 (0)