@@ -1,4 +1,4 @@
|
||||
import { type Duration, Effect, flow, identity, Option, type ParseResult, Ref, Schema, Stream, SubscriptionRef } from "effect"
|
||||
import { type Duration, Effect, Equal, flow, identity, Option, type ParseResult, Ref, Schema, Stream, SubscriptionRef } from "effect"
|
||||
import * as React from "react"
|
||||
import { useFork } from "./useFork.js"
|
||||
import { useOnce } from "./useOnce.js"
|
||||
@@ -34,11 +34,14 @@ export const useInput: {
|
||||
// Sync the upstream state with the internal state
|
||||
// Only mutate the internal state if the upstream encoded value is actually different. This avoids infinite re-render loops.
|
||||
Stream.runForEach(options.ref.changes, upstreamValue =>
|
||||
Effect.andThen(
|
||||
Effect.all([Schema.encode(options.schema)(upstreamValue), internalRef]),
|
||||
([encodedUpstreamValue, internalValue]) => Effect.when(
|
||||
Ref.set(internalRef, encodedUpstreamValue),
|
||||
() => encodedUpstreamValue !== internalValue,
|
||||
Effect.whenEffect(
|
||||
Effect.andThen(
|
||||
Schema.encode(options.schema)(upstreamValue),
|
||||
encodedUpstreamValue => Ref.set(internalRef, encodedUpstreamValue),
|
||||
),
|
||||
internalRef.pipe(
|
||||
Effect.andThen(Schema.decode(options.schema)),
|
||||
Effect.andThen(decodedInternalValue => !Equal.equals(upstreamValue, decodedInternalValue)),
|
||||
),
|
||||
)
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user