@@ -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 * as React from "react"
|
||||||
import { useFork } from "./useFork.js"
|
import { useFork } from "./useFork.js"
|
||||||
import { useOnce } from "./useOnce.js"
|
import { useOnce } from "./useOnce.js"
|
||||||
@@ -34,11 +34,14 @@ export const useInput: {
|
|||||||
// Sync the upstream state with the internal state
|
// 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.
|
// 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.whenEffect(
|
||||||
Effect.andThen(
|
Effect.andThen(
|
||||||
Effect.all([Schema.encode(options.schema)(upstreamValue), internalRef]),
|
Schema.encode(options.schema)(upstreamValue),
|
||||||
([encodedUpstreamValue, internalValue]) => Effect.when(
|
encodedUpstreamValue => Ref.set(internalRef, encodedUpstreamValue),
|
||||||
Ref.set(internalRef, encodedUpstreamValue),
|
),
|
||||||
() => encodedUpstreamValue !== internalValue,
|
internalRef.pipe(
|
||||||
|
Effect.andThen(Schema.decode(options.schema)),
|
||||||
|
Effect.andThen(decodedInternalValue => !Equal.equals(upstreamValue, decodedInternalValue)),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
),
|
),
|
||||||
|
|||||||
@@ -2,9 +2,9 @@ import { TextFieldInput } from "@/lib/TextFieldInput"
|
|||||||
import { runtime } from "@/runtime"
|
import { runtime } from "@/runtime"
|
||||||
import { Container, TextField } from "@radix-ui/themes"
|
import { Container, TextField } from "@radix-ui/themes"
|
||||||
import { createFileRoute } from "@tanstack/react-router"
|
import { createFileRoute } from "@tanstack/react-router"
|
||||||
import { Console, Schema, Stream, SubscriptionRef } from "effect"
|
import { Schema, SubscriptionRef } from "effect"
|
||||||
import { Component, Memo } from "effect-fc"
|
import { Component, Memo } from "effect-fc"
|
||||||
import { useFork, useInput, useOnce, useRefState } from "effect-fc/hooks"
|
import { useInput, useOnce } from "effect-fc/hooks"
|
||||||
|
|
||||||
|
|
||||||
const IntFromString = Schema.NumberFromString.pipe(Schema.int())
|
const IntFromString = Schema.NumberFromString.pipe(Schema.int())
|
||||||
|
|||||||
Reference in New Issue
Block a user