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

This commit is contained in:
Julien Valverdé
2025-08-18 21:24:28 +02:00
parent ea5b5f9ac2
commit e665a9d691
2 changed files with 11 additions and 8 deletions

View File

@@ -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)),
),
)
),