Skip to content

Commit aa0d044

Browse files
fix(overmind): refactor pipe to handle async and errors better
1 parent 443b80a commit aa0d044

File tree

1 file changed

+16
-29
lines changed
  • packages/node_modules/overmind/src

1 file changed

+16
-29
lines changed

packages/node_modules/overmind/src/index.ts

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -813,32 +813,14 @@ export function pipe(...operators) {
813813
if (err) next(err)
814814
else {
815815
let operatorIndex = 0
816-
let asyncTimeout
817-
const finalClearingAsync = (...args) => {
818-
clearTimeout(asyncTimeout)
819-
final(...args)
820-
}
821-
const run = (runErr, runContext) => {
822-
asyncTimeout = setTimeout(() => {
823-
context.execution.emit(EventType.OPERATOR_ASYNC, {
824-
...runContext.execution,
825-
isAsync: true,
826-
})
827-
})
828816

829-
operators[operatorIndex++](
830-
runErr,
831-
runContext,
832-
runNextOperator,
833-
finalClearingAsync
834-
)
835-
}
817+
const run = (operatorErr, operatorContext) => {
818+
const operator = operators[operatorIndex++]
819+
820+
if (!operator) return next(operatorErr, operatorContext)
836821

837-
const runNextOperator = (operatorError, operatorContext) => {
838-
clearTimeout(asyncTimeout)
839-
if (operatorIndex >= operators.length)
840-
return next(operatorError, operatorContext)
841-
if (operatorError) return run(operatorError, operatorContext)
822+
if (operatorErr)
823+
return operator(operatorErr, operatorContext, run, final)
842824

843825
if (operatorContext.value instanceof Promise) {
844826
context.execution.emit(EventType.OPERATOR_ASYNC, {
@@ -847,21 +829,26 @@ export function pipe(...operators) {
847829
})
848830
operatorContext.value
849831
.then((promiseValue) =>
850-
run(null, { ...operatorContext, value: promiseValue })
832+
operator(
833+
null,
834+
{ ...operatorContext, value: promiseValue },
835+
run,
836+
final
837+
)
851838
)
852839
.catch((promiseError) =>
853-
runNextOperator(promiseError, operatorContext)
840+
operator(promiseError, operatorContext, run, final)
854841
)
855842
} else {
856843
try {
857-
run(null, operatorContext)
844+
operator(null, operatorContext, run, final)
858845
} catch (operatorError) {
859-
runNextOperator(operatorError, operatorContext)
846+
operator(operatorError, operatorContext, run, final)
860847
}
861848
}
862849
}
863850

864-
runNextOperator(null, context)
851+
run(null, context)
865852
}
866853
}
867854
instance[IS_OPERATOR] = true

0 commit comments

Comments
 (0)