diff --git a/packages/effect-lens/src/Lens.ts b/packages/effect-lens/src/Lens.ts index a3daf7e..4901de8 100644 --- a/packages/effect-lens/src/Lens.ts +++ b/packages/effect-lens/src/Lens.ts @@ -1,5 +1,6 @@ -import { Array, Chunk, Effect, Function, Pipeable, Predicate, Readable, Stream, Subscribable, type SubscriptionRef, type SynchronizedRef } from "effect" +import { Array, Chunk, Effect, Function, Pipeable, Predicate, Readable, Stream, type SubscriptionRef, type SynchronizedRef } from "effect" import type { NoSuchElementException } from "effect/Cause" +import * as Subscribable from "./Subscribable.js" export const LensTypeId: unique symbol = Symbol.for("@effect-fc/Lens/Lens") @@ -206,7 +207,7 @@ export const mapStream: { /** * Narrows the focus to a field of an object. Replaces the object in an immutable fashion when written to. */ -export const focusField: { +export const focusObjectField: { ( self: Lens, key: K, @@ -223,7 +224,7 @@ export const focusField: { (a, b) => Object.setPrototypeOf({ ...a, [key]: b }, Object.getPrototypeOf(a)), )) -export declare namespace focusMutableField { +export declare namespace focusObjectMutableField { export type WritableKeys = { [K in keyof T]-?: IfEquals< { [P in K]: T[K] }, @@ -239,15 +240,15 @@ export declare namespace focusMutableField { /** * Narrows the focus to a mutable field of an object. Mutates the object in place when written to. */ -export const focusMutableField: { - , ER, EW, RR, RW>( +export const focusObjectMutableField: { + , ER, EW, RR, RW>( self: Lens, key: K, ): Lens - , ER, EW, RR, RW>( + , ER, EW, RR, RW>( key: K, ): (self: Lens) => Lens -} = Function.dual(2, , ER, EW, RR, RW>( +} = Function.dual(2, , ER, EW, RR, RW>( self: Lens, key: K, ): Lens => map(self, a => a[key], (a, b) => { a[key] = b; return a })) @@ -259,7 +260,7 @@ export const focusArrayAt: { ( self: Lens, index: number, - ): Lens + ): Lens ( index: number ): (self: Lens) => Lens diff --git a/packages/effect-lens/src/Subscribable.ts b/packages/effect-lens/src/Subscribable.ts new file mode 100644 index 0000000..125198e --- /dev/null +++ b/packages/effect-lens/src/Subscribable.ts @@ -0,0 +1,53 @@ +import { Array, Chunk, Function, Subscribable } from "effect" +import type { NoSuchElementException } from "effect/Cause" + + +export * from "effect/Subscribable" + +/** + * Narrows the focus to a field of an object. + */ +export const focusObjectField: { + ( + self: Subscribable.Subscribable, + key: K, + ): Subscribable.Subscribable + ( + key: K, + ): (self: Subscribable.Subscribable) => Subscribable.Subscribable +} = Function.dual(2, ( + self: Subscribable.Subscribable, + key: K, +): Subscribable.Subscribable => Subscribable.map(self, a => a[key])) + +/** + * Narrows the focus to an indexed element of an array. + */ +export const focusArrayAt: { + ( + self: Subscribable.Subscribable, + index: number, + ): Subscribable.Subscribable + ( + index: number + ): (self: Subscribable.Subscribable) => Subscribable.Subscribable +} = Function.dual(2, ( + self: Subscribable.Subscribable, + index: number, +): Subscribable.Subscribable => Subscribable.mapEffect(self, Array.get(index))) + +/** + * Narrows the focus to an indexed element of `Chunk`. + */ +export const focusChunkAt: { + ( + self: Subscribable.Subscribable, E, R>, + index: number, + ): Subscribable.Subscribable + ( + index: number + ): (self: Subscribable.Subscribable, E, R>) => Subscribable.Subscribable +} = Function.dual(2, ( + self: Subscribable.Subscribable, E, R>, + index: number, +): Subscribable.Subscribable => Subscribable.mapEffect(self, Chunk.get(index))) diff --git a/packages/effect-lens/src/index.ts b/packages/effect-lens/src/index.ts index 2b9139b..6eec098 100644 --- a/packages/effect-lens/src/index.ts +++ b/packages/effect-lens/src/index.ts @@ -1,2 +1,3 @@ export * as Lens from "./Lens.js" export * as PropertyPath from "./PropertyPath.js" +export * as Subscribable from "./Subscribable.js"