From b6b199b2fa94bba51ef78a381f6b4f0d340882a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Valverd=C3=A9?= Date: Sat, 2 May 2026 00:35:07 +0200 Subject: [PATCH] Add sync mechanism --- packages/effect-fc/src/SynchronizedForm.ts | 51 +++++++++------------- 1 file changed, 20 insertions(+), 31 deletions(-) diff --git a/packages/effect-fc/src/SynchronizedForm.ts b/packages/effect-fc/src/SynchronizedForm.ts index ca92020..07ff2a3 100644 --- a/packages/effect-fc/src/SynchronizedForm.ts +++ b/packages/effect-fc/src/SynchronizedForm.ts @@ -1,4 +1,4 @@ -import { Array, type Context, Effect, Fiber, Option, ParseResult, Pipeable, Predicate, Schema, type Scope, Stream, SubscriptionRef } from "effect" +import { Array, type Context, Effect, Equal, 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" @@ -148,36 +148,25 @@ export class SynchronizedFormImpl< } get run(): Effect.Effect { - return Effect.void - // return this.runSemaphore.withPermits(1)(Effect.provide( - // Effect.andThen( - // Effect.flatMap( - // Lens.get(this.internalEncodedValue), - // encodedValue => this.synchronizeEncodedValue(encodedValue), - // ), - // Stream.runForEach( - // Stream.drop(this.target.changes, 1), - - // targetValue => Schema.encode(this.schema, { errors: "all" })(targetValue).pipe( - // Effect.flatMap(encodedValue => Effect.andThen( - // Effect.whenEffect( - // Lens.set(this.internalEncodedValue, encodedValue), - // Effect.map( - // Lens.get(this.internalEncodedValue), - // currentEncodedValue => !Equal.equals(encodedValue, currentEncodedValue), - // ), - // ), - // Effect.andThen( - // Lens.set(this.issues, Array.empty()), - // ), - // )), - // Effect.ignore, - // ), - // ), - // ), - - // this.context, - // )) + return this.runSemaphore.withPermits(1)(Effect.provide( + Stream.runForEach( + Stream.drop(this.target.changes, 1), + targetValue => Schema.encode(this.schema, { errors: "all" })(targetValue).pipe( + Effect.flatMap(encodedValue => Effect.andThen( + Effect.whenEffect( + Lens.set(this.internalEncodedValue, encodedValue), + Effect.map( + Lens.get(this.internalEncodedValue), + currentEncodedValue => !Equal.equals(encodedValue, currentEncodedValue), + ), + ), + Lens.set(this.issues, Array.empty()), + )), + Effect.ignore, + ), + ), + this.context, + )) } }