From f074ae2978297b3a4e4d258ede257b538c4c8fe1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Valverd=C3=A9?= Date: Fri, 22 May 2026 00:32:14 +0200 Subject: [PATCH] Refactor --- packages/effect-lens/src/Lens.ts | 109 +++++++++++++++++-------------- 1 file changed, 59 insertions(+), 50 deletions(-) diff --git a/packages/effect-lens/src/Lens.ts b/packages/effect-lens/src/Lens.ts index 52035ee..3a7359d 100644 --- a/packages/effect-lens/src/Lens.ts +++ b/packages/effect-lens/src/Lens.ts @@ -28,44 +28,63 @@ extends Subscribable.Subscribable { export const isLens = (u: unknown): u is Lens => Predicate.hasProperty(u, LensTypeId) +export const LensStepTypeId: unique symbol = Symbol.for("@effect-fc/Lens/LensStep") +export type LensStepTypeId = typeof LensTypeId - - -export const LensWithInternalsTypeId: unique symbol = Symbol.for("@effect-fc/Lens/LensWithInternals") -export type LensWithInternalsTypeId = typeof LensWithInternalsTypeId - -export interface LensWithInternals -extends Lens { - readonly [LensWithInternalsTypeId]: LensWithInternalsTypeId - - readonly update: (a: A) => Effect.Effect - readonly withLock: (self: Effect.Effect) => Effect.Effect +export interface LensStep { + readonly [LensStepTypeId]: LensStepTypeId + readonly transform: (parent: B) => Effect.Effect + readonly update: (next: A, parent: B) => Effect.Effect } -export const isLensWithInternals = (u: unknown): u is LensWithInternals => Predicate.hasProperty(u, LensWithInternalsTypeId) +export const isLensStep = (u: unknown): u is LensStep => Predicate.hasProperty(u, LensStepTypeId) -export const asLensWithInternals = ( - lens: Lens -): LensWithInternals => { - if (!isLensWithInternals(lens)) - throw new Error("Not a 'LensWithInternals'.") - return lens as LensWithInternals -} +// export const LensWithInternalsTypeId: unique symbol = Symbol.for("@effect-fc/Lens/LensWithInternals") +// export type LensWithInternalsTypeId = typeof LensWithInternalsTypeId + +// export interface LensWithInternals +// extends Lens { +// readonly [LensWithInternalsTypeId]: LensWithInternalsTypeId + +// readonly update: (a: A) => Effect.Effect +// readonly withLock: (self: Effect.Effect) => Effect.Effect +// } + +// export const isLensWithInternals = (u: unknown): u is LensWithInternals => Predicate.hasProperty(u, LensWithInternalsTypeId) + +// export const asLensWithInternals = ( +// lens: Lens +// ): LensWithInternals => { +// if (!isLensWithInternals(lens)) +// throw new Error("Not a 'LensWithInternals'.") +// return lens as LensWithInternals +// } + + +export const LensImplTypeId: unique symbol = Symbol.for("@effect-fc/Lens/LensImpl") +export type LensImplTypeId = typeof LensImplTypeId export abstract class LensImpl -extends Pipeable.Class() { +extends Pipeable.Class() implements Lens { readonly [Readable.TypeId]: Readable.TypeId = Readable.TypeId readonly [Subscribable.TypeId]: Subscribable.TypeId = Subscribable.TypeId readonly [LensTypeId]: LensTypeId = LensTypeId - readonly [LensWithInternalsTypeId]: LensWithInternalsTypeId = LensWithInternalsTypeId + readonly [LensImplTypeId]: LensImplTypeId = LensImplTypeId abstract readonly get: Effect.Effect abstract readonly changes: Stream.Stream - abstract readonly update: (a: A) => Effect.Effect + abstract readonly commit: (a: A) => Effect.Effect abstract readonly withLock: (self: Effect.Effect) => Effect.Effect - modifyEffect = modifyEffect + modifyEffect( + f: (a: A) => Effect.Effect, + ): Effect.Effect { + return this.withLock(Effect.flatMap( + this.get, + a => Effect.flatMap(f(a), ([b, next]) => Effect.as(this.commit(next), b), + ))) + } } // export declare namespace LensImpl { @@ -103,16 +122,6 @@ extends Pipeable.Class() { // modifyEffect = modifyEffect // } -function modifyEffect( - this: LensWithInternals, - f: (a: A) => Effect.Effect, -): Effect.Effect { - return this.withLock(Effect.flatMap( - this.get, - a => Effect.flatMap(f(a), ([b, next]) => Effect.as(this.update(next), b), - ))) -} - /** * Creates a `Lens` by supplying how to read the current value, observe changes, and apply transformations. */ @@ -121,26 +130,26 @@ export const make = ( ): Lens => new LensImpl(source) -export class LensLazyImpl -extends Pipeable.Class() implements LensWithInternals { - readonly [Readable.TypeId]: Readable.TypeId = Readable.TypeId - readonly [Subscribable.TypeId]: Subscribable.TypeId = Subscribable.TypeId - readonly [LensTypeId]: LensTypeId = LensTypeId - readonly [LensWithInternalsTypeId]: LensWithInternalsTypeId = LensWithInternalsTypeId +// export class LensLazyImpl +// extends Pipeable.Class() implements LensWithInternals { +// readonly [Readable.TypeId]: Readable.TypeId = Readable.TypeId +// readonly [Subscribable.TypeId]: Subscribable.TypeId = Subscribable.TypeId +// readonly [LensTypeId]: LensTypeId = LensTypeId +// readonly [LensWithInternalsTypeId]: LensWithInternalsTypeId = LensWithInternalsTypeId - constructor( - readonly source: LensImpl.Source - ) { - super() - } +// constructor( +// readonly source: LensImpl.Source +// ) { +// super() +// } - get get() { return this.source.get } - get changes() { return this.source.changes } - get update() { return this.source.update } - get withLock() { return this.source.withLock } +// get get() { return this.source.get } +// get changes() { return this.source.changes } +// get update() { return this.source.update } +// get withLock() { return this.source.withLock } - modifyEffect = modifyEffect -} +// modifyEffect = modifyEffect +// } /** * Creates a `Lens` by supplying how to read the current value, observe changes, and apply transformations.