This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import { Array, Chunk, Effect, Function, Pipeable, Predicate, Readable, type Ref, Stream, Subscribable, type SubscriptionRef } from "effect"
|
import { Array, Chunk, Effect, Function, Pipeable, Predicate, Readable, Stream, Subscribable, type SubscriptionRef, type SynchronizedRef } from "effect"
|
||||||
import type { NoSuchElementException } from "effect/Cause"
|
import type { NoSuchElementException } from "effect/Cause"
|
||||||
|
|
||||||
|
|
||||||
@@ -92,25 +92,19 @@ export const fromSubscriptionRef = <A>(
|
|||||||
})
|
})
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a `Lens` that proxies a `Ref`.
|
* Creates a `Lens` that proxies a `SynchronizedRef`.
|
||||||
*
|
*
|
||||||
* Note: since `Ref` does not provide any kind of reactivity mechanism, the produced `Lens` will be non-reactive.
|
* Note: since `SynchronizedRef` does not provide any kind of reactivity mechanism, the produced `Lens` will be non-reactive.
|
||||||
* This means its `changes` stream will only emit the current value once when evaluated and nothing else.
|
* This means its `changes` stream will only emit the current value once when evaluated and nothing else.
|
||||||
*/
|
*/
|
||||||
export const fromRef = <A>(
|
export const fromSynchronizedRef = <A>(
|
||||||
ref: Ref.Ref<A>
|
ref: SynchronizedRef.SynchronizedRef<A>
|
||||||
): Lens<A, never, never, never, never> => make({
|
): Lens<A, never, never, never, never> => make({
|
||||||
get get() { return ref.get },
|
get get() { return ref.get },
|
||||||
get changes() { return Stream.unwrap(Effect.map(ref.get, Stream.make)) },
|
get changes() { return Stream.unwrap(Effect.map(ref.get, Stream.make)) },
|
||||||
modify: <B, E1 = never, R1 = never>(
|
modify: <B, E1 = never, R1 = never>(
|
||||||
f: (a: A) => Effect.Effect<readonly [B, A], E1, R1>
|
f: (a: A) => Effect.Effect<readonly [B, A], E1, R1>
|
||||||
) => Effect.flatMap(ref.get, a => Effect.flatMap(
|
) => ref.modifyEffect(f),
|
||||||
f(a),
|
|
||||||
([b, next]) => Effect.flatMap(
|
|
||||||
ref.set(next),
|
|
||||||
() => Effect.succeed(b),
|
|
||||||
),
|
|
||||||
)),
|
|
||||||
})
|
})
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user