From 6205bf36165f66a44a6946af1e4d0a5d8b29bd28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Valverd=C3=A9?= Date: Wed, 20 May 2026 17:19:03 +0200 Subject: [PATCH] Refactor --- packages/effect-lens/src/Lens.ts | 60 +++++++++++++++++++++++--------- 1 file changed, 43 insertions(+), 17 deletions(-) diff --git a/packages/effect-lens/src/Lens.ts b/packages/effect-lens/src/Lens.ts index b5ddd94..d7faf36 100644 --- a/packages/effect-lens/src/Lens.ts +++ b/packages/effect-lens/src/Lens.ts @@ -129,6 +129,49 @@ export const makeLazy = ( ): Lens => new LensLazyImpl(source) +export declare namespace SynchronizedRefLensImpl { + export interface SynchronizedRefWithInternals + extends SynchronizedRef.SynchronizedRef { + readonly ref: Ref.Ref + readonly withLock: (self: Effect.Effect) => Effect.Effect + } +} + +export class SynchronizedRefLensImpl +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 + + readonly ref: SynchronizedRefLensImpl.SynchronizedRefWithInternals + + constructor( + ref: SynchronizedRef.SynchronizedRef + ) { + super() + this.ref = ref as SynchronizedRefLensImpl.SynchronizedRefWithInternals + } + + get get() { return this.ref.get } + get changes() { return Stream.unwrap(Effect.map(this.ref.get, Stream.make)) } + update(a: A) { return Ref.set(this.ref.ref, a) } + get withLock() { return this.ref.withLock } + + modifyEffect = modifyEffect +} + +/** + * Creates a `Lens` that proxies a `SynchronizedRef`. + * + * Note: since `SynchronizedRef` does not provide any kind of reactivity mechanism, the produced `Lens` will be non-reactive. + * This means its `changes` stream will only emit the current value once when evaluated and nothing else. + */ +export const fromSynchronizedRef = ( + ref: SynchronizedRef.SynchronizedRef +): Lens => new SynchronizedRefLensImpl(ref) + + export declare namespace SubscriptionRefLensImpl { export interface SubscriptionRefWithInternals extends SubscriptionRef.SubscriptionRef { @@ -175,23 +218,6 @@ export const fromSubscriptionRef = ( ): Lens => new SubscriptionRefLensImpl(ref) -/** - * Creates a `Lens` that proxies a `SynchronizedRef`. - * - * Note: since `SynchronizedRef` does not provide any kind of reactivity mechanism, the produced `Lens` will be non-reactive. - * This means its `changes` stream will only emit the current value once when evaluated and nothing else. - */ -export const fromSynchronizedRef = ( - ref: SynchronizedRef.SynchronizedRef -): Lens => make({ - get get() { return ref.get }, - get changes() { return Stream.unwrap(Effect.map(ref.get, Stream.make)) }, - modify: ( - f: (a: A) => Effect.Effect - ) => ref.modifyEffect(f), -}) - - /** * Flattens an effectful `Lens`. */