diff --git a/packages/effect-lens/src/Lens.test.ts b/packages/effect-lens/src/Lens.test.ts index de10c5d..9a62dce 100644 --- a/packages/effect-lens/src/Lens.test.ts +++ b/packages/effect-lens/src/Lens.test.ts @@ -12,7 +12,7 @@ describe("Lens", () => { get: Effect.fail("read" as const), changes: Stream.fail("read" as const), commit: () => Effect.void, - withLock: identity, + lock: Effect.succeed(identity), }), error => `mapped:${ error }`, ) @@ -28,7 +28,7 @@ describe("Lens", () => { get: Effect.succeed(1), changes: Stream.make(1), commit: () => Effect.fail("write" as const), - withLock: identity, + lock: Effect.succeed(identity), }), () => "mapped-write", ) @@ -44,7 +44,7 @@ describe("Lens", () => { get: Effect.fail("read" as const), changes: Stream.fail("read" as const), commit: () => Effect.fail("write" as const), - withLock: identity, + lock: Effect.succeed(identity), }), () => "mapped", ) @@ -68,7 +68,7 @@ describe("Lens", () => { get: Effect.fail("read" as const), changes: Stream.fail("read" as const), commit: () => Effect.void, - withLock: identity, + lock: Effect.succeed(identity), }), () => Lens.fromSubscriptionRef(fallback), ), @@ -89,7 +89,7 @@ describe("Lens", () => { get: Effect.fail("read" as const), changes: Stream.fail("read" as const), commit: () => Effect.void, - withLock: identity, + lock: Effect.succeed(identity), }), () => SubscriptionRef.modify(counter, n => [void 0, n + 1] as const), ) @@ -114,7 +114,7 @@ describe("Lens", () => { get: Effect.succeed(1), changes: Stream.make(1), commit: () => Effect.fail("write" as const), - withLock: identity, + lock: Effect.succeed(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 5edc410..c581347 100644 --- a/packages/effect-lens/src/Lens.ts +++ b/packages/effect-lens/src/Lens.ts @@ -38,6 +38,10 @@ export declare namespace LensImpl { next: Effect.Effect ) => Effect.Effect } + + export interface Lock { + (self: Effect.Effect): Effect.Effect + } } export abstract class LensImpl @@ -49,20 +53,23 @@ extends Pipeable.Class() implements Lens { abstract readonly resolve: Effect.Effect, ER, RR> abstract readonly changes: Stream.Stream - abstract readonly withLock: (self: Effect.Effect) => Effect.Effect + abstract readonly lock: Effect.Effect get get() { return Effect.map(this.resolve, frame => frame.value) } modifyEffect( f: (a: A) => Effect.Effect, ): Effect.Effect { - return this.withLock(Effect.flatMap( - this.resolve, - frame => Effect.flatMap( - f(frame.value), - ([c, next]) => Effect.as(frame.commit(Effect.succeed(next)), c), - ), - )) + return Effect.flatMap( + this.lock, + lock => lock(Effect.flatMap( + this.resolve, + frame => Effect.flatMap( + f(frame.value), + ([c, next]) => Effect.as(frame.commit(Effect.succeed(next)), c), + ), + )), + ) } } @@ -82,7 +89,7 @@ export declare namespace LensLazyImpl { readonly get: Effect.Effect readonly changes: Stream.Stream readonly commit: (a: A) => Effect.Effect - readonly withLock: (self: Effect.Effect) => Effect.Effect + readonly lock: Effect.Effect } } @@ -104,7 +111,7 @@ extends LensImpl { ) } get changes() { return this.source.changes } - get withLock() { return this.source.withLock } + get lock() { return this.source.lock } } /** @@ -130,9 +137,7 @@ export declare namespace DerivedLensImpl { > { readonly resolve: (effect: Effect.Effect, ESR, RSR>) => Effect.Effect, ER, RR> readonly mapStream: (stream: Stream.Stream) => Stream.Stream - readonly withLock: ( - withLock: (self: Effect.Effect) => Effect.Effect - ) => (self: Effect.Effect) => Effect.Effect + readonly mapLock: (lock: Effect.Effect) => Effect.Effect } } @@ -158,7 +163,7 @@ extends LensImpl { get resolve() { return this.source.resolve(this.parent.resolve) } get changes() { return this.source.mapStream(this.parent.changes) } - get withLock() { return this.source.withLock(this.parent.withLock) } + get lock() { return this.source.mapLock(this.parent.lock) } } /** @@ -200,7 +205,7 @@ extends LensImpl { ) } get changes() { return Stream.unwrap(Effect.map(Ref.get(this.ref), Stream.make)) } - get withLock() { return this.semaphore.withPermits(1) } + get lock() { return Effect.succeed(this.semaphore.withPermits(1)) } } /** @@ -220,7 +225,7 @@ export declare namespace SynchronizedRefLensImpl { export interface SynchronizedRefWithInternals extends SynchronizedRef.SynchronizedRef { readonly ref: Ref.Ref - readonly withLock: (self: Effect.Effect) => Effect.Effect + readonly withLock: LensImpl.Lock } } @@ -245,7 +250,7 @@ extends LensImpl { ) } get changes() { return Stream.unwrap(Effect.map(Ref.get(this.ref.ref), Stream.make)) } - get withLock() { return this.ref.withLock } + get lock() { return Effect.succeed(this.ref.withLock) } } /** @@ -292,7 +297,7 @@ extends LensImpl { ) } get changes() { return this.ref.changes } - get withLock() { return this.ref.semaphore.withPermits(1) } + get lock() { return Effect.succeed(this.ref.semaphore.withPermits(1)) } } /** @@ -315,7 +320,7 @@ export const unwrap = ( effect, l => Effect.flatMap(asLensImpl(l).resolve, frame => frame.commit(Effect.succeed(a))), ), - withLock: (self: Effect.Effect) => Effect.flatMap(effect, l => asLensImpl(l).withLock(self)), + lock: Effect.flatMap(effect, l => asLensImpl(l).lock), }) /** @@ -344,7 +349,7 @@ export const map: { }), ), mapStream: Stream.map(get), - withLock: identity, + mapLock: identity, })) /** @@ -376,7 +381,7 @@ export const mapEffect: { ), ), mapStream: Stream.mapEffect(get), - withLock: identity, + mapLock: lock => lock as Effect.Effect, })) /** @@ -462,7 +467,7 @@ export const mapStream: { ): Lens => derive(self, { resolve: identity, mapStream: f, - withLock: identity, + mapLock: identity, })) @@ -485,7 +490,7 @@ export const mapErrorRead: { ): Lens => derive(self, { resolve: Effect.mapError(f), mapStream: Stream.mapError(f), - withLock: identity, + mapLock: identity, })) /** @@ -514,7 +519,7 @@ export const mapErrorWrite: { ), })), mapStream: identity, - withLock: identity, + mapLock: Effect.mapError(f), })) /** @@ -546,7 +551,7 @@ export const mapError: { }), ), mapStream: Stream.mapError(f), - withLock: identity, + mapLock: Effect.mapError(f), })) /** @@ -571,7 +576,7 @@ export const catchAllRead: { error => asLensImpl(f(error)).resolve as Effect.Effect, E2, R2>, ), mapStream: Stream.catchAll(error => f(error).changes), - withLock: identity, + mapLock: lock => lock as Effect.Effect, } as DerivedLensImpl.Source)) /** @@ -599,7 +604,10 @@ export const catchAllWrite: { ), })), mapStream: identity, - withLock: identity, + mapLock: lock => Effect.catchAll( + lock, + error => Effect.as(f(error), identityLock), + ), })) /** @@ -621,7 +629,7 @@ export const tapErrorRead: { ): Lens => derive(self, { resolve: Effect.tapError(f), mapStream: Stream.tapError(f), - withLock: identity, + mapLock: lock => lock, })) /** @@ -650,7 +658,7 @@ export const tapErrorWrite: { ), })), mapStream: identity, - withLock: identity, + mapLock: lock => Effect.tapError(lock, f), })) /** @@ -682,7 +690,7 @@ export const tapError: { }), ), mapStream: Stream.tapError(f), - withLock: identity, + mapLock: lock => Effect.tapError(lock, f), })) @@ -709,7 +717,10 @@ export const provideContext: { }), ), mapStream: Stream.provideSomeContext(context), - withLock: parentWithLock => self => Effect.provide(parentWithLock(Effect.provide(self, context)), context), + mapLock: lock => Effect.map( + Effect.provide(lock, context), + lock => self => Effect.provide(lock(Effect.provide(self, context)), context), + ), })) /** @@ -741,7 +752,10 @@ export const provideService: { }), ), mapStream: Stream.provideService(tag, service), - withLock: parentWithLock => self => Effect.provideService(parentWithLock(Effect.provideService(self, tag, service)), tag, service), + mapLock: lock => Effect.map( + Effect.provideService(lock, tag, service), + lock => self => Effect.provideService(lock(Effect.provideService(self, tag, service)), tag, service), + ), })) diff --git a/packages/effect-lens/src/tests.ts b/packages/effect-lens/src/tests.ts index 09d16b7..6cb06f5 100644 --- a/packages/effect-lens/src/tests.ts +++ b/packages/effect-lens/src/tests.ts @@ -33,7 +33,7 @@ Effect.gen(function*() { get: Effect.succeed("User not found"), changes: Stream.make("User not found"), commit: () => Console.log("Test"), - withLock: Effect.unsafeMakeSemaphore(1).withPermits(1), + lock: Effect.succeed(Effect.unsafeMakeSemaphore(1).withPermits(1)), })), )