Skip to content

Commit

Permalink
Changing the HaltStrategy.match function signature (Effect-TS#2202)
Browse files Browse the repository at this point in the history
Co-authored-by: maksim.khramtsov <[email protected]>
  • Loading branch information
KhraksMamtsov and maksim.khramtsov authored Feb 27, 2024
1 parent a192473 commit 52ec4ca
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 30 deletions.
5 changes: 5 additions & 0 deletions .changeset/real-kangaroos-push.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": minor
---

The signatures of the `HaltStrategy.match` `StreamHaltStrategy.match` functions have been changed to the generally accepted ones
12 changes: 10 additions & 2 deletions packages/effect/src/ExecutionStrategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,14 @@ export const isParallelN: (self: ExecutionStrategy) => self is ParallelN = inter
* @category folding
*/
export const match: {
<A>(onSequential: LazyArg<A>, onParallel: LazyArg<A>, onParallelN: (n: number) => A): (self: ExecutionStrategy) => A
<A>(self: ExecutionStrategy, onSequential: LazyArg<A>, onParallel: LazyArg<A>, onParallelN: (n: number) => A): A
<A>(options: {
readonly onSequential: LazyArg<A>
readonly onParallel: LazyArg<A>
readonly onParallelN: (n: number) => A
}): (self: ExecutionStrategy) => A
<A>(self: ExecutionStrategy, options: {
readonly onSequential: LazyArg<A>
readonly onParallel: LazyArg<A>
readonly onParallelN: (n: number) => A
}): A
} = internal.match
16 changes: 14 additions & 2 deletions packages/effect/src/StreamHaltStrategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,22 @@ export const isBoth: (self: HaltStrategy) => self is Both = internal.isBoth
export const isEither: (self: HaltStrategy) => self is Either = internal.isEither

/**
* Folds over the specified `HaltStrategy` using the provided case functions.
*
* @since 2.0.0
* @category folding
*/
export const match: {
<Z>(onLeft: () => Z, onRight: () => Z, onBoth: () => Z, onEither: () => Z): (self: HaltStrategy) => Z
<Z>(self: HaltStrategy, onLeft: () => Z, onRight: () => Z, onBoth: () => Z, onEither: () => Z): Z
<Z>(options: {
readonly onLeft: () => Z
readonly onRight: () => Z
readonly onBoth: () => Z
readonly onEither: () => Z
}): (self: HaltStrategy) => Z
<Z>(self: HaltStrategy, options: {
readonly onLeft: () => Z
readonly onRight: () => Z
readonly onBoth: () => Z
readonly onEither: () => Z
}): Z
} = internal.match
26 changes: 14 additions & 12 deletions packages/effect/src/internal/executionStrategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,27 +46,29 @@ export const isParallelN = (self: ExecutionStrategy.ExecutionStrategy): self is

/** @internal */
export const match = dual<
<A>(
onSequential: LazyArg<A>,
onParallel: LazyArg<A>,
onParallelN: (n: number) => A
) => (self: ExecutionStrategy.ExecutionStrategy) => A,
<A>(options: {
readonly onSequential: LazyArg<A>
readonly onParallel: LazyArg<A>
readonly onParallelN: (n: number) => A
}) => (self: ExecutionStrategy.ExecutionStrategy) => A,
<A>(
self: ExecutionStrategy.ExecutionStrategy,
onSequential: LazyArg<A>,
onParallel: LazyArg<A>,
onParallelN: (n: number) => A
options: {
readonly onSequential: LazyArg<A>
readonly onParallel: LazyArg<A>
readonly onParallelN: (n: number) => A
}
) => A
>(4, (self, onSequential, onParallel, onParallelN) => {
>(2, (self, options) => {
switch (self._tag) {
case OP_SEQUENTIAL: {
return onSequential()
return options.onSequential()
}
case OP_PARALLEL: {
return onParallel()
return options.onParallel()
}
case OP_PARALLEL_N: {
return onParallelN(self.parallelism)
return options.onParallelN(self.parallelism)
}
}
})
37 changes: 23 additions & 14 deletions packages/effect/src/internal/stream/haltStrategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,33 +53,42 @@ export const isEither = (self: HaltStrategy.HaltStrategy): self is HaltStrategy.

/** @internal */
export const match = dual<
<Z>(onLeft: () => Z, onRight: () => Z, onBoth: () => Z, onEither: () => Z) => (self: HaltStrategy.HaltStrategy) => Z,
<Z>(options: {
readonly onLeft: () => Z
readonly onRight: () => Z
readonly onBoth: () => Z
readonly onEither: () => Z
}) => (self: HaltStrategy.HaltStrategy) => Z,
<Z>(
self: HaltStrategy.HaltStrategy,
onLeft: () => Z,
onRight: () => Z,
onBoth: () => Z,
onEither: () => Z
options: {
readonly onLeft: () => Z
readonly onRight: () => Z
readonly onBoth: () => Z
readonly onEither: () => Z
}
) => Z
>(5, <Z>(
>(2, <Z>(
self: HaltStrategy.HaltStrategy,
onLeft: () => Z,
onRight: () => Z,
onBoth: () => Z,
onEither: () => Z
options: {
readonly onLeft: () => Z
readonly onRight: () => Z
readonly onBoth: () => Z
readonly onEither: () => Z
}
): Z => {
switch (self._tag) {
case OpCodes.OP_LEFT: {
return onLeft()
return options.onLeft()
}
case OpCodes.OP_RIGHT: {
return onRight()
return options.onRight()
}
case OpCodes.OP_BOTH: {
return onBoth()
return options.onBoth()
}
case OpCodes.OP_EITHER: {
return onEither()
return options.onEither()
}
}
})

0 comments on commit 52ec4ca

Please sign in to comment.