From 52c61891795e6e2d3bfa1c4ddd87de90405cb131 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Valverd=C3=A9?= Date: Sat, 2 May 2026 00:07:24 +0200 Subject: [PATCH] Refactor --- packages/effect-fc/src/SynchronizedForm.ts | 36 ++++++++-------------- 1 file changed, 13 insertions(+), 23 deletions(-) diff --git a/packages/effect-fc/src/SynchronizedForm.ts b/packages/effect-fc/src/SynchronizedForm.ts index 93cc40d..58a1137 100644 --- a/packages/effect-fc/src/SynchronizedForm.ts +++ b/packages/effect-fc/src/SynchronizedForm.ts @@ -1,4 +1,4 @@ -import { Array, Cause, Chunk, type Context, Effect, Exit, Fiber, Option, ParseResult, Pipeable, Predicate, Schema, type Scope, Stream, SubscriptionRef } from "effect" +import { Array, type Context, Effect, Fiber, Option, ParseResult, Pipeable, Predicate, Schema, type Scope, Stream, SubscriptionRef } from "effect" import * as Form from "./Form.js" import * as Lens from "./Lens.js" import * as Subscribable from "./Subscribable.js" @@ -121,35 +121,25 @@ export class SynchronizedFormImpl< onSome: Fiber.interrupt, onNone: () => Effect.void, })), - Effect.andThen( - Effect.forkScoped(Effect.onExit( + Effect.andThen(Effect.forkScoped( + Effect.ensuring( Schema.decode(this.schema, { errors: "all" })(encodedValue), - exit => Effect.andThen( - Exit.matchEffect(exit, { - onSuccess: () => Lens.set(this.issues, Array.empty()), - onFailure: c => Option.match( - Chunk.findFirst(Cause.failures(c), e => e._tag === "ParseError"), - { - onSome: e => Effect.flatMap( - ParseResult.ArrayFormatter.formatError(e), - v => Lens.set(this.issues, v), - ), - onNone: () => Effect.void, - }, - ), - }), - Lens.set(this.validationFiber, Option.none()), - ), - )) - ), + Lens.set(this.validationFiber, Option.none()), + ) + )), Effect.tap(fiber => Lens.set(this.validationFiber, Option.some(fiber))), Effect.andThen(Fiber.join), - Effect.tap(value => Effect.onExit( + + Effect.catchTag("ParseError", e => ParseResult.ArrayFormatter.formatError(e).pipe( + Effect.flatMap(v => Lens.set(this.issues, v)), + Effect.andThen(Effect.fail(e)), + )), + Effect.tap(value => Effect.ensuring( Effect.andThen( Lens.set(this.isCommitting, true), Lens.set(this.target, value), ), - () => Lens.set(this.isCommitting, false), + Lens.set(this.isCommitting, false), )), Effect.ignore,