Fix
All checks were successful
Lint / lint (push) Successful in 11s

This commit is contained in:
Julien Valverdé
2025-08-09 11:49:40 +02:00
parent 2674c7e8c9
commit 6f9e0729fc

View File

@@ -30,14 +30,19 @@ export const useInput: {
const [error, setError] = React.useState(Option.none<ParseResult.ParseError>()) const [error, setError] = React.useState(Option.none<ParseResult.ParseError>())
yield* useFork(() => Effect.all([ yield* useFork(() => Effect.all([
// 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 => Stream.runForEach(options.ref.changes, upstreamValue =>
Effect.andThen(internalRef, internalValue => Effect.andThen(
upstreamValue !== internalValue Effect.all([Schema.encode(options.schema)(upstreamValue), internalRef]),
? Effect.andThen(Schema.encode(options.schema)(upstreamValue), v => Ref.set(internalRef, v)) ([encodedUpstreamValue, internalValue]) => Effect.when(
: Effect.void Ref.set(internalRef, encodedUpstreamValue),
() => encodedUpstreamValue !== internalValue,
),
) )
), ),
// Sync all changes to the internal state with upstream
Stream.runForEach( Stream.runForEach(
internalRef.changes.pipe(options.debounce ? Stream.debounce(options.debounce) : identity), internalRef.changes.pipe(options.debounce ? Stream.debounce(options.debounce) : identity),
flow( flow(