+174
-221
@@ -1,4 +1,4 @@
|
||||
import { Array, Chunk, type Context, Effect, Function, identity, type ManagedRuntime, Option, Pipeable, Predicate, PubSub, Readable, Ref, type Runtime, Stream, type SubscriptionRef, type SynchronizedRef } from "effect"
|
||||
import { Array, Chunk, type Context, Effect, Function, identity, Option, Pipeable, Predicate, PubSub, Readable, Ref, Stream, type SubscriptionRef, type SynchronizedRef } from "effect"
|
||||
import type { NoSuchElementException } from "effect/Cause"
|
||||
import * as Subscribable from "./Subscribable.js"
|
||||
|
||||
@@ -28,60 +28,24 @@ extends Subscribable.Subscribable<A, ER, RR> {
|
||||
export const isLens = (u: unknown): u is Lens<unknown, unknown, unknown, unknown, unknown> => Predicate.hasProperty(u, LensTypeId)
|
||||
|
||||
|
||||
export const LensStepTypeId: unique symbol = Symbol.for("@effect-fc/Lens/LensStep")
|
||||
export type LensStepTypeId = typeof LensStepTypeId
|
||||
|
||||
export interface LensFrame<in out A, in out EW = never, in out RW = never> {
|
||||
readonly value: A
|
||||
readonly commit: <E = never, R = never>(
|
||||
next: Effect.Effect<A, E, R>
|
||||
) => Effect.Effect<void, EW | E, RW | R>
|
||||
}
|
||||
|
||||
export interface LensStep<
|
||||
in out A,
|
||||
in out B,
|
||||
in out ER = never,
|
||||
in out ESR = never,
|
||||
in out EW = never,
|
||||
in out ESW = never,
|
||||
in out RR = never,
|
||||
in out RSR = never,
|
||||
in out RW = never,
|
||||
in out RSW = never,
|
||||
> {
|
||||
readonly [LensStepTypeId]: LensStepTypeId
|
||||
readonly access: (effect: Effect.Effect<LensFrame<B, ESW, RSW>, ESR, RSR>) => Effect.Effect<LensFrame<A, EW, RW>, ER, RR>
|
||||
readonly transformStream: (stream: Stream.Stream<B, ESR, RSR>) => Stream.Stream<A, ER, RR>
|
||||
}
|
||||
|
||||
export const isLensStep = (u: unknown): u is LensStep<unknown, unknown, unknown, unknown, unknown, unknown, unknown, unknown, unknown, unknown> => Predicate.hasProperty(u, LensStepTypeId)
|
||||
|
||||
|
||||
export const LensImplTypeId: unique symbol = Symbol.for("@effect-fc/Lens/LensImpl")
|
||||
export type LensImplTypeId = typeof LensImplTypeId
|
||||
|
||||
export const isLensImpl = (u: unknown): u is LensImpl<unknown, unknown, unknown, unknown, unknown, unknown, unknown, unknown, unknown, unknown> => Predicate.hasProperty(u, LensImplTypeId)
|
||||
|
||||
export const asLensImpl = <A, ER, EW, RR, RW>(
|
||||
lens: Lens<A, ER, EW, RR, RW>
|
||||
): LensImpl<A, unknown, ER, unknown, EW, unknown, RR, unknown, RW, unknown> => {
|
||||
if (!isLensImpl(lens))
|
||||
throw new Error("Not a 'LensImpl'.")
|
||||
return lens as LensImpl<A, unknown, ER, unknown, EW, unknown, RR, unknown, RW, unknown>
|
||||
export declare namespace LensImpl {
|
||||
export interface Frame<in out A, in out EW = never, in out RW = never> {
|
||||
readonly value: A
|
||||
readonly commit: <E = never, R = never>(
|
||||
next: Effect.Effect<A, E, R>
|
||||
) => Effect.Effect<void, EW | E, RW | R>
|
||||
}
|
||||
}
|
||||
|
||||
export abstract class LensImpl<
|
||||
in out A,
|
||||
in out B,
|
||||
in out ER = never,
|
||||
in out ESR = never,
|
||||
in out EW = never,
|
||||
in out ESW = never,
|
||||
in out RR = never,
|
||||
in out RSR = never,
|
||||
in out RW = never,
|
||||
in out RSW = never,
|
||||
>
|
||||
extends Pipeable.Class() implements Lens<A, ER, EW, RR, RW> {
|
||||
readonly [Readable.TypeId]: Readable.TypeId = Readable.TypeId
|
||||
@@ -89,39 +53,14 @@ extends Pipeable.Class() implements Lens<A, ER, EW, RR, RW> {
|
||||
readonly [LensTypeId]: LensTypeId = LensTypeId
|
||||
readonly [LensImplTypeId]: LensImplTypeId = LensImplTypeId
|
||||
|
||||
readonly steps: readonly LensStep<unknown, unknown, unknown, unknown, unknown, unknown, unknown, unknown, unknown, unknown>[] = []
|
||||
|
||||
abstract readonly sourceGet: Effect.Effect<B, ESR, RSR>
|
||||
abstract readonly sourceChanges: Stream.Stream<B, ESR, RSR>
|
||||
abstract sourceCommit(b: B): Effect.Effect<void, ESW, RSW>
|
||||
abstract readonly access: 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, EW | E1, RW | R1>
|
||||
|
||||
get access(): Effect.Effect<LensFrame<A, EW, RW>, ER, RR> {
|
||||
let effect: Effect.Effect<LensFrame<unknown, unknown, unknown>, unknown, unknown> = Effect.map(
|
||||
this.sourceGet,
|
||||
value => ({
|
||||
value,
|
||||
commit: next => Effect.flatMap(next, value => this.sourceCommit(value as B)),
|
||||
}),
|
||||
)
|
||||
|
||||
for (const step of this.steps)
|
||||
effect = step.access(effect) as Effect.Effect<LensFrame<unknown, unknown, unknown>, unknown, unknown>
|
||||
|
||||
return effect as Effect.Effect<LensFrame<A, EW, RW>, ER, RR>
|
||||
}
|
||||
|
||||
get get(): Effect.Effect<A, ER, RR> {
|
||||
return Effect.map(this.access, frame => frame.value)
|
||||
}
|
||||
|
||||
get changes(): Stream.Stream<A, ER, RR> {
|
||||
let stream: Stream.Stream<unknown, unknown, unknown> = this.sourceChanges
|
||||
for (const step of this.steps)
|
||||
stream = step.transformStream(stream)
|
||||
return stream as Stream.Stream<A, ER, RR>
|
||||
}
|
||||
|
||||
modifyEffect<C, E1 = never, R1 = never>(
|
||||
f: (a: A) => Effect.Effect<readonly [C, A], E1, R1>,
|
||||
): Effect.Effect<C, ER | EW | E1, RR | RW | R1> {
|
||||
@@ -135,77 +74,128 @@ extends Pipeable.Class() implements Lens<A, ER, EW, RR, RW> {
|
||||
}
|
||||
}
|
||||
|
||||
export const isLensImpl = (u: unknown): u is LensImpl<unknown, unknown, unknown, unknown, unknown> => Predicate.hasProperty(u, LensImplTypeId)
|
||||
|
||||
export const asLensImpl = <A, ER, EW, RR, RW>(
|
||||
lens: Lens<A, ER, EW, RR, RW>
|
||||
): LensImpl<A, ER, EW, RR, RW> => {
|
||||
if (!isLensImpl(lens))
|
||||
throw new Error("Not a 'LensImpl'.")
|
||||
return lens as LensImpl<A, ER, EW, RR, RW>
|
||||
}
|
||||
|
||||
|
||||
export declare namespace LensLazyImpl {
|
||||
export interface Source<in out B, in out ESW = never, in out ESR = never, in out RSR = never, in out RSW = never> {
|
||||
readonly sourceGet: Effect.Effect<B, ESR, RSR>
|
||||
readonly sourceChanges: Stream.Stream<B, ESR, RSR>
|
||||
readonly sourceCommit: (b: B) => Effect.Effect<void, ESW, RSW>
|
||||
readonly get: Effect.Effect<B, ESR, RSR>
|
||||
readonly changes: Stream.Stream<B, ESR, RSR>
|
||||
readonly commit: (b: B) => Effect.Effect<void, ESW, RSW>
|
||||
readonly withLock: <A1, E1, R1>(self: Effect.Effect<A1, E1, R1>) => Effect.Effect<A1, E1, R1>
|
||||
}
|
||||
}
|
||||
|
||||
export class LensLazyImpl<
|
||||
in out A,
|
||||
in out B,
|
||||
in out ER = never,
|
||||
in out ESR = never,
|
||||
in out EW = never,
|
||||
in out ESW = never,
|
||||
in out RR = never,
|
||||
in out ESR = never,
|
||||
in out RSR = never,
|
||||
in out RW = never,
|
||||
in out RSW = never,
|
||||
>
|
||||
extends LensImpl<A, B, ER, ESR, EW, ESW, RR, RSR, RW, RSW> {
|
||||
extends LensImpl<B, ESR, ESW, RSR, RSW> {
|
||||
constructor(
|
||||
readonly source: LensLazyImpl.Source<B, ESW, ESR, RSR, RSW>,
|
||||
) {
|
||||
super()
|
||||
}
|
||||
|
||||
get sourceGet() { return this.source.sourceGet }
|
||||
get sourceChanges() { return this.source.sourceChanges }
|
||||
sourceCommit(b: B) { return this.source.sourceCommit(b) }
|
||||
get access(): Effect.Effect<LensImpl.Frame<B, ESW, RSW>, ESR, RSR> {
|
||||
return Effect.map(
|
||||
this.source.get,
|
||||
value => ({
|
||||
value,
|
||||
commit: next => Effect.flatMap(next, value => this.source.commit(value)),
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
get changes() { return this.source.changes }
|
||||
get withLock() { return this.source.withLock }
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a `Lens` by supplying how to read the current value, observe changes, and apply transformations.
|
||||
*/
|
||||
export const makeLazy = <B, ESW, ESR, RSR, RSW>(
|
||||
export const make = <B, ESW, ESR, RSR, RSW>(
|
||||
source: LensLazyImpl.Source<B, ESW, ESR, RSR, RSW>
|
||||
): Lens<B, ESR, ESW, RSR, RSW> => new LensLazyImpl(source)
|
||||
|
||||
|
||||
export declare namespace DerivedLensImpl {
|
||||
export interface Source<
|
||||
in out A,
|
||||
in out B,
|
||||
in out ER = never,
|
||||
in out ESR = never,
|
||||
in out EW = never,
|
||||
in out ESW = never,
|
||||
in out RR = never,
|
||||
in out RSR = never,
|
||||
in out RW = never,
|
||||
in out RSW = never,
|
||||
> {
|
||||
readonly access: (effect: Effect.Effect<LensImpl.Frame<B, ESW, RSW>, ESR, RSR>) => Effect.Effect<LensImpl.Frame<A, EW, RW>, ER, RR>
|
||||
readonly transformStream: (stream: Stream.Stream<B, ESR, RSR>) => Stream.Stream<A, ER, RR>
|
||||
}
|
||||
}
|
||||
|
||||
export class DerivedLensImpl<
|
||||
in out A,
|
||||
in out B,
|
||||
in out ER = never,
|
||||
in out PER = never,
|
||||
in out EW = never,
|
||||
in out PEW = never,
|
||||
in out RR = never,
|
||||
in out PRR = never,
|
||||
in out RW = never,
|
||||
in out PRW = never,
|
||||
>
|
||||
extends LensImpl<A, ER, EW, RR, RW> {
|
||||
constructor(
|
||||
readonly parent: LensImpl<B, PER, PEW, PRR, PRW>,
|
||||
readonly source: DerivedLensImpl.Source<A, B, ER, PER, EW, PEW, RR, PRR, RW, PRW>,
|
||||
) {
|
||||
super()
|
||||
}
|
||||
|
||||
get access(): Effect.Effect<LensImpl.Frame<A, EW, RW>, ER, RR> {
|
||||
return this.source.access(this.parent.access)
|
||||
}
|
||||
|
||||
get changes(): Stream.Stream<A, ER, RR> {
|
||||
return this.source.transformStream(this.parent.changes)
|
||||
}
|
||||
|
||||
get withLock() {
|
||||
return this.parent.withLock
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Derives a new `Lens` by immutably appending a step to an existing `LensImpl`.
|
||||
* Derives a new `Lens` by linking a step to an existing parent lens.
|
||||
*/
|
||||
export const derive: {
|
||||
<A, B, ER, ESR, EW, ESW, RR, RSR, RW, RSW, C, ER2, EW2, RR2, RW2>(
|
||||
self: LensImpl<A, B, ER, ESR, EW, ESW, RR, RSR, RW, RSW>,
|
||||
step: LensStep<C, A, ER2, ER, EW2, EW, RR2, RR, RW2, RW>,
|
||||
<A, ER, EW, RR, RW, C, ER2, EW2, RR2, RW2>(
|
||||
self: Lens<A, ER, EW, RR, RW>,
|
||||
source: DerivedLensImpl.Source<C, A, ER2, ER, EW2, EW, RR2, RR, RW2, RW>,
|
||||
): Lens<C, ER2, EW2, RR2, RW2>
|
||||
<A, ER, EW, RR, RW, C, ER2, EW2, RR2, RW2>(
|
||||
step: LensStep<C, A, ER2, ER, EW2, EW, RR2, RR, RW2, RW>,
|
||||
): <B, ESR, ESW, RSR, RSW>(
|
||||
self: LensImpl<A, B, ER, ESR, EW, ESW, RR, RSR, RW, RSW>
|
||||
) => Lens<C, ER2, EW2, RR2, RW2>
|
||||
} = Function.dual(2, <A, B, ER, ESR, EW, ESW, RR, RSR, RW, RSW, C, ER2, EW2, RR2, RW2>(
|
||||
self: LensImpl<A, B, ER, ESR, EW, ESW, RR, RSR, RW, RSW>,
|
||||
step: LensStep<C, A, ER2, ER, EW2, EW, RR2, RR, RW2, RW>,
|
||||
): Lens<C, ER2, EW2, RR2, RW2> => Object.defineProperty(
|
||||
Object.defineProperties(
|
||||
Object.create(Object.getPrototypeOf(self)),
|
||||
Object.getOwnPropertyDescriptors(self),
|
||||
),
|
||||
"steps",
|
||||
{
|
||||
configurable: true,
|
||||
enumerable: true,
|
||||
value: [...self.steps, step as LensStep<unknown, unknown, unknown, unknown, unknown, unknown, unknown, unknown, unknown, unknown>],
|
||||
writable: false,
|
||||
},
|
||||
) as Lens<C, ER2, EW2, RR2, RW2>)
|
||||
source: DerivedLensImpl.Source<C, A, ER2, ER, EW2, EW, RR2, RR, RW2, RW>,
|
||||
): (self: Lens<A, ER, EW, RR, RW>) => Lens<C, ER2, EW2, RR2, RW2>
|
||||
} = Function.dual(2, <A, ER, EW, RR, RW, C, ER2, EW2, RR2, RW2>(
|
||||
self: Lens<A, ER, EW, RR, RW>,
|
||||
source: DerivedLensImpl.Source<C, A, ER2, ER, EW2, EW, RR2, RR, RW2, RW>,
|
||||
): Lens<C, ER2, EW2, RR2, RW2> => new DerivedLensImpl(asLensImpl(self), source))
|
||||
|
||||
|
||||
export declare namespace SynchronizedRefLensImpl {
|
||||
@@ -217,7 +207,7 @@ export declare namespace SynchronizedRefLensImpl {
|
||||
}
|
||||
|
||||
export class SynchronizedRefLensImpl<in out A>
|
||||
extends LensImpl<A, A, never, never, never, never, never, never, never, never> {
|
||||
extends LensImpl<A, never, never, never, never> {
|
||||
readonly ref: SynchronizedRefLensImpl.SynchronizedRefWithInternals<A>
|
||||
|
||||
constructor(
|
||||
@@ -227,9 +217,17 @@ extends LensImpl<A, A, never, never, never, never, never, never, never, never> {
|
||||
this.ref = ref as SynchronizedRefLensImpl.SynchronizedRefWithInternals<A>
|
||||
}
|
||||
|
||||
get sourceGet() { return this.ref.get }
|
||||
get sourceChanges() { return Stream.unwrap(Effect.map(this.ref.get, Stream.make)) }
|
||||
sourceCommit(a: A) { return Ref.set(this.ref.ref, a) }
|
||||
get access(): Effect.Effect<LensImpl.Frame<A>, never, never> {
|
||||
return Effect.map(
|
||||
this.ref.get,
|
||||
value => ({
|
||||
value,
|
||||
commit: next => Effect.flatMap(next, value => Ref.set(this.ref.ref, value)),
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
get changes() { return Stream.unwrap(Effect.map(this.ref.get, Stream.make)) }
|
||||
get withLock() { return this.ref.withLock }
|
||||
}
|
||||
|
||||
@@ -254,7 +252,7 @@ export declare namespace SubscriptionRefLensImpl {
|
||||
}
|
||||
|
||||
export class SubscriptionRefLensImpl<in out A>
|
||||
extends LensImpl<A, A, never, never, never, never, never, never, never, never> {
|
||||
extends LensImpl<A, never, never, never, never> {
|
||||
readonly ref: SubscriptionRefLensImpl.SubscriptionRefWithInternals<A>
|
||||
|
||||
constructor(
|
||||
@@ -264,9 +262,18 @@ extends LensImpl<A, A, never, never, never, never, never, never, never, never> {
|
||||
this.ref = ref as SubscriptionRefLensImpl.SubscriptionRefWithInternals<A>
|
||||
}
|
||||
|
||||
get sourceGet() { return this.ref.get }
|
||||
get sourceChanges() { return this.ref.changes }
|
||||
sourceCommit(a: A) {
|
||||
get access(): Effect.Effect<LensImpl.Frame<A>, never, never> {
|
||||
return Effect.map(
|
||||
this.ref.get,
|
||||
value => ({
|
||||
value,
|
||||
commit: next => Effect.flatMap(next, value => this.commit(value)),
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
get changes() { return this.ref.changes }
|
||||
commit(a: A) {
|
||||
return Effect.zipLeft(
|
||||
Ref.set(this.ref.ref, a),
|
||||
PubSub.publish(this.ref.pubsub, a),
|
||||
@@ -288,10 +295,10 @@ export const fromSubscriptionRef = <A>(
|
||||
*/
|
||||
export const unwrap = <A, ER, EW, RR, RW, E1, R1>(
|
||||
effect: Effect.Effect<Lens<A, ER, EW, RR, RW>, E1, R1>
|
||||
): Lens<A, ER | E1, ER | EW | E1, RR | R1, RR | RW | R1> => makeLazy<A, ER | EW | E1, ER | E1, RR | R1, RR | RW | R1>({
|
||||
sourceGet: Effect.flatMap(effect, l => l.get),
|
||||
sourceChanges: Stream.unwrap(Effect.map(effect, l => l.changes)),
|
||||
sourceCommit: a => Effect.flatMap(
|
||||
): Lens<A, ER | E1, ER | EW | E1, RR | R1, RR | RW | R1> => make({
|
||||
get: Effect.flatMap(effect, l => l.get),
|
||||
changes: Stream.unwrap(Effect.map(effect, l => l.changes)),
|
||||
commit: a => Effect.flatMap(
|
||||
effect,
|
||||
l => Effect.flatMap(asLensImpl(l).access, frame => frame.commit(Effect.succeed(a))),
|
||||
),
|
||||
@@ -338,25 +345,19 @@ export const mapEffect: {
|
||||
self: Lens<A, ER, EW, RR, RW>,
|
||||
get: (a: NoInfer<A>) => Effect.Effect<B, EGet, RGet>,
|
||||
set: (a: NoInfer<A>, b: B) => Effect.Effect<NoInfer<A>, ESet, RSet>,
|
||||
): Lens<B, ER | EGet, EW | ESet, RR | RGet, RW | RSet> => {
|
||||
return derive(
|
||||
asLensImpl(self),
|
||||
{
|
||||
[LensStepTypeId]: LensStepTypeId,
|
||||
access: parent => Effect.flatMap(
|
||||
parent,
|
||||
frame => Effect.map(
|
||||
get(frame.value),
|
||||
value => ({
|
||||
value,
|
||||
commit: next => frame.commit(Effect.flatMap(next, b => set(frame.value, b))),
|
||||
}),
|
||||
),
|
||||
),
|
||||
transformStream: stream => Stream.mapEffect(stream, get),
|
||||
},
|
||||
)
|
||||
})
|
||||
): Lens<B, ER | EGet, EW | ESet, RR | RGet, RW | RSet> => derive(self, {
|
||||
access: parent => Effect.flatMap(
|
||||
parent,
|
||||
frame => Effect.map(
|
||||
get(frame.value),
|
||||
value => ({
|
||||
value,
|
||||
commit: next => frame.commit(Effect.flatMap(next, b => set(frame.value, b))),
|
||||
}),
|
||||
),
|
||||
),
|
||||
transformStream: Stream.mapEffect(get),
|
||||
}))
|
||||
|
||||
/**
|
||||
* Derives a new `Lens` by applying synchronous getters and setters over the value inside an `Option`.
|
||||
@@ -438,11 +439,10 @@ export const mapStream: {
|
||||
} = Function.dual(2, <A, ER, EW, RR, RW>(
|
||||
self: Lens<A, ER, EW, RR, RW>,
|
||||
f: (changes: Stream.Stream<NoInfer<A>, NoInfer<ER>, NoInfer<RR>>) => Stream.Stream<NoInfer<A>, NoInfer<ER>, NoInfer<RR>>,
|
||||
): Lens<A, ER, EW, RR, RW> => derive(asLensImpl(self), {
|
||||
[LensStepTypeId]: LensStepTypeId,
|
||||
access: parent => parent,
|
||||
): Lens<A, ER, EW, RR, RW> => derive(self, {
|
||||
access: identity,
|
||||
transformStream: f,
|
||||
} as LensStep<A, A, ER, ER, EW, EW, RR, RR, RW, RW>))
|
||||
}))
|
||||
|
||||
|
||||
/**
|
||||
@@ -461,11 +461,10 @@ export const mapErrorRead: {
|
||||
} = Function.dual(2, <A, ER, EW, RR, RW, E2>(
|
||||
self: Lens<A, ER, EW, RR, RW>,
|
||||
f: (error: NoInfer<ER>) => E2,
|
||||
): Lens<A, E2, EW, RR, RW> => derive(asLensImpl(self), {
|
||||
[LensStepTypeId]: LensStepTypeId,
|
||||
access: parent => Effect.mapError(parent, f),
|
||||
transformStream: stream => Stream.mapError(stream, f),
|
||||
} as LensStep<A, A, E2, ER, EW, EW, RR, RR, RW, RW>))
|
||||
): Lens<A, E2, EW, RR, RW> => derive(self, {
|
||||
access: Effect.mapError(f),
|
||||
transformStream: Stream.mapError(f),
|
||||
}))
|
||||
|
||||
/**
|
||||
* Transforms modify errors of a `Lens`.
|
||||
@@ -484,14 +483,13 @@ export const mapErrorWrite: {
|
||||
} = Function.dual(2, <A, ER, EW, RR, RW, E2>(
|
||||
self: Lens<A, ER, EW, RR, RW>,
|
||||
f: (error: unknown) => E2,
|
||||
): Lens<A, ER, E2, RR, RW> => derive(asLensImpl(self), {
|
||||
[LensStepTypeId]: LensStepTypeId,
|
||||
): Lens<A, ER, E2, RR, RW> => derive(self, {
|
||||
access: parent => Effect.map(parent, frame => ({
|
||||
value: frame.value,
|
||||
commit: next => Effect.mapError(frame.commit(next), f),
|
||||
})),
|
||||
transformStream: stream => stream,
|
||||
} as LensStep<A, A, ER, ER, E2, EW, RR, RR, RW, RW>))
|
||||
transformStream: identity,
|
||||
}))
|
||||
|
||||
/**
|
||||
* Transforms all errors of a `Lens`.
|
||||
@@ -510,8 +508,7 @@ export const mapError: {
|
||||
} = Function.dual(2, <A, ER, EW, RR, RW, E2>(
|
||||
self: Lens<A, ER, EW, RR, RW>,
|
||||
f: (error: unknown) => E2,
|
||||
): Lens<A, E2, E2, RR, RW> => derive(asLensImpl(self), {
|
||||
[LensStepTypeId]: LensStepTypeId,
|
||||
): Lens<A, E2, E2, RR, RW> => derive(self, {
|
||||
access: parent => Effect.map(
|
||||
Effect.mapError(parent, f),
|
||||
frame => ({
|
||||
@@ -519,8 +516,8 @@ export const mapError: {
|
||||
commit: next => Effect.mapError(frame.commit(next), f),
|
||||
}),
|
||||
),
|
||||
transformStream: stream => Stream.mapError(stream, f),
|
||||
} as LensStep<A, A, E2, ER, E2, EW, RR, RR, RW, RW>))
|
||||
transformStream: Stream.mapError(f),
|
||||
}))
|
||||
|
||||
/**
|
||||
* Recovers from read failures of a `Lens`.
|
||||
@@ -538,11 +535,10 @@ export const catchAllRead: {
|
||||
} = Function.dual(2, <A, ER, EW, RR, RW, E2, R2>(
|
||||
self: Lens<A, ER, EW, RR, RW>,
|
||||
f: (error: NoInfer<ER>) => Subscribable.Subscribable<A, E2, R2>,
|
||||
): Lens<A, E2, EW, RR | R2, RW> => derive(asLensImpl(self), {
|
||||
[LensStepTypeId]: LensStepTypeId,
|
||||
access: parent => Effect.catchAll(parent, error => asLensImpl(f(error) as Lens<A, E2, EW, R2, RW>).access),
|
||||
transformStream: stream => Stream.catchAll(stream, error => f(error).changes),
|
||||
} as LensStep<A, A, E2, ER, EW, EW, RR | R2, RR, RW, RW>))
|
||||
): Lens<A, E2, EW, RR | R2, RW> => derive(self, {
|
||||
access: Effect.catchAll(error => asLensImpl(f(error) as Lens<A, E2, EW, R2, RW>).access),
|
||||
transformStream: Stream.catchAll(error => f(error).changes),
|
||||
}))
|
||||
|
||||
/**
|
||||
* Runs an effect when read failures occur.
|
||||
@@ -560,11 +556,10 @@ export const tapErrorRead: {
|
||||
} = Function.dual(2, <A, ER, EW, RR, RW, E2, R2>(
|
||||
self: Lens<A, ER, EW, RR, RW>,
|
||||
f: (error: NoInfer<ER>) => Effect.Effect<any, E2, R2>,
|
||||
): Lens<A, ER | E2, EW, RR | R2, RW> => derive(asLensImpl(self), {
|
||||
[LensStepTypeId]: LensStepTypeId,
|
||||
access: parent => Effect.tapError(parent, f),
|
||||
transformStream: stream => Stream.tapError(stream, f),
|
||||
} as LensStep<A, A, ER | E2, ER, EW, EW, RR | R2, RR, RW, RW>))
|
||||
): Lens<A, ER | E2, EW, RR | R2, RW> => derive(self, {
|
||||
access: Effect.tapError(f),
|
||||
transformStream: Stream.tapError(f),
|
||||
}))
|
||||
|
||||
/**
|
||||
* Runs an effect when modify failures occur.
|
||||
@@ -583,14 +578,13 @@ export const tapErrorWrite: {
|
||||
} = Function.dual(2, <A, ER, EW, RR, RW, E2, R2>(
|
||||
self: Lens<A, ER, EW, RR, RW>,
|
||||
f: (error: unknown) => Effect.Effect<any, E2, R2>,
|
||||
): Lens<A, ER, EW | E2, RR, RW | R2> => derive(asLensImpl(self), {
|
||||
[LensStepTypeId]: LensStepTypeId,
|
||||
): Lens<A, ER, EW | E2, RR, RW | R2> => derive(self, {
|
||||
access: parent => Effect.map(parent, frame => ({
|
||||
value: frame.value,
|
||||
commit: next => Effect.tapError(frame.commit(next), f),
|
||||
})),
|
||||
transformStream: stream => stream,
|
||||
} as LensStep<A, A, ER, ER, EW | E2, EW, RR, RR, RW | R2, RW>))
|
||||
transformStream: identity,
|
||||
}))
|
||||
|
||||
/**
|
||||
* Runs an effect when any `Lens` failure occurs.
|
||||
@@ -609,8 +603,7 @@ export const tapError: {
|
||||
} = Function.dual(2, <A, ER, EW, RR, RW, E2, R2>(
|
||||
self: Lens<A, ER, EW, RR, RW>,
|
||||
f: (error: unknown) => Effect.Effect<any, E2, R2>,
|
||||
): Lens<A, ER | E2, EW | E2, RR | R2, RW | R2> => derive(asLensImpl(self), {
|
||||
[LensStepTypeId]: LensStepTypeId,
|
||||
): Lens<A, ER | E2, EW | E2, RR | R2, RW | R2> => derive(self, {
|
||||
access: parent => Effect.map(
|
||||
Effect.tapError(parent, f),
|
||||
frame => ({
|
||||
@@ -618,8 +611,8 @@ export const tapError: {
|
||||
commit: next => Effect.tapError(frame.commit(next), f),
|
||||
}),
|
||||
),
|
||||
transformStream: stream => Stream.tapError(stream, f),
|
||||
} as LensStep<A, A, ER | E2, ER, EW | E2, EW, RR | R2, RR, RW | R2, RW>))
|
||||
transformStream: Stream.tapError(f),
|
||||
}))
|
||||
|
||||
|
||||
/**
|
||||
@@ -636,8 +629,7 @@ export const provideContext: {
|
||||
} = Function.dual(2, <A, ER, EW, RR, RW, R2>(
|
||||
self: Lens<A, ER, EW, RR, RW>,
|
||||
context: Context.Context<R2>,
|
||||
): Lens<A, ER, EW, Exclude<RR, R2>, Exclude<RW, R2>> => derive(asLensImpl(self), {
|
||||
[LensStepTypeId]: LensStepTypeId,
|
||||
): Lens<A, ER, EW, Exclude<RR, R2>, Exclude<RW, R2>> => derive(self, {
|
||||
access: parent => Effect.map(
|
||||
Effect.provide(parent, context),
|
||||
frame => ({
|
||||
@@ -645,46 +637,8 @@ export const provideContext: {
|
||||
commit: next => Effect.provide(frame.commit(Effect.provide(next, context)), context),
|
||||
}),
|
||||
),
|
||||
transformStream: stream => Stream.provideSomeContext(stream, context),
|
||||
} as LensStep<A, A, ER, ER, EW, EW, Exclude<RR, R2>, RR, Exclude<RW, R2>, RW>))
|
||||
|
||||
/**
|
||||
* Provides a `Runtime` or `ManagedRuntime` to a `Lens`, removing it from both the read and write environments.
|
||||
*
|
||||
* `ManagedRuntime` may add its construction errors to both the read and write error channels.
|
||||
*/
|
||||
export const provideRuntime: {
|
||||
<R2>(
|
||||
runtime: Runtime.Runtime<R2>,
|
||||
): <A, ER, EW, RR, RW>(self: Lens<A, ER, EW, RR, RW>) => Lens<A, ER, EW, Exclude<RR, R2>, Exclude<RW, R2>>
|
||||
<E2, R2>(
|
||||
managedRuntime: ManagedRuntime.ManagedRuntime<R2, E2>,
|
||||
): <A, ER, EW, RR, RW>(self: Lens<A, ER, EW, RR, RW>) => Lens<A, ER | E2, EW | E2, Exclude<RR, R2>, Exclude<RW, R2>>
|
||||
<A, ER, EW, RR, RW, R2>(
|
||||
self: Lens<A, ER, EW, RR, RW>,
|
||||
runtime: Runtime.Runtime<R2>,
|
||||
): Lens<A, ER, EW, Exclude<RR, R2>, Exclude<RW, R2>>
|
||||
<A, ER, EW, RR, RW, E2, R2>(
|
||||
self: Lens<A, ER, EW, RR, RW>,
|
||||
runtime: ManagedRuntime.ManagedRuntime<R2, E2>,
|
||||
): Lens<A, ER | E2, EW | E2, Exclude<RR, R2>, Exclude<RW, R2>>
|
||||
} = Function.dual(2, <A, ER, EW, RR, RW, E2, R2>(
|
||||
self: Lens<A, ER, EW, RR, RW>,
|
||||
runtime: Runtime.Runtime<R2>,
|
||||
) => derive(asLensImpl(self), {
|
||||
[LensStepTypeId]: LensStepTypeId,
|
||||
access: parent => Effect.map(
|
||||
Effect.provide(parent, runtime),
|
||||
frame => ({
|
||||
value: frame.value,
|
||||
commit: next => Effect.provide(frame.commit(Effect.provide(next, runtime)), runtime),
|
||||
}),
|
||||
),
|
||||
transformStream: stream => Stream.unwrap(Effect.map(
|
||||
Effect.provide(Effect.context<RR>(), runtime),
|
||||
context => Stream.provideContext(stream, context),
|
||||
)),
|
||||
} as LensStep<A, A, ER | E2, ER, EW | E2, EW, Exclude<RR, R2>, RR, Exclude<RW, R2>, RW>))
|
||||
transformStream: Stream.provideSomeContext(context),
|
||||
}))
|
||||
|
||||
/**
|
||||
* Provides a single service to a `Lens`, removing it from both the read and write environments.
|
||||
@@ -706,8 +660,7 @@ export const provideService: {
|
||||
self: Lens<A, ER, EW, RR, RW>,
|
||||
tag: Context.Tag<I, S>,
|
||||
service: NoInfer<S>,
|
||||
): Lens<A, ER, EW, Exclude<RR, I>, Exclude<RW, I>> => derive(asLensImpl(self), {
|
||||
[LensStepTypeId]: LensStepTypeId,
|
||||
): Lens<A, ER, EW, Exclude<RR, I>, Exclude<RW, I>> => derive(self, {
|
||||
access: parent => Effect.map(
|
||||
Effect.provideService(parent, tag, service),
|
||||
frame => ({
|
||||
@@ -715,8 +668,8 @@ export const provideService: {
|
||||
commit: next => Effect.provideService(frame.commit(Effect.provideService(next, tag, service)), tag, service),
|
||||
}),
|
||||
),
|
||||
transformStream: stream => Stream.provideService(stream, tag, service),
|
||||
} as LensStep<A, A, ER, ER, EW, EW, Exclude<RR, I>, RR, Exclude<RW, I>, RW>))
|
||||
transformStream: Stream.provideService(tag, service),
|
||||
}))
|
||||
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user