|
|
|
@@ -1,4 +1,4 @@
|
|
|
|
import { Array, type Context, Effect, Equal, Fiber, flow, Option, ParseResult, Pipeable, Predicate, Schema, type Scope, Stream, SubscriptionRef } from "effect"
|
|
|
|
import { Array, Cause, Chunk, type Context, Effect, Exit, Fiber, Option, ParseResult, Pipeable, Predicate, Schema, type Scope, SubscriptionRef } from "effect"
|
|
|
|
import * as Form from "./Form.js"
|
|
|
|
import * as Form from "./Form.js"
|
|
|
|
import * as Lens from "./Lens.js"
|
|
|
|
import * as Lens from "./Lens.js"
|
|
|
|
import * as Subscribable from "./Subscribable.js"
|
|
|
|
import * as Subscribable from "./Subscribable.js"
|
|
|
|
@@ -39,139 +39,132 @@ export class SynchronizedFormImpl<
|
|
|
|
readonly [SynchronizedFormTypeId]: SynchronizedFormTypeId = SynchronizedFormTypeId
|
|
|
|
readonly [SynchronizedFormTypeId]: SynchronizedFormTypeId = SynchronizedFormTypeId
|
|
|
|
|
|
|
|
|
|
|
|
readonly path = [] as const
|
|
|
|
readonly path = [] as const
|
|
|
|
|
|
|
|
|
|
|
|
readonly value: Subscribable.Subscribable<Option.Option<A>, never, never>
|
|
|
|
|
|
|
|
readonly encodedValue: Lens.Lens<I, never, never, never, never>
|
|
|
|
readonly encodedValue: Lens.Lens<I, never, never, never, never>
|
|
|
|
readonly isValidating: Subscribable.Subscribable<boolean, never, never>
|
|
|
|
|
|
|
|
readonly canCommit: Subscribable.Subscribable<boolean, never, never>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
constructor(
|
|
|
|
readonly schema: Schema.Schema<A, I, R>,
|
|
|
|
readonly schema: Schema.Schema<A, I, R>,
|
|
|
|
readonly context: Context.Context<Scope.Scope | R | TRR | TRW>,
|
|
|
|
readonly context: Context.Context<Scope.Scope | R | TRR | TRW>,
|
|
|
|
readonly target: Lens.Lens<A, TER, TEW, TRR, TRW>,
|
|
|
|
readonly target: Lens.Lens<A, TER, TEW, TRR, TRW>,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
readonly value: Lens.Lens<Option.Option<A>, never, never, never, never>,
|
|
|
|
readonly internalEncodedValue: Lens.Lens<I, never, never, never, never>,
|
|
|
|
readonly internalEncodedValue: Lens.Lens<I, never, never, never, never>,
|
|
|
|
readonly issues: Lens.Lens<readonly ParseResult.ArrayFormatterIssue[], never, never, never, never>,
|
|
|
|
readonly issues: Lens.Lens<readonly ParseResult.ArrayFormatterIssue[], never, never, never, never>,
|
|
|
|
readonly validationFiber: Lens.Lens<Option.Option<Fiber.Fiber<A, ParseResult.ParseError>>, never, never, never, never>,
|
|
|
|
readonly validationFiber: Lens.Lens<Option.Option<Fiber.Fiber<A, ParseResult.ParseError>>, never, never, never, never>,
|
|
|
|
|
|
|
|
readonly isValidating: Subscribable.Subscribable<boolean, never, never>,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
readonly canCommit: Subscribable.Subscribable<boolean, never, never>,
|
|
|
|
readonly isCommitting: Lens.Lens<boolean, never, never>,
|
|
|
|
readonly isCommitting: Lens.Lens<boolean, never, never>,
|
|
|
|
|
|
|
|
|
|
|
|
readonly runSemaphore: Effect.Semaphore,
|
|
|
|
readonly runSemaphore: Effect.Semaphore,
|
|
|
|
) {
|
|
|
|
) {
|
|
|
|
super()
|
|
|
|
super()
|
|
|
|
|
|
|
|
this.encodedValue = makeEncodedValueLens(this)
|
|
|
|
this.value = Effect.succeed(this).pipe(
|
|
|
|
|
|
|
|
Effect.map(self => Subscribable.make({
|
|
|
|
|
|
|
|
get get() { return Effect.provide(Effect.option(self.target.get), self.context) },
|
|
|
|
|
|
|
|
get changes() {
|
|
|
|
|
|
|
|
return Stream.provideContext(
|
|
|
|
|
|
|
|
self.target.changes.pipe(
|
|
|
|
|
|
|
|
Stream.map(Option.some),
|
|
|
|
|
|
|
|
Stream.catchAll(() => Stream.make(Option.none())),
|
|
|
|
|
|
|
|
),
|
|
|
|
|
|
|
|
self.context,
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
})),
|
|
|
|
|
|
|
|
Subscribable.unwrap,
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
this.encodedValue = Effect.succeed(this).pipe(
|
|
|
|
|
|
|
|
Effect.map(self => Lens.make<I, never, never, never, never>({
|
|
|
|
|
|
|
|
get get() { return self.internalEncodedValue.get },
|
|
|
|
|
|
|
|
get changes() { return self.internalEncodedValue.changes },
|
|
|
|
|
|
|
|
modify: f => self.internalEncodedValue.modify(
|
|
|
|
|
|
|
|
encodedValue => Effect.map(
|
|
|
|
|
|
|
|
f(encodedValue),
|
|
|
|
|
|
|
|
([b, nextEncodedValue]) => [
|
|
|
|
|
|
|
|
[b, nextEncodedValue] as const,
|
|
|
|
|
|
|
|
nextEncodedValue,
|
|
|
|
|
|
|
|
] as const
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
).pipe(
|
|
|
|
|
|
|
|
Effect.tap(([, nextEncodedValue]) =>
|
|
|
|
|
|
|
|
self.synchronizeEncodedValue(nextEncodedValue).pipe(
|
|
|
|
|
|
|
|
Effect.forkScoped,
|
|
|
|
|
|
|
|
Effect.provide(self.context),
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
),
|
|
|
|
|
|
|
|
Effect.map(([b]) => b),
|
|
|
|
|
|
|
|
),
|
|
|
|
|
|
|
|
})),
|
|
|
|
|
|
|
|
Lens.unwrap,
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
this.isValidating = Effect.succeed(this).pipe(
|
|
|
|
|
|
|
|
Effect.map(self => Subscribable.map(self.validationFiber, Option.isSome)),
|
|
|
|
|
|
|
|
Subscribable.unwrap,
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
this.canCommit = Effect.succeed(this).pipe(
|
|
|
|
|
|
|
|
Effect.map(self => Subscribable.map(
|
|
|
|
|
|
|
|
Subscribable.zipLatestAll(self.issues, self.validationFiber, self.isCommitting),
|
|
|
|
|
|
|
|
([issues, validationFiber, isCommitting]) => (
|
|
|
|
|
|
|
|
Array.isEmptyReadonlyArray(issues) &&
|
|
|
|
|
|
|
|
Option.isNone(validationFiber) &&
|
|
|
|
|
|
|
|
!isCommitting
|
|
|
|
|
|
|
|
),
|
|
|
|
|
|
|
|
)),
|
|
|
|
|
|
|
|
Subscribable.unwrap,
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
synchronizeEncodedValue(encodedValue: I): Effect.Effect<void, TER | TEW, never> {
|
|
|
|
synchronizeEncodedValue(encodedValue: I): Effect.Effect<void, never, never> {
|
|
|
|
return Lens.get(this.validationFiber).pipe(
|
|
|
|
return Lens.get(this.validationFiber).pipe(
|
|
|
|
Effect.andThen(Option.match({
|
|
|
|
Effect.andThen(Option.match({
|
|
|
|
onSome: Fiber.interrupt,
|
|
|
|
onSome: Fiber.interrupt,
|
|
|
|
onNone: () => Effect.void,
|
|
|
|
onNone: () => Effect.void,
|
|
|
|
})),
|
|
|
|
})),
|
|
|
|
Effect.andThen(Effect.forkScoped(
|
|
|
|
Effect.andThen(
|
|
|
|
Effect.ensuring(
|
|
|
|
Effect.forkScoped(Effect.onExit(
|
|
|
|
Schema.decode(this.schema, { errors: "all" })(encodedValue),
|
|
|
|
Schema.decode(this.schema, { errors: "all" })(encodedValue),
|
|
|
|
Lens.set(this.validationFiber, Option.none()),
|
|
|
|
exit => Effect.andThen(
|
|
|
|
)
|
|
|
|
Exit.matchEffect(exit, {
|
|
|
|
)),
|
|
|
|
onSuccess: v => Effect.andThen(
|
|
|
|
Effect.tap(fiber => Lens.set(this.validationFiber, Option.some(fiber))),
|
|
|
|
Lens.set(this.value, Option.some(v)),
|
|
|
|
Effect.flatMap(Fiber.join),
|
|
|
|
Lens.set(this.issues, Array.empty()),
|
|
|
|
|
|
|
|
),
|
|
|
|
Effect.flatMap(value => Effect.ensuring(
|
|
|
|
onFailure: c => Option.match(
|
|
|
|
Lens.set(this.isCommitting, true).pipe(
|
|
|
|
Chunk.findFirst(Cause.failures(c), e => e._tag === "ParseError"),
|
|
|
|
Effect.andThen(Lens.set(this.issues, Array.empty())),
|
|
|
|
{
|
|
|
|
Effect.andThen(Lens.set(this.target, value)),
|
|
|
|
onSome: e => Effect.flatMap(
|
|
|
|
),
|
|
|
|
ParseResult.ArrayFormatter.formatError(e),
|
|
|
|
Lens.set(this.isCommitting, false),
|
|
|
|
v => Lens.set(this.issues, v),
|
|
|
|
)),
|
|
|
|
),
|
|
|
|
Effect.catchIf(
|
|
|
|
onNone: () => Effect.void,
|
|
|
|
ParseResult.isParseError,
|
|
|
|
},
|
|
|
|
flow(
|
|
|
|
),
|
|
|
|
ParseResult.ArrayFormatter.formatError,
|
|
|
|
}),
|
|
|
|
Effect.flatMap(v => Lens.set(this.issues, v)),
|
|
|
|
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.andThen(
|
|
|
|
|
|
|
|
Lens.set(this.isCommitting, true),
|
|
|
|
|
|
|
|
Lens.set(this.target, value),
|
|
|
|
|
|
|
|
),
|
|
|
|
|
|
|
|
() => Lens.set(this.isCommitting, false),
|
|
|
|
|
|
|
|
)),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Effect.ignore,
|
|
|
|
Effect.provide(this.context),
|
|
|
|
Effect.provide(this.context),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
get run(): Effect.Effect<void, TER, never> {
|
|
|
|
get run(): Effect.Effect<void, TER> {
|
|
|
|
return this.runSemaphore.withPermits(1)(Effect.provide(
|
|
|
|
return Effect.void
|
|
|
|
Stream.runForEach(
|
|
|
|
// return this.runSemaphore.withPermits(1)(Effect.provide(
|
|
|
|
Stream.drop(this.target.changes, 1),
|
|
|
|
// Effect.andThen(
|
|
|
|
targetValue => Schema.encode(this.schema, { errors: "all" })(targetValue).pipe(
|
|
|
|
// Effect.flatMap(
|
|
|
|
Effect.flatMap(encodedValue => Effect.whenEffect(
|
|
|
|
// Lens.get(this.internalEncodedValue),
|
|
|
|
Effect.andThen(
|
|
|
|
// encodedValue => this.synchronizeEncodedValue(encodedValue),
|
|
|
|
Lens.set(this.issues, Array.empty()),
|
|
|
|
// ),
|
|
|
|
Lens.set(this.internalEncodedValue, encodedValue),
|
|
|
|
// Stream.runForEach(
|
|
|
|
),
|
|
|
|
// Stream.drop(this.target.changes, 1),
|
|
|
|
Effect.map(
|
|
|
|
|
|
|
|
Lens.get(this.internalEncodedValue),
|
|
|
|
// targetValue => Schema.encode(this.schema, { errors: "all" })(targetValue).pipe(
|
|
|
|
currentEncodedValue => !Equal.equals(encodedValue, currentEncodedValue),
|
|
|
|
// Effect.flatMap(encodedValue => Effect.andThen(
|
|
|
|
),
|
|
|
|
// Effect.whenEffect(
|
|
|
|
)),
|
|
|
|
// Lens.set(this.internalEncodedValue, encodedValue),
|
|
|
|
Effect.ignore,
|
|
|
|
// Effect.map(
|
|
|
|
),
|
|
|
|
// Lens.get(this.internalEncodedValue),
|
|
|
|
),
|
|
|
|
// currentEncodedValue => !Equal.equals(encodedValue, currentEncodedValue),
|
|
|
|
this.context,
|
|
|
|
// ),
|
|
|
|
))
|
|
|
|
// ),
|
|
|
|
|
|
|
|
// Effect.andThen(
|
|
|
|
|
|
|
|
// Lens.set(this.value, Option.some(targetValue)),
|
|
|
|
|
|
|
|
// Lens.set(this.issues, Array.empty()),
|
|
|
|
|
|
|
|
// ),
|
|
|
|
|
|
|
|
// )),
|
|
|
|
|
|
|
|
// Effect.ignore,
|
|
|
|
|
|
|
|
// ),
|
|
|
|
|
|
|
|
// ),
|
|
|
|
|
|
|
|
// ),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// this.context,
|
|
|
|
|
|
|
|
// ))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const makeEncodedValueLens = <A, I, R, TER, TEW, TRR, TRW>(
|
|
|
|
|
|
|
|
self: SynchronizedFormImpl<A, I, R, TER, TEW, TRR, TRW>
|
|
|
|
|
|
|
|
): Lens.Lens<I, never, never, never, never> => Lens.make({
|
|
|
|
|
|
|
|
get get() { return self.internalEncodedValue.get },
|
|
|
|
|
|
|
|
get changes() { return self.internalEncodedValue.changes },
|
|
|
|
|
|
|
|
modify: f => self.internalEncodedValue.modify(
|
|
|
|
|
|
|
|
encodedValue => Effect.map(
|
|
|
|
|
|
|
|
f(encodedValue),
|
|
|
|
|
|
|
|
([b, nextEncodedValue]) => [
|
|
|
|
|
|
|
|
[b, nextEncodedValue] as const,
|
|
|
|
|
|
|
|
nextEncodedValue,
|
|
|
|
|
|
|
|
] as const
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
).pipe(
|
|
|
|
|
|
|
|
Effect.tap(([, nextEncodedValue]) =>
|
|
|
|
|
|
|
|
self.synchronizeEncodedValue(nextEncodedValue).pipe(
|
|
|
|
|
|
|
|
Effect.forkScoped,
|
|
|
|
|
|
|
|
Effect.provide(self.context),
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
),
|
|
|
|
|
|
|
|
Effect.map(([b]) => b),
|
|
|
|
|
|
|
|
),
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const isSynchronizedForm = (u: unknown): u is SynchronizedForm<unknown, unknown, unknown, unknown, unknown, unknown, unknown> => Predicate.hasProperty(u, SynchronizedFormTypeId)
|
|
|
|
export const isSynchronizedForm = (u: unknown): u is SynchronizedForm<unknown, unknown, unknown, unknown, unknown, unknown, unknown> => Predicate.hasProperty(u, SynchronizedFormTypeId)
|
|
|
|
|
|
|
|
|
|
|
|
@@ -191,6 +184,11 @@ export const make = Effect.fnUntraced(function* <A, I = A, R = never, TER = neve
|
|
|
|
ParseResult.ParseError | TER,
|
|
|
|
ParseResult.ParseError | TER,
|
|
|
|
Scope.Scope | R | TRR | TRW
|
|
|
|
Scope.Scope | R | TRR | TRW
|
|
|
|
> {
|
|
|
|
> {
|
|
|
|
|
|
|
|
const valueLens = Lens.fromSubscriptionRef(yield* SubscriptionRef.make(Option.none<A>()))
|
|
|
|
|
|
|
|
const issuesLens = Lens.fromSubscriptionRef(yield* SubscriptionRef.make<readonly ParseResult.ArrayFormatterIssue[]>(Array.empty()))
|
|
|
|
|
|
|
|
const validationFiberLens = Lens.fromSubscriptionRef(yield* SubscriptionRef.make(Option.none<Fiber.Fiber<A, ParseResult.ParseError>>()))
|
|
|
|
|
|
|
|
const isCommittingLens = Lens.fromSubscriptionRef(yield* SubscriptionRef.make(false))
|
|
|
|
|
|
|
|
|
|
|
|
const initialEncodedValue = options.initialEncodedValue !== undefined
|
|
|
|
const initialEncodedValue = options.initialEncodedValue !== undefined
|
|
|
|
? options.initialEncodedValue
|
|
|
|
? options.initialEncodedValue
|
|
|
|
: yield* Effect.flatMap(
|
|
|
|
: yield* Effect.flatMap(
|
|
|
|
@@ -203,10 +201,22 @@ export const make = Effect.fnUntraced(function* <A, I = A, R = never, TER = neve
|
|
|
|
yield* Effect.context<Scope.Scope | R | TRR | TRW>(),
|
|
|
|
yield* Effect.context<Scope.Scope | R | TRR | TRW>(),
|
|
|
|
options.target,
|
|
|
|
options.target,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
valueLens,
|
|
|
|
Lens.fromSubscriptionRef(yield* SubscriptionRef.make(initialEncodedValue)),
|
|
|
|
Lens.fromSubscriptionRef(yield* SubscriptionRef.make(initialEncodedValue)),
|
|
|
|
Lens.fromSubscriptionRef(yield* SubscriptionRef.make<readonly ParseResult.ArrayFormatterIssue[]>(Array.empty())),
|
|
|
|
issuesLens,
|
|
|
|
Lens.fromSubscriptionRef(yield* SubscriptionRef.make(Option.none<Fiber.Fiber<A, ParseResult.ParseError>>())),
|
|
|
|
validationFiberLens,
|
|
|
|
Lens.fromSubscriptionRef(yield* SubscriptionRef.make(false)),
|
|
|
|
Subscribable.map(validationFiberLens, Option.isSome),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Subscribable.map(
|
|
|
|
|
|
|
|
Subscribable.zipLatestAll(valueLens, issuesLens, validationFiberLens, isCommittingLens),
|
|
|
|
|
|
|
|
([value, issues, validationFiber, isCommitting]) => (
|
|
|
|
|
|
|
|
Option.isSome(value) &&
|
|
|
|
|
|
|
|
Array.isEmptyReadonlyArray(issues) &&
|
|
|
|
|
|
|
|
Option.isNone(validationFiber) &&
|
|
|
|
|
|
|
|
!isCommitting
|
|
|
|
|
|
|
|
),
|
|
|
|
|
|
|
|
),
|
|
|
|
|
|
|
|
isCommittingLens,
|
|
|
|
|
|
|
|
|
|
|
|
yield* Effect.makeSemaphore(1),
|
|
|
|
yield* Effect.makeSemaphore(1),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|