Skip to content

Commit

Permalink
use LazyArg for Effect.if branches (#2451)
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-smart authored and mikearnaldi committed Apr 16, 2024
1 parent 9aeae46 commit 53d1c2a
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 43 deletions.
23 changes: 23 additions & 0 deletions .changeset/three-poems-beg.md
@@ -0,0 +1,23 @@
---
"effect": minor
---

use LazyArg for Effect.if branches

Instead of:

```ts
Effect.if(true, {
onTrue: Effect.succeed("true"),
onFalse: Effect.succeed("false"),
});
```

You should now write:

```ts
Effect.if(true, {
onTrue: () => Effect.succeed("true"),
onFalse: () => Effect.succeed("false"),
});
```
14 changes: 5 additions & 9 deletions packages/effect/src/Effect.ts
Expand Up @@ -3340,16 +3340,12 @@ export const option: <A, E, R>(self: Effect<A, E, R>) => Effect<Option.Option<A>

const if_: {
<A1, E1, R1, A2, E2, R2>(
options: { readonly onTrue: Effect<A1, E1, R1>; readonly onFalse: Effect<A2, E2, R2> }
options: { readonly onTrue: LazyArg<Effect<A1, E1, R1>>; readonly onFalse: LazyArg<Effect<A2, E2, R2>> }
): <E = never, R = never>(self: boolean | Effect<boolean, E, R>) => Effect<A1 | A2, E1 | E2 | E, R1 | R2 | R>
<A1, E1, R1, A2, E2, R2>(
self: boolean,
options: { readonly onTrue: Effect<A1, E1, R1>; readonly onFalse: Effect<A2, E2, R2> }
): Effect<A1 | A2, E1 | E2, R1 | R2>
<E, R, A1, E1, R1, A2, E2, R2>(
self: Effect<boolean, E, R>,
options: { readonly onTrue: Effect<A1, E1, R1>; readonly onFalse: Effect<A2, E2, R2> }
): Effect<A1 | A2, E | E1 | E2, R | R1 | R2>
<A1, E1, R1, A2, E2, R2, E = never, R = never>(
self: boolean | Effect<boolean, E, R>,
options: { readonly onTrue: LazyArg<Effect<A1, E1, R1>>; readonly onFalse: LazyArg<Effect<A2, E2, R2>> }
): Effect<A1 | A2, E1 | E2 | E, R1 | R2 | R>
} = core.if_

export {
Expand Down
44 changes: 21 additions & 23 deletions packages/effect/src/internal/core.ts
Expand Up @@ -926,35 +926,33 @@ export const forEachSequentialDiscard: {
export const if_ = dual<
<A1, E1, R1, A2, E2, R2>(
options: {
readonly onTrue: Effect.Effect<A1, E1, R1>
readonly onFalse: Effect.Effect<A2, E2, R2>
readonly onTrue: LazyArg<Effect.Effect<A1, E1, R1>>
readonly onFalse: LazyArg<Effect.Effect<A2, E2, R2>>
}
) => <E = never, R = never>(
self: Effect.Effect<boolean, E, R> | boolean
) => Effect.Effect<A1 | A2, E | E1 | E2, R | R1 | R2>,
{
<A1, E1, R1, A2, E2, R2>(
self: boolean,
options: {
readonly onTrue: Effect.Effect<A1, E1, R1>
readonly onFalse: Effect.Effect<A2, E2, R2>
}
): Effect.Effect<A1 | A2, E1 | E2, R1 | R2>
<E, R, A1, E1, R1, A2, E2, R2>(
self: Effect.Effect<boolean, E, R>,
options: {
readonly onTrue: Effect.Effect<A1, E1, R1>
readonly onFalse: Effect.Effect<A2, E2, R2>
}
): Effect.Effect<A1 | A2, E1 | E2 | E, R1 | R2 | R>
}
<A1, E1, R1, A2, E2, R2, E = never, R = never>(
self: Effect.Effect<boolean, E, R> | boolean,
options: {
readonly onTrue: LazyArg<Effect.Effect<A1, E1, R1>>
readonly onFalse: LazyArg<Effect.Effect<A2, E2, R2>>
}
) => Effect.Effect<A1 | A2, E1 | E2 | E, R1 | R2 | R>
>(
(args) => typeof args[0] === "boolean" || isEffect(args[0]),
(self: boolean | Effect.Effect<unknown, unknown, unknown>, { onFalse, onTrue }: {
readonly onTrue: Effect.Effect<unknown, unknown, unknown>
readonly onFalse: Effect.Effect<unknown, unknown, unknown>
// eslint-disable-next-line no-extra-boolean-cast
}) => isEffect(self) ? flatMap(self, (b) => (b ? onTrue : onFalse)) : Boolean(self) ? onTrue : onFalse
<A1, E1, R1, A2, E2, R2, E = never, R = never>(
self: Effect.Effect<boolean, E, R> | boolean,
options: {
readonly onTrue: LazyArg<Effect.Effect<A1, E1, R1>>
readonly onFalse: LazyArg<Effect.Effect<A2, E2, R2>>
}
): Effect.Effect<A1 | A2, E1 | E2 | E, R1 | R2 | R> =>
isEffect(self)
? flatMap(self, (b): Effect.Effect<A1 | A2, E1 | E2, R1 | R2> => (b ? options.onTrue() : options.onFalse()))
: self
? options.onTrue()
: options.onFalse()
)

/* @internal */
Expand Down
2 changes: 1 addition & 1 deletion packages/effect/src/internal/fiberRuntime.ts
Expand Up @@ -1597,7 +1597,7 @@ export const exists: {
core.matchEffect(
forEach(
elements,
(a, i) => core.if_(f(a, i), { onTrue: core.fail(_existsParFound), onFalse: core.unit }),
(a, i) => core.if_(f(a, i), { onTrue: () => core.fail(_existsParFound), onFalse: () => core.unit }),
options
),
{
Expand Down
12 changes: 6 additions & 6 deletions packages/effect/test/Effect/cause-rendering.test.ts
Expand Up @@ -30,8 +30,8 @@ describe("Effect", () => {
const effect = Effect.withSpan("spanB")(
Effect.withSpan("spanA")(
Effect.if(Effect.sync(() => Math.random() > 1), {
onTrue: Effect.fail(new E2()),
onFalse: Effect.fail(err)
onTrue: () => Effect.fail(new E2()),
onFalse: () => Effect.fail(err)
})
)
).pipe(Effect.catchTag("E2", (e) => Effect.die(e)))
Expand All @@ -55,8 +55,8 @@ describe("Effect", () => {
const effect = Effect.withSpan("spanB")(
Effect.withSpan("spanA")(
Effect.if(Effect.sync(() => Math.random() > 1), {
onTrue: Effect.fail(new E2()),
onFalse: Effect.fail(new E1())
onTrue: () => Effect.fail(new E2()),
onFalse: () => Effect.fail(new E1())
})
)
).pipe(Effect.catchAll((e) => Effect.fail(e)))
Expand All @@ -76,8 +76,8 @@ describe("Effect", () => {
const effect = Effect.withSpan("spanB")(
Effect.withSpan("spanA")(
Effect.if(Effect.sync(() => Math.random() > 1), {
onTrue: Effect.fail(new E2()),
onFalse: Effect.fail(new E1())
onTrue: () => Effect.fail(new E2()),
onFalse: () => Effect.fail(new E1())
})
)
).pipe(Effect.catchTags({ E2: (e) => Effect.die(e) }))
Expand Down
8 changes: 4 additions & 4 deletions packages/effect/test/Effect/sequencing.test.ts
Expand Up @@ -71,8 +71,8 @@ describe("Effect", () => {
const result = yield* $(
true,
Effect.if({
onTrue: Effect.succeed(true),
onFalse: Effect.succeed(false)
onTrue: () => Effect.succeed(true),
onFalse: () => Effect.succeed(false)
})
)
assert.isTrue(result)
Expand All @@ -82,8 +82,8 @@ describe("Effect", () => {
const result = yield* $(
Effect.succeed(false),
Effect.if({
onFalse: Effect.succeed(true),
onTrue: Effect.succeed(false)
onFalse: () => Effect.succeed(true),
onTrue: () => Effect.succeed(false)
})
)
assert.isTrue(result)
Expand Down

0 comments on commit 53d1c2a

Please sign in to comment.