From 1e068af09c5d4c41d3387185148b0c63d7b42a43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Valverd=C3=A9?= Date: Mon, 30 Mar 2026 13:27:54 +0200 Subject: [PATCH] Add Subscribable utils --- packages/effect-lens/src/Subscribable.ts | 37 +++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) 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. */