0.2.0 #5
@@ -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 LensStepTypeId: unique symbol = Symbol.for("@effect-fc/Lens/LensStep")
|
||||
export type LensStepTypeId = typeof LensTypeId
|
||||
|
||||
|
||||
|
||||
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 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
|
||||
readonly transform: (parent: B) => Effect.Effect<A, ER, RR>
|
||||
readonly update: (next: A, parent: B) => Effect.Effect<B, EW, RW>
|
||||
}
|
||||
|
||||
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>
|
||||
extends Pipeable.Class() {
|
||||
extends Pipeable.Class() implements Lens<A, ER, EW, RR, RW> {
|
||||
readonly [Readable.TypeId]: Readable.TypeId = Readable.TypeId
|
||||
readonly [Subscribable.TypeId]: Subscribable.TypeId = Subscribable.TypeId
|
||||
readonly [LensTypeId]: LensTypeId = LensTypeId
|
||||
readonly [LensWithInternalsTypeId]: LensWithInternalsTypeId = LensWithInternalsTypeId
|
||||
readonly [LensImplTypeId]: LensImplTypeId = LensImplTypeId
|
||||
|
||||
abstract readonly get: Effect.Effect<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>
|
||||
|
||||
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 {
|
||||
@@ -103,16 +122,6 @@ extends Pipeable.Class() {
|
||||
// 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.
|
||||
*/
|
||||
@@ -121,26 +130,26 @@ export const make = <A, ER, EW, RR, RW>(
|
||||
): 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>
|
||||
extends Pipeable.Class() implements LensWithInternals<A, ER, EW, RR, RW> {
|
||||
readonly [Readable.TypeId]: Readable.TypeId = Readable.TypeId
|
||||
readonly [Subscribable.TypeId]: Subscribable.TypeId = Subscribable.TypeId
|
||||
readonly [LensTypeId]: LensTypeId = LensTypeId
|
||||
readonly [LensWithInternalsTypeId]: LensWithInternalsTypeId = LensWithInternalsTypeId
|
||||
// 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> {
|
||||
// readonly [Readable.TypeId]: Readable.TypeId = Readable.TypeId
|
||||
// readonly [Subscribable.TypeId]: Subscribable.TypeId = Subscribable.TypeId
|
||||
// readonly [LensTypeId]: LensTypeId = LensTypeId
|
||||
// readonly [LensWithInternalsTypeId]: LensWithInternalsTypeId = LensWithInternalsTypeId
|
||||
|
||||
constructor(
|
||||
readonly source: LensImpl.Source<A, ER, EW, RR, RW>
|
||||
) {
|
||||
super()
|
||||
}
|
||||
// constructor(
|
||||
// readonly source: LensImpl.Source<A, ER, EW, RR, RW>
|
||||
// ) {
|
||||
// super()
|
||||
// }
|
||||
|
||||
get get() { return this.source.get }
|
||||
get changes() { return this.source.changes }
|
||||
get update() { return this.source.update }
|
||||
get withLock() { return this.source.withLock }
|
||||
// get get() { return this.source.get }
|
||||
// get changes() { return this.source.changes }
|
||||
// get update() { return this.source.update }
|
||||
// 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.
|
||||
|
||||
Reference in New Issue
Block a user