Skip to content

Commit 37526ba

Browse files
feat(overmind): make all operators an action
1 parent 08656f4 commit 37526ba

File tree

1 file changed

+36
-9
lines changed
  • packages/node_modules/overmind/src

1 file changed

+36
-9
lines changed

packages/node_modules/overmind/src/index.ts

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,7 @@ function startDebugOperator(type, operationName, execution) {
559559
export function map<Input, Output>(
560560
operation: (input: Context<Input>) => Output
561561
): Operator<Input, Output extends Promise<infer U> ? U : Output> {
562-
return (err, context, next) => {
562+
const instance = (err, context, next) => {
563563
if (err) next(err)
564564
else {
565565
const stopDebugOperator = startDebugOperator(
@@ -572,12 +572,15 @@ export function map<Input, Output>(
572572
next(null, { ...context, value } as any)
573573
}
574574
}
575+
instance[IS_PIPE] = true
576+
577+
return instance
575578
}
576579

577580
export function run<Input>(
578581
operation: (input: Context<Input>) => void
579582
): Operator<Input, Input> {
580-
return (err, context, next) => {
583+
const instance = (err, context, next) => {
581584
if (err) next(err)
582585
else {
583586
const stopDebugOperator = startDebugOperator(
@@ -590,12 +593,15 @@ export function run<Input>(
590593
next(null, context as any)
591594
}
592595
}
596+
instance[IS_PIPE] = true
597+
598+
return instance
593599
}
594600

595601
export function forEach<Input extends any[]>(
596602
forEachItemOperator: Operator<Input[0], any>
597603
): Operator<Input, Input> {
598-
return (err, context, next) => {
604+
const instance = (err, context, next) => {
599605
if (err) next(err)
600606
else {
601607
const stopDebugOperator = startDebugOperator(
@@ -627,22 +633,28 @@ export function forEach<Input extends any[]>(
627633
)
628634
}
629635
}
636+
instance[IS_PIPE] = true
637+
638+
return instance
630639
}
631640

632641
export function filter<Input>(
633642
operation: (input: Context<Input>) => boolean
634643
): Operator<Input, Input> {
635-
return (err, context, next, final) => {
644+
const instance = (err, context, next, final) => {
636645
if (err) next(err)
637646
else if (operation(context)) next(null, context)
638647
else final(null, context)
639648
}
649+
instance[IS_PIPE] = true
650+
651+
return instance
640652
}
641653

642654
export function mutate<Input>(
643655
operation: (input: Context<Input>) => void
644656
): Operator<Input, Input> {
645-
return (err, context, next) => {
657+
const instance = (err, context, next) => {
646658
if (err) next(err)
647659
else {
648660
const operatorId = context.execution.operatorId
@@ -663,6 +675,9 @@ export function mutate<Input>(
663675
next(null, context)
664676
}
665677
}
678+
instance[IS_PIPE] = true
679+
680+
return instance
666681
}
667682

668683
export function fork<
@@ -672,13 +687,16 @@ export function fork<
672687
operation: (input: Context<Input>) => keyof Paths,
673688
paths: Paths
674689
): Operator<Input, Input> {
675-
return (err, context, next) => {
690+
const instance = (err, context, next) => {
676691
if (err) return next(err)
677692
paths[operation(context)](null, context, (err) => {
678693
if (err) next(err)
679694
else next(null, context)
680695
})
681696
}
697+
instance[IS_PIPE] = true
698+
699+
return instance
682700
}
683701

684702
export function when<Input, OutputA, OutputB>(
@@ -688,24 +706,30 @@ export function when<Input, OutputA, OutputB>(
688706
false: Operator<Input, OutputB>
689707
}
690708
): Operator<Input, OutputA | OutputB> {
691-
return (err, context, next) => {
709+
const instance = (err, context, next) => {
692710
if (err) next(err)
693711
else if (operation(context)) paths.true(null, context, next)
694712
else paths.false(null, context, next)
695713
}
714+
instance[IS_PIPE] = true
715+
716+
return instance
696717
}
697718

698719
export function wait<Input>(ms: number): Operator<Input, Input> {
699-
return (err, context, next) => {
720+
const instance = (err, context, next) => {
700721
if (err) next(err)
701722
else setTimeout(() => next(null, context), ms)
702723
}
724+
instance[IS_PIPE] = true
725+
726+
return instance
703727
}
704728

705729
export function debounce<Input>(ms: number): Operator<Input, Input> {
706730
let timeout
707731
let previousFinal
708-
return (err, value, next, final) => {
732+
const instance = (err, value, next, final) => {
709733
if (err) {
710734
return next(err)
711735
}
@@ -719,4 +743,7 @@ export function debounce<Input>(ms: number): Operator<Input, Input> {
719743
next(null, value)
720744
}, ms)
721745
}
746+
instance[IS_PIPE] = true
747+
748+
return instance
722749
}

0 commit comments

Comments
 (0)