From 2e5ef92bb128021a8b263378ea785242461469f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Valverd=C3=A9?= Date: Tue, 26 May 2026 20:55:39 +0200 Subject: [PATCH] Refactor --- packages/effect-lens/src/Lens.test.ts | 17 ++++--- packages/effect-lens/src/Lens.ts | 70 +++++++++------------------ 2 files changed, 35 insertions(+), 52 deletions(-) diff --git a/packages/effect-lens/src/Lens.test.ts b/packages/effect-lens/src/Lens.test.ts index 58b118f..7ee6ed8 100644 --- a/packages/effect-lens/src/Lens.test.ts +++ b/packages/effect-lens/src/Lens.test.ts @@ -1,5 +1,5 @@ import { describe, expect, test } from "bun:test" -import { Chunk, Context, Effect, Option, Stream, SubscriptionRef } from "effect" +import { Chunk, Context, Effect, identity, Option, Stream, SubscriptionRef } from "effect" import * as Lens from "./Lens.js" @@ -11,7 +11,8 @@ describe("Lens", () => { Lens.make({ get: Effect.fail("read" as const), changes: Stream.fail("read" as const), - set: () => Effect.void, + commit: () => Effect.void, + withLock: identity, }), error => `mapped:${ error }`, ) @@ -26,7 +27,8 @@ describe("Lens", () => { Lens.make({ get: Effect.succeed(1), changes: Stream.make(1), - set: () => Effect.fail("write" as const), + commit: () => Effect.fail("write" as const), + withLock: identity, }), () => "mapped-write", ) @@ -41,7 +43,8 @@ describe("Lens", () => { Lens.make({ get: Effect.fail("read" as const), changes: Stream.fail("read" as const), - set: () => Effect.fail("write" as const), + commit: () => Effect.fail("write" as const), + withLock: identity, }), () => "mapped", ) @@ -84,7 +87,8 @@ describe("Lens", () => { Lens.make({ get: Effect.fail("read" as const), changes: Stream.fail("read" as const), - set: () => Effect.void, + commit: () => Effect.void, + withLock: identity, }), () => SubscriptionRef.modify(counter, n => [void 0, n + 1] as const), ) @@ -108,7 +112,8 @@ describe("Lens", () => { Lens.make({ get: Effect.succeed(1), changes: Stream.make(1), - set: () => Effect.fail("write" as const), + commit: () => Effect.fail("write" as const), + withLock: identity, }), () => SubscriptionRef.modify(counter, n => [void 0, n + 1] as const), ) diff --git a/packages/effect-lens/src/Lens.ts b/packages/effect-lens/src/Lens.ts index f467d71..0db61d0 100644 --- a/packages/effect-lens/src/Lens.ts +++ b/packages/effect-lens/src/Lens.ts @@ -40,13 +40,7 @@ export declare namespace LensImpl { } } -export abstract class LensImpl< - in out A, - in out ER = never, - in out EW = never, - in out RR = never, - in out RW = never, -> +export abstract class LensImpl extends Pipeable.Class() implements Lens { readonly [Readable.TypeId]: Readable.TypeId = Readable.TypeId readonly [Subscribable.TypeId]: Subscribable.TypeId = Subscribable.TypeId @@ -57,13 +51,11 @@ extends Pipeable.Class() implements Lens { abstract readonly changes: Stream.Stream abstract readonly withLock: (self: Effect.Effect) => Effect.Effect - get get(): Effect.Effect { - return Effect.map(this.access, frame => frame.value) - } + get get() { return Effect.map(this.access, frame => frame.value) } - modifyEffect( - f: (a: A) => Effect.Effect, - ): Effect.Effect { + modifyEffect( + f: (a: A) => Effect.Effect, + ): Effect.Effect { return this.withLock(Effect.flatMap( this.access, frame => Effect.flatMap( @@ -86,29 +78,23 @@ export const asLensImpl = ( export declare namespace LensLazyImpl { - export interface Source { - readonly get: Effect.Effect - readonly changes: Stream.Stream - readonly commit: (b: B) => Effect.Effect + export interface Source { + readonly get: Effect.Effect + readonly changes: Stream.Stream + readonly commit: (a: A) => Effect.Effect readonly withLock: (self: Effect.Effect) => Effect.Effect } } -export class LensLazyImpl< - in out B, - in out ESW = never, - in out ESR = never, - in out RSR = never, - in out RSW = never, -> -extends LensImpl { +export class LensLazyImpl +extends LensImpl { constructor( - readonly source: LensLazyImpl.Source, + readonly source: LensLazyImpl.Source, ) { super() } - get access(): Effect.Effect, ESR, RSR> { + get access(): Effect.Effect, ER, RR> { return Effect.map( this.source.get, value => ({ @@ -117,7 +103,6 @@ extends LensImpl { }), ) } - get changes() { return this.source.changes } get withLock() { return this.source.withLock } } @@ -125,9 +110,9 @@ extends LensImpl { /** * Creates a `Lens` by supplying how to read the current value, observe changes, and apply transformations. */ -export const make = ( - source: LensLazyImpl.Source -): Lens => new LensLazyImpl(source) +export const make = ( + source: LensLazyImpl.Source +): Lens => new LensLazyImpl(source) export declare namespace DerivedLensImpl { @@ -168,17 +153,9 @@ extends LensImpl { super() } - get access(): Effect.Effect, ER, RR> { - return this.source.access(this.parent.access) - } - - get changes(): Stream.Stream { - return this.source.transformStream(this.parent.changes) - } - - get withLock() { - return this.parent.withLock - } + get access() { return this.source.access(this.parent.access) } + get changes() { return this.source.transformStream(this.parent.changes) } + get withLock() { return this.parent.withLock } } /** @@ -222,13 +199,14 @@ extends LensImpl { this.ref.get, value => ({ value, - commit: next => Effect.flatMap(next, value => Ref.set(this.ref.ref, value)), + commit: next => Effect.flatMap(next, value => this.commit(value)), }), ) } - get changes() { return Stream.unwrap(Effect.map(this.ref.get, Stream.make)) } get withLock() { return this.ref.withLock } + + commit(a: A) { return Ref.set(this.ref.ref, a) } } /** @@ -271,15 +249,15 @@ extends LensImpl { }), ) } - get changes() { return this.ref.changes } + get withLock() { return this.ref.semaphore.withPermits(1) } + commit(a: A) { return Effect.zipLeft( Ref.set(this.ref.ref, a), PubSub.publish(this.ref.pubsub, a), ) } - get withLock() { return this.ref.semaphore.withPermits(1) } } /**