From 2df12d7f404b0d1e5c9169917d9d2b88e4169225 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Valverd=C3=A9?= Date: Wed, 24 Sep 2025 06:08:02 +0200 Subject: [PATCH] Form work --- packages/effect-fc/src/Form.ts | 51 +++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 19 deletions(-) diff --git a/packages/effect-fc/src/Form.ts b/packages/effect-fc/src/Form.ts index eea0c07..5ccf511 100644 --- a/packages/effect-fc/src/Form.ts +++ b/packages/effect-fc/src/Form.ts @@ -71,16 +71,6 @@ export const make: { ) }) -const run = (self: Form) => Stream.runForEach( - self.encodedValueRef.changes, - flow( - Schema.decode(self.schema, { errors: "all" }), - Effect.andThen(v => SubscriptionRef.set(self.valueRef, Option.some(v))), - Effect.andThen(SubscriptionRef.set(self.errorRef, Option.none())), - Effect.catchTag("ParseError", e => SubscriptionRef.set(self.errorRef, Option.some(e))) - ), -) - export namespace service { export interface Options extends make.Options {} } @@ -104,6 +94,16 @@ export const useForm: { return form }) +const run = (self: Form) => Stream.runForEach( + self.encodedValueRef.changes, + flow( + Schema.decode(self.schema, { errors: "all" }), + Effect.andThen(v => SubscriptionRef.set(self.valueRef, Option.some(v))), + Effect.andThen(SubscriptionRef.set(self.errorRef, Option.none())), + Effect.catchTag("ParseError", e => SubscriptionRef.set(self.errorRef, Option.some(e))) + ), +) + export namespace useInput { export interface Options { @@ -139,18 +139,31 @@ export const useInput: { const [value, setValue] = yield* Hooks.useRefState(internalValueRef) const [issues] = yield* Hooks.useSubscribables(issuesSubscribable) - yield* Hooks.useFork(() => Stream.runForEach( - internalValueRef.changes.pipe( - Stream.changesWith(Equivalence.strict()), - options?.debounce ? Stream.debounce(options.debounce) : identity, - Stream.drop(1), + yield* Hooks.useFork(() => Effect.all([ + Stream.runForEach( + self.encodedValueRef.changes.pipe( + Stream.flatMap(PropertyPath.get(path)), + Stream.drop(1), + ), + + upstreamEncodedValue => internalValueRef.pipe( + + ), ), - internalValue => self.encodedValueRef.pipe( - Effect.andThen(encodedValue => PropertyPath.immutableSet(encodedValue, path, internalValue)), - Effect.andThen(encodedValue => SubscriptionRef.set(self.encodedValueRef, encodedValue)), + Stream.runForEach( + internalValueRef.changes.pipe( + Stream.changesWith(Equivalence.strict()), + options?.debounce ? Stream.debounce(options.debounce) : identity, + Stream.drop(1), + ), + + internalValue => self.encodedValueRef.pipe( + Effect.andThen(encodedValue => PropertyPath.immutableSet(encodedValue, path, internalValue)), + Effect.andThen(encodedValue => SubscriptionRef.set(self.encodedValueRef, encodedValue)), + ), ), - ), [internalValueRef, self, ...path]) + ], { concurrency: "unbounded" }), [internalValueRef, self, ...path]) return { value, setValue, issues } })