diff --git a/packages/effect-lens/src/Lens.ts b/packages/effect-lens/src/Lens.ts index 43b0a11..1f52957 100644 --- a/packages/effect-lens/src/Lens.ts +++ b/packages/effect-lens/src/Lens.ts @@ -149,7 +149,7 @@ export class LensLazyImpl< > extends LensImpl { constructor( - readonly source: LensLazyImpl.Source + readonly source: LensLazyImpl.Source, ) { super() } @@ -174,6 +174,36 @@ export const makeLazy = ( source: LensLazyImpl.Source ): Lens => new LensLazyImpl(source) +/** + * Derives a new `Lens` by immutably appending a step to an existing `LensImpl`. + */ +export const derive: { + ( + self: LensImpl, + step: LensStep, + ): Lens + ( + step: LensStep, + ): ( + self: LensImpl + ) => Lens +} = Function.dual(2, ( + self: LensImpl, + step: LensStep, +): Lens => Object.defineProperty( + Object.defineProperties( + Object.create(Object.getPrototypeOf(self)), + Object.getOwnPropertyDescriptors(self), + ), + "steps", + { + configurable: true, + enumerable: true, + value: [...self.steps, step as LensStep], + writable: false, + }, +) as Lens) + export declare namespace SynchronizedRefLensImpl { export interface SynchronizedRefWithInternals