+163
-210
@@ -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 type { NoSuchElementException } from "effect/Cause"
|
||||||
import * as Subscribable from "./Subscribable.js"
|
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 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 const LensImplTypeId: unique symbol = Symbol.for("@effect-fc/Lens/LensImpl")
|
||||||
export type LensStepTypeId = typeof LensStepTypeId
|
export type LensImplTypeId = typeof LensImplTypeId
|
||||||
|
|
||||||
export interface LensFrame<in out A, in out EW = never, in out RW = never> {
|
export declare namespace LensImpl {
|
||||||
|
export interface Frame<in out A, in out EW = never, in out RW = never> {
|
||||||
readonly value: A
|
readonly value: A
|
||||||
readonly commit: <E = never, R = never>(
|
readonly commit: <E = never, R = never>(
|
||||||
next: Effect.Effect<A, E, R>
|
next: Effect.Effect<A, E, R>
|
||||||
) => Effect.Effect<void, EW | E, RW | 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 abstract class LensImpl<
|
export abstract class LensImpl<
|
||||||
in out A,
|
in out A,
|
||||||
in out B,
|
|
||||||
in out ER = never,
|
in out ER = never,
|
||||||
in out ESR = never,
|
|
||||||
in out EW = never,
|
in out EW = never,
|
||||||
in out ESW = never,
|
|
||||||
in out RR = never,
|
in out RR = never,
|
||||||
in out RSR = never,
|
|
||||||
in out RW = never,
|
in out RW = never,
|
||||||
in out RSW = never,
|
|
||||||
>
|
>
|
||||||
extends Pipeable.Class() implements Lens<A, ER, EW, RR, RW> {
|
extends Pipeable.Class() implements Lens<A, ER, EW, RR, RW> {
|
||||||
readonly [Readable.TypeId]: Readable.TypeId = Readable.TypeId
|
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 [LensTypeId]: LensTypeId = LensTypeId
|
||||||
readonly [LensImplTypeId]: LensImplTypeId = LensImplTypeId
|
readonly [LensImplTypeId]: LensImplTypeId = LensImplTypeId
|
||||||
|
|
||||||
readonly steps: readonly LensStep<unknown, unknown, unknown, unknown, unknown, unknown, unknown, unknown, unknown, unknown>[] = []
|
abstract readonly access: Effect.Effect<LensImpl.Frame<A, EW, RW>, ER, RR>
|
||||||
|
abstract readonly changes: Stream.Stream<A, ER, RR>
|
||||||
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 withLock: <A1, E1, R1>(self: Effect.Effect<A1, E1, R1>) => Effect.Effect<A1, EW | E1, RW | R1>
|
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> {
|
get get(): Effect.Effect<A, ER, RR> {
|
||||||
return Effect.map(this.access, frame => frame.value)
|
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>(
|
modifyEffect<C, E1 = never, R1 = never>(
|
||||||
f: (a: A) => Effect.Effect<readonly [C, A], E1, R1>,
|
f: (a: A) => Effect.Effect<readonly [C, A], E1, R1>,
|
||||||
): Effect.Effect<C, ER | EW | E1, RR | RW | R1> {
|
): Effect.Effect<C, ER | EW | E1, RR | RW | R1> {
|
||||||
@@ -135,17 +74,64 @@ 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 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> {
|
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 get: Effect.Effect<B, ESR, RSR>
|
||||||
readonly sourceChanges: Stream.Stream<B, ESR, RSR>
|
readonly changes: Stream.Stream<B, ESR, RSR>
|
||||||
readonly sourceCommit: (b: B) => Effect.Effect<void, ESW, RSW>
|
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>
|
readonly withLock: <A1, E1, R1>(self: Effect.Effect<A1, E1, R1>) => Effect.Effect<A1, E1, R1>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class LensLazyImpl<
|
export class LensLazyImpl<
|
||||||
|
in out B,
|
||||||
|
in out ESW = never,
|
||||||
|
in out ESR = never,
|
||||||
|
in out RSR = never,
|
||||||
|
in out RSW = never,
|
||||||
|
>
|
||||||
|
extends LensImpl<B, ESR, ESW, RSR, RSW> {
|
||||||
|
constructor(
|
||||||
|
readonly source: LensLazyImpl.Source<B, ESW, ESR, RSR, RSW>,
|
||||||
|
) {
|
||||||
|
super()
|
||||||
|
}
|
||||||
|
|
||||||
|
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 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 A,
|
||||||
in out B,
|
in out B,
|
||||||
in out ER = never,
|
in out ER = never,
|
||||||
@@ -156,56 +142,60 @@ export class LensLazyImpl<
|
|||||||
in out RSR = never,
|
in out RSR = never,
|
||||||
in out RW = never,
|
in out RW = never,
|
||||||
in out RSW = 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, B, ER, ESR, EW, ESW, RR, RSR, RW, RSW> {
|
extends LensImpl<A, ER, EW, RR, RW> {
|
||||||
constructor(
|
constructor(
|
||||||
readonly source: LensLazyImpl.Source<B, ESW, ESR, RSR, RSW>,
|
readonly parent: LensImpl<B, PER, PEW, PRR, PRW>,
|
||||||
|
readonly source: DerivedLensImpl.Source<A, B, ER, PER, EW, PEW, RR, PRR, RW, PRW>,
|
||||||
) {
|
) {
|
||||||
super()
|
super()
|
||||||
}
|
}
|
||||||
|
|
||||||
get sourceGet() { return this.source.sourceGet }
|
get access(): Effect.Effect<LensImpl.Frame<A, EW, RW>, ER, RR> {
|
||||||
get sourceChanges() { return this.source.sourceChanges }
|
return this.source.access(this.parent.access)
|
||||||
sourceCommit(b: B) { return this.source.sourceCommit(b) }
|
}
|
||||||
get withLock() { return this.source.withLock }
|
|
||||||
|
get changes(): Stream.Stream<A, ER, RR> {
|
||||||
|
return this.source.transformStream(this.parent.changes)
|
||||||
|
}
|
||||||
|
|
||||||
|
get withLock() {
|
||||||
|
return this.parent.withLock
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a `Lens` by supplying how to read the current value, observe changes, and apply transformations.
|
* Derives a new `Lens` by linking a step to an existing parent lens.
|
||||||
*/
|
|
||||||
export const makeLazy = <B, ESW, ESR, RSR, RSW>(
|
|
||||||
source: LensLazyImpl.Source<B, ESW, ESR, RSR, RSW>
|
|
||||||
): Lens<B, ESR, ESW, RSR, RSW> => new LensLazyImpl(source)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Derives a new `Lens` by immutably appending a step to an existing `LensImpl`.
|
|
||||||
*/
|
*/
|
||||||
export const derive: {
|
export const derive: {
|
||||||
<A, B, ER, ESR, EW, ESW, RR, RSR, RW, RSW, C, ER2, EW2, RR2, RW2>(
|
<A, ER, EW, RR, RW, C, ER2, EW2, RR2, RW2>(
|
||||||
self: LensImpl<A, B, ER, ESR, EW, ESW, RR, RSR, RW, RSW>,
|
self: Lens<A, ER, EW, RR, RW>,
|
||||||
step: LensStep<C, A, ER2, ER, EW2, EW, RR2, RR, RW2, RW>,
|
source: DerivedLensImpl.Source<C, A, ER2, ER, EW2, EW, RR2, RR, RW2, RW>,
|
||||||
): Lens<C, ER2, EW2, RR2, RW2>
|
): Lens<C, ER2, EW2, RR2, RW2>
|
||||||
<A, ER, EW, RR, RW, 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>,
|
source: DerivedLensImpl.Source<C, A, ER2, ER, EW2, EW, RR2, RR, RW2, RW>,
|
||||||
): <B, ESR, ESW, RSR, RSW>(
|
): (self: Lens<A, ER, EW, RR, RW>) => Lens<C, ER2, EW2, RR2, RW2>
|
||||||
self: LensImpl<A, B, ER, ESR, EW, ESW, RR, RSR, RW, RSW>
|
} = Function.dual(2, <A, ER, EW, RR, RW, C, ER2, EW2, RR2, RW2>(
|
||||||
) => Lens<C, ER2, EW2, RR2, RW2>
|
self: Lens<A, ER, EW, RR, RW>,
|
||||||
} = Function.dual(2, <A, B, ER, ESR, EW, ESW, RR, RSR, RW, RSW, C, ER2, EW2, RR2, RW2>(
|
source: DerivedLensImpl.Source<C, A, ER2, ER, EW2, EW, RR2, RR, RW2, RW>,
|
||||||
self: LensImpl<A, B, ER, ESR, EW, ESW, RR, RSR, RW, RSW>,
|
): Lens<C, ER2, EW2, RR2, RW2> => new DerivedLensImpl(asLensImpl(self), source))
|
||||||
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>)
|
|
||||||
|
|
||||||
|
|
||||||
export declare namespace SynchronizedRefLensImpl {
|
export declare namespace SynchronizedRefLensImpl {
|
||||||
@@ -217,7 +207,7 @@ export declare namespace SynchronizedRefLensImpl {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export class SynchronizedRefLensImpl<in out A>
|
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>
|
readonly ref: SynchronizedRefLensImpl.SynchronizedRefWithInternals<A>
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
@@ -227,9 +217,17 @@ extends LensImpl<A, A, never, never, never, never, never, never, never, never> {
|
|||||||
this.ref = ref as SynchronizedRefLensImpl.SynchronizedRefWithInternals<A>
|
this.ref = ref as SynchronizedRefLensImpl.SynchronizedRefWithInternals<A>
|
||||||
}
|
}
|
||||||
|
|
||||||
get sourceGet() { return this.ref.get }
|
get access(): Effect.Effect<LensImpl.Frame<A>, never, never> {
|
||||||
get sourceChanges() { return Stream.unwrap(Effect.map(this.ref.get, Stream.make)) }
|
return Effect.map(
|
||||||
sourceCommit(a: A) { return Ref.set(this.ref.ref, a) }
|
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 }
|
get withLock() { return this.ref.withLock }
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -254,7 +252,7 @@ export declare namespace SubscriptionRefLensImpl {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export class SubscriptionRefLensImpl<in out A>
|
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>
|
readonly ref: SubscriptionRefLensImpl.SubscriptionRefWithInternals<A>
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
@@ -264,9 +262,18 @@ extends LensImpl<A, A, never, never, never, never, never, never, never, never> {
|
|||||||
this.ref = ref as SubscriptionRefLensImpl.SubscriptionRefWithInternals<A>
|
this.ref = ref as SubscriptionRefLensImpl.SubscriptionRefWithInternals<A>
|
||||||
}
|
}
|
||||||
|
|
||||||
get sourceGet() { return this.ref.get }
|
get access(): Effect.Effect<LensImpl.Frame<A>, never, never> {
|
||||||
get sourceChanges() { return this.ref.changes }
|
return Effect.map(
|
||||||
sourceCommit(a: A) {
|
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(
|
return Effect.zipLeft(
|
||||||
Ref.set(this.ref.ref, a),
|
Ref.set(this.ref.ref, a),
|
||||||
PubSub.publish(this.ref.pubsub, 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>(
|
export const unwrap = <A, ER, EW, RR, RW, E1, R1>(
|
||||||
effect: Effect.Effect<Lens<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>({
|
): Lens<A, ER | E1, ER | EW | E1, RR | R1, RR | RW | R1> => make({
|
||||||
sourceGet: Effect.flatMap(effect, l => l.get),
|
get: Effect.flatMap(effect, l => l.get),
|
||||||
sourceChanges: Stream.unwrap(Effect.map(effect, l => l.changes)),
|
changes: Stream.unwrap(Effect.map(effect, l => l.changes)),
|
||||||
sourceCommit: a => Effect.flatMap(
|
commit: a => Effect.flatMap(
|
||||||
effect,
|
effect,
|
||||||
l => Effect.flatMap(asLensImpl(l).access, frame => frame.commit(Effect.succeed(a))),
|
l => Effect.flatMap(asLensImpl(l).access, frame => frame.commit(Effect.succeed(a))),
|
||||||
),
|
),
|
||||||
@@ -338,11 +345,7 @@ export const mapEffect: {
|
|||||||
self: Lens<A, ER, EW, RR, RW>,
|
self: Lens<A, ER, EW, RR, RW>,
|
||||||
get: (a: NoInfer<A>) => Effect.Effect<B, EGet, RGet>,
|
get: (a: NoInfer<A>) => Effect.Effect<B, EGet, RGet>,
|
||||||
set: (a: NoInfer<A>, b: B) => Effect.Effect<NoInfer<A>, ESet, RSet>,
|
set: (a: NoInfer<A>, b: B) => Effect.Effect<NoInfer<A>, ESet, RSet>,
|
||||||
): Lens<B, ER | EGet, EW | ESet, RR | RGet, RW | RSet> => {
|
): Lens<B, ER | EGet, EW | ESet, RR | RGet, RW | RSet> => derive(self, {
|
||||||
return derive(
|
|
||||||
asLensImpl(self),
|
|
||||||
{
|
|
||||||
[LensStepTypeId]: LensStepTypeId,
|
|
||||||
access: parent => Effect.flatMap(
|
access: parent => Effect.flatMap(
|
||||||
parent,
|
parent,
|
||||||
frame => Effect.map(
|
frame => Effect.map(
|
||||||
@@ -353,10 +356,8 @@ export const mapEffect: {
|
|||||||
}),
|
}),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
transformStream: stream => Stream.mapEffect(stream, get),
|
transformStream: Stream.mapEffect(get),
|
||||||
},
|
}))
|
||||||
)
|
|
||||||
})
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Derives a new `Lens` by applying synchronous getters and setters over the value inside an `Option`.
|
* 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>(
|
} = Function.dual(2, <A, ER, EW, RR, RW>(
|
||||||
self: Lens<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>>,
|
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), {
|
): Lens<A, ER, EW, RR, RW> => derive(self, {
|
||||||
[LensStepTypeId]: LensStepTypeId,
|
access: identity,
|
||||||
access: parent => parent,
|
|
||||||
transformStream: f,
|
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>(
|
} = Function.dual(2, <A, ER, EW, RR, RW, E2>(
|
||||||
self: Lens<A, ER, EW, RR, RW>,
|
self: Lens<A, ER, EW, RR, RW>,
|
||||||
f: (error: NoInfer<ER>) => E2,
|
f: (error: NoInfer<ER>) => E2,
|
||||||
): Lens<A, E2, EW, RR, RW> => derive(asLensImpl(self), {
|
): Lens<A, E2, EW, RR, RW> => derive(self, {
|
||||||
[LensStepTypeId]: LensStepTypeId,
|
access: Effect.mapError(f),
|
||||||
access: parent => Effect.mapError(parent, f),
|
transformStream: Stream.mapError(f),
|
||||||
transformStream: stream => Stream.mapError(stream, f),
|
}))
|
||||||
} as LensStep<A, A, E2, ER, EW, EW, RR, RR, RW, RW>))
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Transforms modify errors of a `Lens`.
|
* Transforms modify errors of a `Lens`.
|
||||||
@@ -484,14 +483,13 @@ export const mapErrorWrite: {
|
|||||||
} = Function.dual(2, <A, ER, EW, RR, RW, E2>(
|
} = Function.dual(2, <A, ER, EW, RR, RW, E2>(
|
||||||
self: Lens<A, ER, EW, RR, RW>,
|
self: Lens<A, ER, EW, RR, RW>,
|
||||||
f: (error: unknown) => E2,
|
f: (error: unknown) => E2,
|
||||||
): Lens<A, ER, E2, RR, RW> => derive(asLensImpl(self), {
|
): Lens<A, ER, E2, RR, RW> => derive(self, {
|
||||||
[LensStepTypeId]: LensStepTypeId,
|
|
||||||
access: parent => Effect.map(parent, frame => ({
|
access: parent => Effect.map(parent, frame => ({
|
||||||
value: frame.value,
|
value: frame.value,
|
||||||
commit: next => Effect.mapError(frame.commit(next), f),
|
commit: next => Effect.mapError(frame.commit(next), f),
|
||||||
})),
|
})),
|
||||||
transformStream: stream => stream,
|
transformStream: identity,
|
||||||
} as LensStep<A, A, ER, ER, E2, EW, RR, RR, RW, RW>))
|
}))
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Transforms all errors of a `Lens`.
|
* Transforms all errors of a `Lens`.
|
||||||
@@ -510,8 +508,7 @@ export const mapError: {
|
|||||||
} = Function.dual(2, <A, ER, EW, RR, RW, E2>(
|
} = Function.dual(2, <A, ER, EW, RR, RW, E2>(
|
||||||
self: Lens<A, ER, EW, RR, RW>,
|
self: Lens<A, ER, EW, RR, RW>,
|
||||||
f: (error: unknown) => E2,
|
f: (error: unknown) => E2,
|
||||||
): Lens<A, E2, E2, RR, RW> => derive(asLensImpl(self), {
|
): Lens<A, E2, E2, RR, RW> => derive(self, {
|
||||||
[LensStepTypeId]: LensStepTypeId,
|
|
||||||
access: parent => Effect.map(
|
access: parent => Effect.map(
|
||||||
Effect.mapError(parent, f),
|
Effect.mapError(parent, f),
|
||||||
frame => ({
|
frame => ({
|
||||||
@@ -519,8 +516,8 @@ export const mapError: {
|
|||||||
commit: next => Effect.mapError(frame.commit(next), f),
|
commit: next => Effect.mapError(frame.commit(next), f),
|
||||||
}),
|
}),
|
||||||
),
|
),
|
||||||
transformStream: stream => Stream.mapError(stream, f),
|
transformStream: Stream.mapError(f),
|
||||||
} as LensStep<A, A, E2, ER, E2, EW, RR, RR, RW, RW>))
|
}))
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Recovers from read failures of a `Lens`.
|
* Recovers from read failures of a `Lens`.
|
||||||
@@ -538,11 +535,10 @@ export const catchAllRead: {
|
|||||||
} = Function.dual(2, <A, ER, EW, RR, RW, E2, R2>(
|
} = Function.dual(2, <A, ER, EW, RR, RW, E2, R2>(
|
||||||
self: Lens<A, ER, EW, RR, RW>,
|
self: Lens<A, ER, EW, RR, RW>,
|
||||||
f: (error: NoInfer<ER>) => Subscribable.Subscribable<A, E2, R2>,
|
f: (error: NoInfer<ER>) => Subscribable.Subscribable<A, E2, R2>,
|
||||||
): Lens<A, E2, EW, RR | R2, RW> => derive(asLensImpl(self), {
|
): Lens<A, E2, EW, RR | R2, RW> => derive(self, {
|
||||||
[LensStepTypeId]: LensStepTypeId,
|
access: Effect.catchAll(error => asLensImpl(f(error) as Lens<A, E2, EW, R2, RW>).access),
|
||||||
access: parent => Effect.catchAll(parent, error => asLensImpl(f(error) as Lens<A, E2, EW, R2, RW>).access),
|
transformStream: Stream.catchAll(error => f(error).changes),
|
||||||
transformStream: stream => Stream.catchAll(stream, error => f(error).changes),
|
}))
|
||||||
} as LensStep<A, A, E2, ER, EW, EW, RR | R2, RR, RW, RW>))
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Runs an effect when read failures occur.
|
* Runs an effect when read failures occur.
|
||||||
@@ -560,11 +556,10 @@ export const tapErrorRead: {
|
|||||||
} = Function.dual(2, <A, ER, EW, RR, RW, E2, R2>(
|
} = Function.dual(2, <A, ER, EW, RR, RW, E2, R2>(
|
||||||
self: Lens<A, ER, EW, RR, RW>,
|
self: Lens<A, ER, EW, RR, RW>,
|
||||||
f: (error: NoInfer<ER>) => Effect.Effect<any, E2, R2>,
|
f: (error: NoInfer<ER>) => Effect.Effect<any, E2, R2>,
|
||||||
): Lens<A, ER | E2, EW, RR | R2, RW> => derive(asLensImpl(self), {
|
): Lens<A, ER | E2, EW, RR | R2, RW> => derive(self, {
|
||||||
[LensStepTypeId]: LensStepTypeId,
|
access: Effect.tapError(f),
|
||||||
access: parent => Effect.tapError(parent, f),
|
transformStream: Stream.tapError(f),
|
||||||
transformStream: stream => Stream.tapError(stream, f),
|
}))
|
||||||
} as LensStep<A, A, ER | E2, ER, EW, EW, RR | R2, RR, RW, RW>))
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Runs an effect when modify failures occur.
|
* Runs an effect when modify failures occur.
|
||||||
@@ -583,14 +578,13 @@ export const tapErrorWrite: {
|
|||||||
} = Function.dual(2, <A, ER, EW, RR, RW, E2, R2>(
|
} = Function.dual(2, <A, ER, EW, RR, RW, E2, R2>(
|
||||||
self: Lens<A, ER, EW, RR, RW>,
|
self: Lens<A, ER, EW, RR, RW>,
|
||||||
f: (error: unknown) => Effect.Effect<any, E2, R2>,
|
f: (error: unknown) => Effect.Effect<any, E2, R2>,
|
||||||
): Lens<A, ER, EW | E2, RR, RW | R2> => derive(asLensImpl(self), {
|
): Lens<A, ER, EW | E2, RR, RW | R2> => derive(self, {
|
||||||
[LensStepTypeId]: LensStepTypeId,
|
|
||||||
access: parent => Effect.map(parent, frame => ({
|
access: parent => Effect.map(parent, frame => ({
|
||||||
value: frame.value,
|
value: frame.value,
|
||||||
commit: next => Effect.tapError(frame.commit(next), f),
|
commit: next => Effect.tapError(frame.commit(next), f),
|
||||||
})),
|
})),
|
||||||
transformStream: stream => stream,
|
transformStream: identity,
|
||||||
} as LensStep<A, A, ER, ER, EW | E2, EW, RR, RR, RW | R2, RW>))
|
}))
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Runs an effect when any `Lens` failure occurs.
|
* 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>(
|
} = Function.dual(2, <A, ER, EW, RR, RW, E2, R2>(
|
||||||
self: Lens<A, ER, EW, RR, RW>,
|
self: Lens<A, ER, EW, RR, RW>,
|
||||||
f: (error: unknown) => Effect.Effect<any, E2, R2>,
|
f: (error: unknown) => Effect.Effect<any, E2, R2>,
|
||||||
): Lens<A, ER | E2, EW | E2, RR | R2, RW | R2> => derive(asLensImpl(self), {
|
): Lens<A, ER | E2, EW | E2, RR | R2, RW | R2> => derive(self, {
|
||||||
[LensStepTypeId]: LensStepTypeId,
|
|
||||||
access: parent => Effect.map(
|
access: parent => Effect.map(
|
||||||
Effect.tapError(parent, f),
|
Effect.tapError(parent, f),
|
||||||
frame => ({
|
frame => ({
|
||||||
@@ -618,8 +611,8 @@ export const tapError: {
|
|||||||
commit: next => Effect.tapError(frame.commit(next), f),
|
commit: next => Effect.tapError(frame.commit(next), f),
|
||||||
}),
|
}),
|
||||||
),
|
),
|
||||||
transformStream: stream => Stream.tapError(stream, f),
|
transformStream: Stream.tapError(f),
|
||||||
} as LensStep<A, A, ER | E2, ER, EW | E2, EW, RR | R2, RR, RW | R2, RW>))
|
}))
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -636,8 +629,7 @@ export const provideContext: {
|
|||||||
} = Function.dual(2, <A, ER, EW, RR, RW, R2>(
|
} = Function.dual(2, <A, ER, EW, RR, RW, R2>(
|
||||||
self: Lens<A, ER, EW, RR, RW>,
|
self: Lens<A, ER, EW, RR, RW>,
|
||||||
context: Context.Context<R2>,
|
context: Context.Context<R2>,
|
||||||
): Lens<A, ER, EW, Exclude<RR, R2>, Exclude<RW, R2>> => derive(asLensImpl(self), {
|
): Lens<A, ER, EW, Exclude<RR, R2>, Exclude<RW, R2>> => derive(self, {
|
||||||
[LensStepTypeId]: LensStepTypeId,
|
|
||||||
access: parent => Effect.map(
|
access: parent => Effect.map(
|
||||||
Effect.provide(parent, context),
|
Effect.provide(parent, context),
|
||||||
frame => ({
|
frame => ({
|
||||||
@@ -645,46 +637,8 @@ export const provideContext: {
|
|||||||
commit: next => Effect.provide(frame.commit(Effect.provide(next, context)), context),
|
commit: next => Effect.provide(frame.commit(Effect.provide(next, context)), context),
|
||||||
}),
|
}),
|
||||||
),
|
),
|
||||||
transformStream: stream => Stream.provideSomeContext(stream, context),
|
transformStream: Stream.provideSomeContext(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>))
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provides a single service to a `Lens`, removing it from both the read and write environments.
|
* 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>,
|
self: Lens<A, ER, EW, RR, RW>,
|
||||||
tag: Context.Tag<I, S>,
|
tag: Context.Tag<I, S>,
|
||||||
service: NoInfer<S>,
|
service: NoInfer<S>,
|
||||||
): Lens<A, ER, EW, Exclude<RR, I>, Exclude<RW, I>> => derive(asLensImpl(self), {
|
): Lens<A, ER, EW, Exclude<RR, I>, Exclude<RW, I>> => derive(self, {
|
||||||
[LensStepTypeId]: LensStepTypeId,
|
|
||||||
access: parent => Effect.map(
|
access: parent => Effect.map(
|
||||||
Effect.provideService(parent, tag, service),
|
Effect.provideService(parent, tag, service),
|
||||||
frame => ({
|
frame => ({
|
||||||
@@ -715,8 +668,8 @@ export const provideService: {
|
|||||||
commit: next => Effect.provideService(frame.commit(Effect.provideService(next, tag, service)), tag, service),
|
commit: next => Effect.provideService(frame.commit(Effect.provideService(next, tag, service)), tag, service),
|
||||||
}),
|
}),
|
||||||
),
|
),
|
||||||
transformStream: stream => Stream.provideService(stream, tag, service),
|
transformStream: Stream.provideService(tag, service),
|
||||||
} as LensStep<A, A, ER, ER, EW, EW, Exclude<RR, I>, RR, Exclude<RW, I>, RW>))
|
}))
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user