Skip to content

Commit

Permalink
fix(overmind): fix async handling in operators
Browse files Browse the repository at this point in the history
  • Loading branch information
christianalfoni committed Apr 3, 2019
1 parent 4a5f6f4 commit 0c74225
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 41 deletions.
24 changes: 5 additions & 19 deletions packages/node_modules/overmind/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ export class Overmind<ThisConfig extends IConfiguration>
configuration.actions
)
} else {
console.log(location.hostname)
warning +=
'\n\n - You are not running on localhost. You will have to manually define the devtools option to connect'
}
Expand Down Expand Up @@ -987,25 +988,10 @@ export function pipe(...operators) {
const run = (operatorErr, operatorContext) => {
const operator = operators[operatorIndex++]

if (!operatorErr && operatorContext.value instanceof Promise) {
operatorContext.value
.then((promiseValue) =>
(operator || next)(
null,
{ ...operatorContext, value: promiseValue },
run,
final
)
)
.catch((promiseError) =>
(operator || next)(promiseError, operatorContext, run, final)
)
} else {
try {
;(operator || next)(operatorErr, operatorContext, run, final)
} catch (operatorError) {
;(operator || next)(operatorError, operatorContext, run, final)
}
try {
;(operator || next)(operatorErr, operatorContext, run, final)
} catch (operatorError) {
;(operator || next)(operatorError, operatorContext, run, final)
}
}

Expand Down
64 changes: 42 additions & 22 deletions packages/node_modules/overmind/src/operator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,24 +144,34 @@ export function createOperator<ThisConfig extends IConfiguration>(
},
context.value,
(err, value, options = {}) => {
clearTimeout(asyncTimeout)
if (options.path) {
const newContext = createContext(
context,
value,
context.execution.path &&
context.execution.path.concat(options.path.name)
)
const nextWithPath = createNextPath(next)
options.path.operator(err, newContext, (...args) => {
operatorStopped(context, args[1].value)
nextWithPath(...args)
})
function run(err, value) {
if (options.path) {
const newContext = createContext(
context,
value,
context.execution.path &&
context.execution.path.concat(options.path.name)
)
const nextWithPath = createNextPath(next)
options.path.operator(err, newContext, (...args) => {
operatorStopped(context, args[1].value)
nextWithPath(...args)
})
} else {
operatorStopped(context, err || value, {
isSkipped: err ? true : options.isSkipped,
})
next(err, createContext(context, value))
}
}

if (value && value instanceof Promise) {
value
.then((promiseValue) => run(err, promiseValue))
.catch((promiseError) => run(promiseError, promiseError))
} else {
operatorStopped(context, err || value, {
isSkipped: err ? true : options.isSkipped,
})
next(err, createContext(context, value))
clearTimeout(asyncTimeout)
run(err, value)
}
},
(err, value) => {
Expand Down Expand Up @@ -234,11 +244,21 @@ export function createMutationOperator<ThisConfig extends IConfiguration>(
? context.value
: context.execution.scopeValue(context.value, mutationTree),
(err, value, options = {}) => {
clearTimeout(asyncTimeout)
operatorStopped(context, err || value, {
isSkipped: err ? true : options.isSkipped,
})
next(err, createContext(context, value))
function run(err, value) {
operatorStopped(context, err || value, {
isSkipped: err ? true : options.isSkipped,
})
next(err, createContext(context, value))
}

if (value && value instanceof Promise) {
value
.then((promiseValue) => run(err, promiseValue))
.catch((promiseError) => run(promiseError, promiseError))
} else {
clearTimeout(asyncTimeout)
run(err, value)
}
},
(err, value) => {
clearTimeout(asyncTimeout)
Expand Down

0 comments on commit 0c74225

Please sign in to comment.