0.2.0 #5

Merged
Thilawyn merged 59 commits from next into master 2026-05-30 06:10:54 +02:00
3 changed files with 53 additions and 39 deletions
Showing only changes of commit b817d1f8b3 - Show all commits
+6 -6
View File
@@ -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),
)
+41 -27
View File
@@ -38,6 +38,10 @@ export declare namespace LensImpl {
next: Effect.Effect<A, E, R>
) => Effect.Effect<void, EW | E, RW | R>
}
export interface Lock {
<A1, E1, R1>(self: Effect.Effect<A1, E1, R1>): Effect.Effect<A1, E1, R1>
}
}
export abstract class LensImpl<in out A, in out ER = never, in out EW = never, in out RR = never, in out RW = never>
@@ -49,20 +53,23 @@ extends Pipeable.Class() implements Lens<A, ER, EW, RR, RW> {
abstract readonly resolve: Effect.Effect<LensImpl.Frame<A, EW, RW>, ER, RR>
abstract readonly changes: Stream.Stream<A, ER, RR>
abstract readonly withLock: <A1, E1, R1>(self: Effect.Effect<A1, E1, R1>) => Effect.Effect<A1, E1, R1>
abstract readonly lock: Effect.Effect<LensImpl.Lock, EW, RW>
get get() { return Effect.map(this.resolve, frame => frame.value) }
modifyEffect<B, E1 = never, R1 = never>(
f: (a: A) => Effect.Effect<readonly [B, A], E1, R1>,
): Effect.Effect<B, ER | EW | E1, RR | RW | R1> {
return this.withLock(Effect.flatMap(
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<A, ER, RR>
readonly changes: Stream.Stream<A, ER, RR>
readonly commit: (a: A) => Effect.Effect<void, EW, RW>
readonly withLock: <A1, E1, R1>(self: Effect.Effect<A1, E1, R1>) => Effect.Effect<A1, E1, R1>
readonly lock: Effect.Effect<LensImpl.Lock, EW, RW>
}
}
@@ -104,7 +111,7 @@ extends LensImpl<A, ER, EW, RR, RW> {
)
}
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<LensImpl.Frame<B, ESW, RSW>, ESR, RSR>) => Effect.Effect<LensImpl.Frame<A, EW, RW>, ER, RR>
readonly mapStream: (stream: Stream.Stream<B, ESR, RSR>) => Stream.Stream<A, ER, RR>
readonly withLock: (
withLock: <A1, E1, R1>(self: Effect.Effect<A1, E1, R1>) => Effect.Effect<A1, E1, R1>
) => <A1, E1, R1>(self: Effect.Effect<A1, E1, R1>) => Effect.Effect<A1, E1, R1>
readonly mapLock: (lock: Effect.Effect<LensImpl.Lock, ESW, RSW>) => Effect.Effect<LensImpl.Lock, EW, RW>
}
}
@@ -158,7 +163,7 @@ extends LensImpl<A, ER, EW, RR, RW> {
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<A, never, never, never, never> {
)
}
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<in out A>
extends SynchronizedRef.SynchronizedRef<A> {
readonly ref: Ref.Ref<A>
readonly withLock: <A1, E1, R1>(self: Effect.Effect<A1, E1, R1>) => Effect.Effect<A1, E1, R1>
readonly withLock: LensImpl.Lock
}
}
@@ -245,7 +250,7 @@ extends LensImpl<A, never, never, never, never> {
)
}
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<A, never, never, never, never> {
)
}
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 = <A, ER, EW, RR, RW, E1, R1>(
effect,
l => Effect.flatMap(asLensImpl(l).resolve, frame => frame.commit(Effect.succeed(a))),
),
withLock: <A1, E1, R1>(self: Effect.Effect<A1, E1, R1>) => 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<LensImpl.Lock, EW | ESet, RW | RSet>,
}))
/**
@@ -462,7 +467,7 @@ export const mapStream: {
): Lens<A, ER, EW, RR, RW> => derive(self, {
resolve: identity,
mapStream: f,
withLock: identity,
mapLock: identity,
}))
@@ -485,7 +490,7 @@ export const mapErrorRead: {
): Lens<A, E2, EW, RR, RW> => 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<LensImpl.Frame<A, EW | EW2, RW | RW2>, E2, R2>,
),
mapStream: Stream.catchAll(error => f(error).changes),
withLock: identity,
mapLock: lock => lock as Effect.Effect<LensImpl.Lock, EW | EW2, RW | RW2>,
} as DerivedLensImpl.Source<A, A, E2, ER, EW | EW2, EW, RR | R2, RR, RW | RW2, RW>))
/**
@@ -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<A, ER | E2, EW, RR | R2, RW> => 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),
),
}))
+1 -1
View File
@@ -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)),
})),
)