diff --git a/packages/effect-lens/src/Subscribable.ts b/packages/effect-lens/src/Subscribable.ts index c88a108..10cf991 100644 --- a/packages/effect-lens/src/Subscribable.ts +++ b/packages/effect-lens/src/Subscribable.ts @@ -1,9 +1,44 @@ -import { Array, Chunk, Function, Subscribable } from "effect" +import { Array, Chunk, Effect, Function, Option, Subscribable } from "effect" import type { NoSuchElementException } from "effect/Cause" export * from "effect/Subscribable" +/** + * Maps over an `Option` value in the `Subscribable`. + */ +export const mapOption: { + ( + self: Subscribable.Subscribable, E, R>, + f: (a: A) => B, + ): Subscribable.Subscribable, E, R> + ( + f: (a: A) => B, + ): (self: Subscribable.Subscribable, E, R>) => Subscribable.Subscribable, E, R> +} = Function.dual(2, ( + self: Subscribable.Subscribable, E, R>, + f: (a: A) => B, +): Subscribable.Subscribable, E, R> => Subscribable.map(self, Option.map(f))) + +/** + * Maps over an `Option` value in the `Subscribable` with an Effect. + */ +export const mapOptionEffect: { + ( + self: Subscribable.Subscribable, E, R>, + f: (a: A) => Effect.Effect, + ): Subscribable.Subscribable, E | E2, R> + ( + f: (a: A) => Effect.Effect, + ): (self: Subscribable.Subscribable, E, R>) => Subscribable.Subscribable, E | E2, R> +} = Function.dual(2, ( + self: Subscribable.Subscribable, E, R>, + f: (a: A) => Effect.Effect, +): Subscribable.Subscribable, E | E2, R> => Subscribable.mapEffect(self, Option.match({ + onSome: a => Effect.map(f(a), Option.some), + onNone: () => Effect.succeed(Option.none()), +}))) + /** * Narrows the focus to a field of an object. */