@@ -28,44 +28,63 @@ 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 type LensStepTypeId = typeof LensTypeId
|
||||||
|
|
||||||
|
export interface LensStep<in out A, in out B, in out ER = never, in out EW = never, in out RR = never, in out RW = never> {
|
||||||
|
readonly [LensStepTypeId]: LensStepTypeId
|
||||||
export const LensWithInternalsTypeId: unique symbol = Symbol.for("@effect-fc/Lens/LensWithInternals")
|
readonly transform: (parent: B) => Effect.Effect<A, ER, RR>
|
||||||
export type LensWithInternalsTypeId = typeof LensWithInternalsTypeId
|
readonly update: (next: A, parent: B) => Effect.Effect<B, EW, RW>
|
||||||
|
|
||||||
export interface LensWithInternals<in out A, in out ER = never, in out EW = never, in out RR = never, in out RW = never>
|
|
||||||
extends Lens<A, ER, EW, RR, RW> {
|
|
||||||
readonly [LensWithInternalsTypeId]: LensWithInternalsTypeId
|
|
||||||
|
|
||||||
readonly update: (a: A) => Effect.Effect<void, EW, RW>
|
|
||||||
readonly withLock: <A1, E1, R1>(self: Effect.Effect<A1, E1, R1>) => Effect.Effect<A1, EW | E1, RW | R1>
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const isLensWithInternals = (u: unknown): u is LensWithInternals<unknown, unknown, unknown, unknown, unknown> => Predicate.hasProperty(u, LensWithInternalsTypeId)
|
export const isLensStep = (u: unknown): u is LensStep<unknown, unknown, unknown, unknown, unknown, unknown> => Predicate.hasProperty(u, LensStepTypeId)
|
||||||
|
|
||||||
export const asLensWithInternals = <A, ER, EW, RR, RW>(
|
|
||||||
lens: Lens<A, ER, EW, RR, RW>
|
|
||||||
): LensWithInternals<A, ER, EW, RR, RW> => {
|
|
||||||
if (!isLensWithInternals(lens))
|
|
||||||
throw new Error("Not a 'LensWithInternals'.")
|
|
||||||
return lens as LensWithInternals<A, ER, EW, RR, RW>
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// export const LensWithInternalsTypeId: unique symbol = Symbol.for("@effect-fc/Lens/LensWithInternals")
|
||||||
|
// export type LensWithInternalsTypeId = typeof LensWithInternalsTypeId
|
||||||
|
|
||||||
|
// export interface LensWithInternals<in out A, in out ER = never, in out EW = never, in out RR = never, in out RW = never>
|
||||||
|
// extends Lens<A, ER, EW, RR, RW> {
|
||||||
|
// readonly [LensWithInternalsTypeId]: LensWithInternalsTypeId
|
||||||
|
|
||||||
|
// readonly update: (a: A) => Effect.Effect<void, EW, RW>
|
||||||
|
// readonly withLock: <A1, E1, R1>(self: Effect.Effect<A1, E1, R1>) => Effect.Effect<A1, EW | E1, RW | R1>
|
||||||
|
// }
|
||||||
|
|
||||||
|
// export const isLensWithInternals = (u: unknown): u is LensWithInternals<unknown, unknown, unknown, unknown, unknown> => Predicate.hasProperty(u, LensWithInternalsTypeId)
|
||||||
|
|
||||||
|
// export const asLensWithInternals = <A, ER, EW, RR, RW>(
|
||||||
|
// lens: Lens<A, ER, EW, RR, RW>
|
||||||
|
// ): LensWithInternals<A, ER, EW, RR, RW> => {
|
||||||
|
// if (!isLensWithInternals(lens))
|
||||||
|
// throw new Error("Not a 'LensWithInternals'.")
|
||||||
|
// return lens as LensWithInternals<A, ER, EW, RR, RW>
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
|
export const LensImplTypeId: unique symbol = Symbol.for("@effect-fc/Lens/LensImpl")
|
||||||
|
export type LensImplTypeId = typeof LensImplTypeId
|
||||||
|
|
||||||
export abstract class LensImpl<in out A, in out ER = never, in out EW = never, in out RR = never, in out RW = never>
|
export abstract class LensImpl<in out A, in out ER = never, in out EW = never, in out RR = never, in out RW = never>
|
||||||
extends Pipeable.Class() {
|
extends Pipeable.Class() implements Lens<A, ER, EW, RR, RW> {
|
||||||
readonly [Readable.TypeId]: Readable.TypeId = Readable.TypeId
|
readonly [Readable.TypeId]: Readable.TypeId = Readable.TypeId
|
||||||
readonly [Subscribable.TypeId]: Subscribable.TypeId = Subscribable.TypeId
|
readonly [Subscribable.TypeId]: Subscribable.TypeId = Subscribable.TypeId
|
||||||
readonly [LensTypeId]: LensTypeId = LensTypeId
|
readonly [LensTypeId]: LensTypeId = LensTypeId
|
||||||
readonly [LensWithInternalsTypeId]: LensWithInternalsTypeId = LensWithInternalsTypeId
|
readonly [LensImplTypeId]: LensImplTypeId = LensImplTypeId
|
||||||
|
|
||||||
abstract readonly get: Effect.Effect<A, ER, RR>
|
abstract readonly get: Effect.Effect<A, ER, RR>
|
||||||
abstract readonly changes: Stream.Stream<A, ER, RR>
|
abstract readonly changes: Stream.Stream<A, ER, RR>
|
||||||
abstract readonly update: (a: A) => Effect.Effect<void, EW, RW>
|
abstract readonly commit: (a: A) => Effect.Effect<void, EW, RW>
|
||||||
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>
|
||||||
|
|
||||||
modifyEffect = modifyEffect
|
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(
|
||||||
|
this.get,
|
||||||
|
a => Effect.flatMap(f(a), ([b, next]) => Effect.as(this.commit(next), b),
|
||||||
|
)))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// export declare namespace LensImpl {
|
// export declare namespace LensImpl {
|
||||||
@@ -103,16 +122,6 @@ extends Pipeable.Class() {
|
|||||||
// modifyEffect = modifyEffect
|
// modifyEffect = modifyEffect
|
||||||
// }
|
// }
|
||||||
|
|
||||||
function modifyEffect<A, B, ER = never, EW = never, E1 = never, RR = never, RW = never, R1 = never>(
|
|
||||||
this: LensWithInternals<A, ER, EW, RR, RW>,
|
|
||||||
f: (a: A) => Effect.Effect<readonly [B, A], E1, R1>,
|
|
||||||
): Effect.Effect<B, ER | EW | E1, RR | RW | R1> {
|
|
||||||
return this.withLock(Effect.flatMap(
|
|
||||||
this.get,
|
|
||||||
a => Effect.flatMap(f(a), ([b, next]) => Effect.as(this.update(next), b),
|
|
||||||
)))
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a `Lens` by supplying how to read the current value, observe changes, and apply transformations.
|
* Creates a `Lens` by supplying how to read the current value, observe changes, and apply transformations.
|
||||||
*/
|
*/
|
||||||
@@ -121,26 +130,26 @@ export const make = <A, ER, EW, RR, RW>(
|
|||||||
): Lens<A, ER, EW, RR, RW> => new LensImpl(source)
|
): Lens<A, ER, EW, RR, RW> => new LensImpl(source)
|
||||||
|
|
||||||
|
|
||||||
export class LensLazyImpl<in out A, in out ER = never, in out EW = never, in out RR = never, in out RW = never>
|
// export class LensLazyImpl<in out A, in out ER = never, in out EW = never, in out RR = never, in out RW = never>
|
||||||
extends Pipeable.Class() implements LensWithInternals<A, ER, EW, RR, RW> {
|
// extends Pipeable.Class() implements LensWithInternals<A, ER, EW, RR, RW> {
|
||||||
readonly [Readable.TypeId]: Readable.TypeId = Readable.TypeId
|
// readonly [Readable.TypeId]: Readable.TypeId = Readable.TypeId
|
||||||
readonly [Subscribable.TypeId]: Subscribable.TypeId = Subscribable.TypeId
|
// readonly [Subscribable.TypeId]: Subscribable.TypeId = Subscribable.TypeId
|
||||||
readonly [LensTypeId]: LensTypeId = LensTypeId
|
// readonly [LensTypeId]: LensTypeId = LensTypeId
|
||||||
readonly [LensWithInternalsTypeId]: LensWithInternalsTypeId = LensWithInternalsTypeId
|
// readonly [LensWithInternalsTypeId]: LensWithInternalsTypeId = LensWithInternalsTypeId
|
||||||
|
|
||||||
constructor(
|
// constructor(
|
||||||
readonly source: LensImpl.Source<A, ER, EW, RR, RW>
|
// readonly source: LensImpl.Source<A, ER, EW, RR, RW>
|
||||||
) {
|
// ) {
|
||||||
super()
|
// super()
|
||||||
}
|
// }
|
||||||
|
|
||||||
get get() { return this.source.get }
|
// get get() { return this.source.get }
|
||||||
get changes() { return this.source.changes }
|
// get changes() { return this.source.changes }
|
||||||
get update() { return this.source.update }
|
// get update() { return this.source.update }
|
||||||
get withLock() { return this.source.withLock }
|
// get withLock() { return this.source.withLock }
|
||||||
|
|
||||||
modifyEffect = modifyEffect
|
// modifyEffect = modifyEffect
|
||||||
}
|
// }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a `Lens` by supplying how to read the current value, observe changes, and apply transformations.
|
* Creates a `Lens` by supplying how to read the current value, observe changes, and apply transformations.
|
||||||
|
|||||||
Reference in New Issue
Block a user