0.2.6 #49
@@ -1,9 +1,7 @@
|
||||
import { Array, Cause, Chunk, type Context, type Duration, Effect, Equal, Exit, Fiber, Function, identity, Option, ParseResult, Pipeable, Predicate, Schema, type Scope, Stream } from "effect"
|
||||
import { Array, type Cause, Chunk, type Duration, Effect, Equal, Function, identity, Option, type ParseResult, Pipeable, Predicate, type Scope, Stream } from "effect"
|
||||
import type * as React from "react"
|
||||
import * as Component from "./Component.js"
|
||||
import * as Lens from "./Lens.js"
|
||||
import * as Mutation from "./Mutation.js"
|
||||
import * as Result from "./Result.js"
|
||||
import * as Subscribable from "./Subscribable.js"
|
||||
import * as SubscriptionRef from "./SubscriptionRef.js"
|
||||
|
||||
@@ -42,379 +40,7 @@ extends Pipeable.Class() implements Form<P, A, I, ER, EW> {
|
||||
}
|
||||
|
||||
|
||||
export const SubmittableFormTypeId: unique symbol = Symbol.for("@effect-fc/Form/SubmittableForm")
|
||||
export type SubmittableFormTypeId = typeof SubmittableFormTypeId
|
||||
|
||||
export interface SubmittableForm<in out A, in out I = A, in out R = never, in out MA = void, in out ME = never, in out MR = never, in out MP = never>
|
||||
extends Form<readonly [], A, I, never, never> {
|
||||
readonly [SubmittableFormTypeId]: SubmittableFormTypeId
|
||||
|
||||
readonly schema: Schema.Schema<A, I, R>
|
||||
readonly context: Context.Context<Scope.Scope | R>
|
||||
readonly mutation: Mutation.Mutation<
|
||||
readonly [value: A, form: SubmittableForm<A, I, R, unknown, unknown, unknown>],
|
||||
MA, ME, MR, MP
|
||||
>
|
||||
readonly validationFiber: Subscribable.Subscribable<Option.Option<Fiber.Fiber<A, ParseResult.ParseError>>, never, never>
|
||||
|
||||
readonly run: Effect.Effect<void>
|
||||
readonly submit: Effect.Effect<Option.Option<Result.Final<MA, ME, MP>>, Cause.NoSuchElementException>
|
||||
}
|
||||
|
||||
export class SubmittableFormImpl<in out A, in out I = A, in out R = never, in out MA = void, in out ME = never, in out MR = never, in out MP = never>
|
||||
extends Pipeable.Class() implements SubmittableForm<A, I, R, MA, ME, MR, MP> {
|
||||
readonly [FormTypeId]: FormTypeId = FormTypeId
|
||||
readonly [SubmittableFormTypeId]: SubmittableFormTypeId = SubmittableFormTypeId
|
||||
|
||||
readonly path = [] as const
|
||||
|
||||
constructor(
|
||||
readonly schema: Schema.Schema<A, I, R>,
|
||||
readonly context: Context.Context<Scope.Scope | R>,
|
||||
readonly mutation: Mutation.Mutation<
|
||||
readonly [value: A, form: SubmittableForm<A, I, R, unknown, unknown, unknown>],
|
||||
MA, ME, MR, MP
|
||||
>,
|
||||
readonly value: Lens.Lens<Option.Option<A>, never, never, never, never>,
|
||||
readonly encodedValue: Lens.Lens<I, 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 isValidating: Subscribable.Subscribable<boolean, never, never>,
|
||||
|
||||
readonly canCommit: Subscribable.Subscribable<boolean, never, never>,
|
||||
readonly isCommitting: Subscribable.Subscribable<boolean, never, never>,
|
||||
|
||||
readonly runSemaphore: Effect.Semaphore,
|
||||
) {
|
||||
super()
|
||||
}
|
||||
|
||||
get run(): Effect.Effect<void> {
|
||||
return this.runSemaphore.withPermits(1)(Stream.runForEach(
|
||||
this.encodedValue.changes,
|
||||
|
||||
encodedValue => Lens.get(this.validationFiber).pipe(
|
||||
Effect.andThen(Option.match({
|
||||
onSome: Fiber.interrupt,
|
||||
onNone: () => Effect.void,
|
||||
})),
|
||||
Effect.andThen(
|
||||
Effect.forkScoped(Effect.onExit(
|
||||
Schema.decode(this.schema, { errors: "all" })(encodedValue),
|
||||
exit => Effect.andThen(
|
||||
Exit.matchEffect(exit, {
|
||||
onSuccess: v => Effect.andThen(
|
||||
Lens.set(this.value, Option.some(v)),
|
||||
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()),
|
||||
),
|
||||
)).pipe(
|
||||
Effect.tap(fiber => Lens.set(this.validationFiber, Option.some(fiber))),
|
||||
Effect.andThen(Fiber.join),
|
||||
Effect.forkScoped,
|
||||
)
|
||||
),
|
||||
Effect.provide(this.context),
|
||||
),
|
||||
))
|
||||
}
|
||||
|
||||
get submit(): Effect.Effect<Option.Option<Result.Final<MA, ME, MP>>, Cause.NoSuchElementException> {
|
||||
return Lens.get(this.value).pipe(
|
||||
Effect.andThen(identity),
|
||||
Effect.andThen(value => this.submitValue(value)),
|
||||
)
|
||||
}
|
||||
|
||||
submitValue(value: A): Effect.Effect<Option.Option<Result.Final<MA, ME, MP>>> {
|
||||
return Effect.whenEffect(
|
||||
Effect.tap(
|
||||
this.mutation.mutate([value, this as any]),
|
||||
result => Result.isFailure(result)
|
||||
? Option.match(
|
||||
Chunk.findFirst(
|
||||
Cause.failures(result.cause as Cause.Cause<ParseResult.ParseError>),
|
||||
e => e._tag === "ParseError",
|
||||
),
|
||||
{
|
||||
onSome: e => Effect.flatMap(
|
||||
ParseResult.ArrayFormatter.formatError(e),
|
||||
v => Lens.set(this.issues, v),
|
||||
),
|
||||
onNone: () => Effect.void,
|
||||
},
|
||||
)
|
||||
: Effect.void
|
||||
),
|
||||
this.canCommit.get,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export const SynchronizedFormTypeId: unique symbol = Symbol.for("@effect-fc/Form/SynchronizedForm")
|
||||
export type SynchronizedFormTypeId = typeof SynchronizedFormTypeId
|
||||
|
||||
export interface SynchronizedForm<
|
||||
in out A,
|
||||
in out I = A,
|
||||
in out R = never,
|
||||
in out TER = never,
|
||||
in out TEW = never,
|
||||
in out TRR = never,
|
||||
in out TRW = never,
|
||||
> extends Form<readonly [], A, I, never, never> {
|
||||
readonly [SynchronizedFormTypeId]: SynchronizedFormTypeId
|
||||
|
||||
readonly schema: Schema.Schema<A, I, R>
|
||||
readonly context: Context.Context<Scope.Scope | R | TRR | TRW>
|
||||
readonly target: Lens.Lens<A, TER, TEW, TRR, TRW>
|
||||
readonly validationFiber: Subscribable.Subscribable<Option.Option<Fiber.Fiber<A, ParseResult.ParseError>>, never, never>
|
||||
|
||||
readonly run: Effect.Effect<void, ParseResult.ParseError | TER | TEW>
|
||||
}
|
||||
|
||||
export class SynchronizedFormImpl<
|
||||
in out A,
|
||||
in out I = A,
|
||||
in out R = never,
|
||||
in out TER = never,
|
||||
in out TEW = never,
|
||||
in out TRR = never,
|
||||
in out TRW = never,
|
||||
> extends Pipeable.Class() implements SynchronizedForm<A, I, R, TER, TEW, TRR, TRW> {
|
||||
readonly [FormTypeId]: FormTypeId = FormTypeId
|
||||
readonly [SynchronizedFormTypeId]: SynchronizedFormTypeId = SynchronizedFormTypeId
|
||||
|
||||
readonly path = [] as const
|
||||
|
||||
constructor(
|
||||
readonly schema: Schema.Schema<A, I, R>,
|
||||
readonly context: Context.Context<Scope.Scope | R | TRR | TRW>,
|
||||
readonly target: Lens.Lens<A, TER, TEW, TRR, TRW>,
|
||||
|
||||
readonly value: Lens.Lens<Option.Option<A>, never, never, never, never>,
|
||||
readonly encodedValue: Lens.Lens<I, 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 isValidating: Subscribable.Subscribable<boolean, never, never>,
|
||||
|
||||
readonly canCommit: Subscribable.Subscribable<boolean, never, never>,
|
||||
readonly isCommitting: Lens.Lens<boolean, never, never>,
|
||||
|
||||
readonly runSemaphore: Effect.Semaphore,
|
||||
) {
|
||||
super()
|
||||
}
|
||||
|
||||
get run(): Effect.Effect<void, ParseResult.ParseError | TER | TEW> {
|
||||
return this.runSemaphore.withPermits(1)(Effect.provide(
|
||||
Effect.all([
|
||||
Stream.runForEach(
|
||||
this.encodedValue.changes,
|
||||
|
||||
encodedValue => Lens.get(this.validationFiber).pipe(
|
||||
Effect.andThen(Option.match({
|
||||
onSome: Fiber.interrupt,
|
||||
onNone: () => Effect.void,
|
||||
})),
|
||||
Effect.andThen(
|
||||
Effect.forkScoped(Effect.onExit(
|
||||
Schema.decode(this.schema, { errors: "all" })(encodedValue),
|
||||
exit => Effect.andThen(
|
||||
Exit.matchEffect(exit, {
|
||||
onSuccess: v => Lens.set(this.value, Option.some(v)).pipe(
|
||||
Effect.andThen(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()),
|
||||
),
|
||||
)).pipe(
|
||||
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.forkScoped,
|
||||
)
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
Stream.runForEach(
|
||||
Stream.drop(this.target.changes, 1),
|
||||
|
||||
targetValue => Schema.encode(this.schema, { errors: "all" })(targetValue).pipe(
|
||||
Effect.flatMap(encodedValue => Effect.whenEffect(
|
||||
Lens.set(this.encodedValue, encodedValue),
|
||||
Effect.map(
|
||||
Lens.get(this.encodedValue),
|
||||
currentEncodedValue => !Equal.equals(encodedValue, currentEncodedValue),
|
||||
),
|
||||
)),
|
||||
),
|
||||
),
|
||||
], { concurrency: "unbounded", discard: true }),
|
||||
|
||||
this.context,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export const isForm = (u: unknown): u is Form<readonly PropertyKey[], unknown, unknown> => Predicate.hasProperty(u, FormTypeId)
|
||||
export const isSubmittableForm = (u: unknown): u is SubmittableForm<unknown, unknown, unknown, unknown, unknown, unknown, unknown> => Predicate.hasProperty(u, SubmittableFormTypeId)
|
||||
export const isSynchronizedForm = (u: unknown): u is SynchronizedForm<unknown, unknown, unknown, unknown, unknown, unknown, unknown> => Predicate.hasProperty(u, SynchronizedFormTypeId)
|
||||
|
||||
export declare namespace makeSubmittable {
|
||||
export interface Options<in out A, in out I = A, in out R = never, in out MA = void, in out ME = never, in out MR = never, in out MP = never>
|
||||
extends Mutation.make.Options<
|
||||
readonly [value: NoInfer<A>, form: SubmittableForm<NoInfer<A>, NoInfer<I>, NoInfer<R>, unknown, unknown, unknown>],
|
||||
MA, ME, MR, MP
|
||||
> {
|
||||
readonly schema: Schema.Schema<A, I, R>
|
||||
readonly initialEncodedValue: NoInfer<I>
|
||||
}
|
||||
}
|
||||
|
||||
export const makeSubmittable = Effect.fnUntraced(function* <A, I = A, R = never, MA = void, ME = never, MR = never, MP = never>(
|
||||
options: makeSubmittable.Options<A, I, R, MA, ME, MR, MP>
|
||||
): Effect.fn.Return<
|
||||
SubmittableForm<A, I, R, MA, ME, Result.forkEffect.OutputContext<MR, MP>, MP>,
|
||||
never,
|
||||
Scope.Scope | R | Result.forkEffect.OutputContext<MR, MP>
|
||||
> {
|
||||
const mutation = yield* Mutation.make(options)
|
||||
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>>()))
|
||||
|
||||
return new SubmittableFormImpl(
|
||||
options.schema,
|
||||
yield* Effect.context<Scope.Scope | R>(),
|
||||
mutation,
|
||||
|
||||
valueLens,
|
||||
Lens.fromSubscriptionRef(yield* SubscriptionRef.make(options.initialEncodedValue)),
|
||||
issuesLens,
|
||||
validationFiberLens,
|
||||
Subscribable.map(validationFiberLens, Option.isSome),
|
||||
|
||||
Subscribable.map(
|
||||
Subscribable.zipLatestAll(valueLens, issuesLens, validationFiberLens, mutation.result),
|
||||
([value, issues, validationFiber, result]) => (
|
||||
Option.isSome(value) &&
|
||||
Array.isEmptyReadonlyArray(issues) &&
|
||||
Option.isNone(validationFiber) &&
|
||||
!(Result.isRunning(result) || Result.hasRefreshingFlag(result))
|
||||
),
|
||||
),
|
||||
Subscribable.map(mutation.result, result => Result.isRunning(result) || Result.hasRefreshingFlag(result)),
|
||||
|
||||
yield* Effect.makeSemaphore(1),
|
||||
)
|
||||
})
|
||||
|
||||
export declare namespace serviceSubmittable {
|
||||
export interface Options<in out A, in out I = A, in out R = never, in out MA = void, in out ME = never, in out MR = never, in out MP = never>
|
||||
extends makeSubmittable.Options<A, I, R, MA, ME, MR, MP> {}
|
||||
}
|
||||
|
||||
export const serviceSubmittable = <A, I = A, R = never, MA = void, ME = never, MR = never, MP = never>(
|
||||
options: serviceSubmittable.Options<A, I, R, MA, ME, MR, MP>
|
||||
): Effect.Effect<
|
||||
SubmittableForm<A, I, R, MA, ME, Result.forkEffect.OutputContext<MR, MP>, MP>,
|
||||
never,
|
||||
Scope.Scope | R | Result.forkEffect.OutputContext<MR, MP>
|
||||
> => Effect.tap(
|
||||
makeSubmittable(options),
|
||||
form => Effect.forkScoped(form.run),
|
||||
)
|
||||
|
||||
export declare namespace makeSynchronized {
|
||||
export interface Options<in out A, in out I = A, in out R = never, in out TER = never, in out TEW = never, in out TRR = never, in out TRW = never> {
|
||||
readonly schema: Schema.Schema<A, I, R>
|
||||
readonly target: Lens.Lens<A, TER, TEW, TRR, TRW>
|
||||
readonly initialEncodedValue: NoInfer<I>
|
||||
}
|
||||
}
|
||||
|
||||
export const makeSynchronized = Effect.fnUntraced(function* <A, I = A, R = never, TER = never, TEW = never, TRR = never, TRW = never>(
|
||||
options: makeSynchronized.Options<A, I, R, TER, TEW, TRR, TRW>
|
||||
): Effect.fn.Return<
|
||||
SynchronizedForm<A, I, R, TER, TEW, TRR, TRW>,
|
||||
ParseResult.ParseError | TER,
|
||||
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 = yield* Lens.get(options.target).pipe(
|
||||
Effect.flatMap(Schema.encode(options.schema, { errors: "all" })),
|
||||
)
|
||||
|
||||
return new SynchronizedFormImpl(
|
||||
options.schema,
|
||||
yield* Effect.context<Scope.Scope | R | TRR | TRW>(),
|
||||
options.target,
|
||||
|
||||
valueLens,
|
||||
Lens.fromSubscriptionRef(yield* SubscriptionRef.make(initialEncodedValue)),
|
||||
issuesLens,
|
||||
validationFiberLens,
|
||||
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),
|
||||
)
|
||||
})
|
||||
|
||||
export declare namespace serviceSynchronized {
|
||||
export interface Options<in out A, in out I = A, in out R = never, in out TER = never, in out TEW = never, in out TRR = never, in out TRW = never>
|
||||
extends makeSynchronized.Options<A, I, R, TER, TEW, TRR, TRW> {}
|
||||
}
|
||||
|
||||
export const serviceSynchronized = <A, I = A, R = never, TER = never, TEW = never, TRR = never, TRW = never>(
|
||||
options: serviceSynchronized.Options<A, I, R, TER, TEW, TRR, TRW>
|
||||
): Effect.Effect<
|
||||
SynchronizedForm<A, I, R, TER, TEW, TRR, TRW>,
|
||||
ParseResult.ParseError | TER,
|
||||
Scope.Scope | R | TRR | TRW
|
||||
> => Effect.tap(
|
||||
makeSynchronized(options),
|
||||
form => Effect.forkScoped(form.run),
|
||||
)
|
||||
|
||||
|
||||
const filterIssuesByPath = (
|
||||
|
||||
194
packages/effect-fc/src/SubmittableForm.ts
Normal file
194
packages/effect-fc/src/SubmittableForm.ts
Normal file
@@ -0,0 +1,194 @@
|
||||
import { Array, Cause, Chunk, type Context, Effect, Exit, Fiber, identity, Option, ParseResult, Pipeable, Predicate, Schema, type Scope, Stream } from "effect"
|
||||
import * as Form from "./Form.js"
|
||||
import * as Lens from "./Lens.js"
|
||||
import * as Mutation from "./Mutation.js"
|
||||
import * as Result from "./Result.js"
|
||||
import * as Subscribable from "./Subscribable.js"
|
||||
import * as SubscriptionRef from "./SubscriptionRef.js"
|
||||
|
||||
|
||||
export const SubmittableFormTypeId: unique symbol = Symbol.for("@effect-fc/Form/SubmittableForm")
|
||||
export type SubmittableFormTypeId = typeof SubmittableFormTypeId
|
||||
|
||||
export interface SubmittableForm<in out A, in out I = A, in out R = never, in out MA = void, in out ME = never, in out MR = never, in out MP = never>
|
||||
extends Form.Form<readonly [], A, I, never, never> {
|
||||
readonly [SubmittableFormTypeId]: SubmittableFormTypeId
|
||||
|
||||
readonly schema: Schema.Schema<A, I, R>
|
||||
readonly context: Context.Context<Scope.Scope | R>
|
||||
readonly mutation: Mutation.Mutation<
|
||||
readonly [value: A, form: SubmittableForm<A, I, R, unknown, unknown, unknown>],
|
||||
MA, ME, MR, MP
|
||||
>
|
||||
readonly validationFiber: Subscribable.Subscribable<Option.Option<Fiber.Fiber<A, ParseResult.ParseError>>, never, never>
|
||||
|
||||
readonly run: Effect.Effect<void>
|
||||
readonly submit: Effect.Effect<Option.Option<Result.Final<MA, ME, MP>>, Cause.NoSuchElementException>
|
||||
}
|
||||
|
||||
export class SubmittableFormImpl<in out A, in out I = A, in out R = never, in out MA = void, in out ME = never, in out MR = never, in out MP = never>
|
||||
extends Pipeable.Class() implements SubmittableForm<A, I, R, MA, ME, MR, MP> {
|
||||
readonly [Form.FormTypeId]: Form.FormTypeId = Form.FormTypeId
|
||||
readonly [SubmittableFormTypeId]: SubmittableFormTypeId = SubmittableFormTypeId
|
||||
|
||||
readonly path = [] as const
|
||||
|
||||
constructor(
|
||||
readonly schema: Schema.Schema<A, I, R>,
|
||||
readonly context: Context.Context<Scope.Scope | R>,
|
||||
readonly mutation: Mutation.Mutation<
|
||||
readonly [value: A, form: SubmittableForm<A, I, R, unknown, unknown, unknown>],
|
||||
MA, ME, MR, MP
|
||||
>,
|
||||
readonly value: Lens.Lens<Option.Option<A>, never, never, never, never>,
|
||||
readonly encodedValue: Lens.Lens<I, 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 isValidating: Subscribable.Subscribable<boolean, never, never>,
|
||||
|
||||
readonly canCommit: Subscribable.Subscribable<boolean, never, never>,
|
||||
readonly isCommitting: Subscribable.Subscribable<boolean, never, never>,
|
||||
|
||||
readonly runSemaphore: Effect.Semaphore,
|
||||
) {
|
||||
super()
|
||||
}
|
||||
|
||||
get run(): Effect.Effect<void> {
|
||||
return this.runSemaphore.withPermits(1)(Stream.runForEach(
|
||||
this.encodedValue.changes,
|
||||
|
||||
encodedValue => Lens.get(this.validationFiber).pipe(
|
||||
Effect.andThen(Option.match({
|
||||
onSome: Fiber.interrupt,
|
||||
onNone: () => Effect.void,
|
||||
})),
|
||||
Effect.andThen(
|
||||
Effect.forkScoped(Effect.onExit(
|
||||
Schema.decode(this.schema, { errors: "all" })(encodedValue),
|
||||
exit => Effect.andThen(
|
||||
Exit.matchEffect(exit, {
|
||||
onSuccess: v => Effect.andThen(
|
||||
Lens.set(this.value, Option.some(v)),
|
||||
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()),
|
||||
),
|
||||
)).pipe(
|
||||
Effect.tap(fiber => Lens.set(this.validationFiber, Option.some(fiber))),
|
||||
Effect.andThen(Fiber.join),
|
||||
Effect.forkScoped,
|
||||
)
|
||||
),
|
||||
Effect.provide(this.context),
|
||||
),
|
||||
))
|
||||
}
|
||||
|
||||
get submit(): Effect.Effect<Option.Option<Result.Final<MA, ME, MP>>, Cause.NoSuchElementException> {
|
||||
return Lens.get(this.value).pipe(
|
||||
Effect.andThen(identity),
|
||||
Effect.andThen(value => this.submitValue(value)),
|
||||
)
|
||||
}
|
||||
|
||||
submitValue(value: A): Effect.Effect<Option.Option<Result.Final<MA, ME, MP>>> {
|
||||
return Effect.whenEffect(
|
||||
Effect.tap(
|
||||
this.mutation.mutate([value, this as any]),
|
||||
result => Result.isFailure(result)
|
||||
? Option.match(
|
||||
Chunk.findFirst(
|
||||
Cause.failures(result.cause as Cause.Cause<ParseResult.ParseError>),
|
||||
e => e._tag === "ParseError",
|
||||
),
|
||||
{
|
||||
onSome: e => Effect.flatMap(
|
||||
ParseResult.ArrayFormatter.formatError(e),
|
||||
v => Lens.set(this.issues, v),
|
||||
),
|
||||
onNone: () => Effect.void,
|
||||
},
|
||||
)
|
||||
: Effect.void
|
||||
),
|
||||
this.canCommit.get,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export const isSubmittableForm = (u: unknown): u is SubmittableForm<unknown, unknown, unknown, unknown, unknown, unknown, unknown> => Predicate.hasProperty(u, SubmittableFormTypeId)
|
||||
|
||||
export declare namespace makeSubmittable {
|
||||
export interface Options<in out A, in out I = A, in out R = never, in out MA = void, in out ME = never, in out MR = never, in out MP = never>
|
||||
extends Mutation.make.Options<
|
||||
readonly [value: NoInfer<A>, form: SubmittableForm<NoInfer<A>, NoInfer<I>, NoInfer<R>, unknown, unknown, unknown>],
|
||||
MA, ME, MR, MP
|
||||
> {
|
||||
readonly schema: Schema.Schema<A, I, R>
|
||||
readonly initialEncodedValue: NoInfer<I>
|
||||
}
|
||||
}
|
||||
|
||||
export const makeSubmittable = Effect.fnUntraced(function* <A, I = A, R = never, MA = void, ME = never, MR = never, MP = never>(
|
||||
options: makeSubmittable.Options<A, I, R, MA, ME, MR, MP>
|
||||
): Effect.fn.Return<
|
||||
SubmittableForm<A, I, R, MA, ME, Result.forkEffect.OutputContext<MR, MP>, MP>,
|
||||
never,
|
||||
Scope.Scope | R | Result.forkEffect.OutputContext<MR, MP>
|
||||
> {
|
||||
const mutation = yield* Mutation.make(options)
|
||||
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>>()))
|
||||
|
||||
return new SubmittableFormImpl(
|
||||
options.schema,
|
||||
yield* Effect.context<Scope.Scope | R>(),
|
||||
mutation,
|
||||
|
||||
valueLens,
|
||||
Lens.fromSubscriptionRef(yield* SubscriptionRef.make(options.initialEncodedValue)),
|
||||
issuesLens,
|
||||
validationFiberLens,
|
||||
Subscribable.map(validationFiberLens, Option.isSome),
|
||||
|
||||
Subscribable.map(
|
||||
Subscribable.zipLatestAll(valueLens, issuesLens, validationFiberLens, mutation.result),
|
||||
([value, issues, validationFiber, result]) => (
|
||||
Option.isSome(value) &&
|
||||
Array.isEmptyReadonlyArray(issues) &&
|
||||
Option.isNone(validationFiber) &&
|
||||
!(Result.isRunning(result) || Result.hasRefreshingFlag(result))
|
||||
),
|
||||
),
|
||||
Subscribable.map(mutation.result, result => Result.isRunning(result) || Result.hasRefreshingFlag(result)),
|
||||
|
||||
yield* Effect.makeSemaphore(1),
|
||||
)
|
||||
})
|
||||
|
||||
export declare namespace serviceSubmittable {
|
||||
export interface Options<in out A, in out I = A, in out R = never, in out MA = void, in out ME = never, in out MR = never, in out MP = never>
|
||||
extends makeSubmittable.Options<A, I, R, MA, ME, MR, MP> {}
|
||||
}
|
||||
|
||||
export const serviceSubmittable = <A, I = A, R = never, MA = void, ME = never, MR = never, MP = never>(
|
||||
options: serviceSubmittable.Options<A, I, R, MA, ME, MR, MP>
|
||||
): Effect.Effect<
|
||||
SubmittableForm<A, I, R, MA, ME, Result.forkEffect.OutputContext<MR, MP>, MP>,
|
||||
never,
|
||||
Scope.Scope | R | Result.forkEffect.OutputContext<MR, MP>
|
||||
> => Effect.tap(
|
||||
makeSubmittable(options),
|
||||
form => Effect.forkScoped(form.run),
|
||||
)
|
||||
194
packages/effect-fc/src/SynchronizedForm.ts
Normal file
194
packages/effect-fc/src/SynchronizedForm.ts
Normal file
@@ -0,0 +1,194 @@
|
||||
import { Array, Cause, Chunk, type Context, Effect, Equal, Exit, Fiber, Option, ParseResult, Pipeable, Predicate, Schema, type Scope, Stream } from "effect"
|
||||
import * as Form from "./Form.js"
|
||||
import * as Lens from "./Lens.js"
|
||||
import * as Subscribable from "./Subscribable.js"
|
||||
import * as SubscriptionRef from "./SubscriptionRef.js"
|
||||
|
||||
|
||||
export const SynchronizedFormTypeId: unique symbol = Symbol.for("@effect-fc/Form/SynchronizedForm")
|
||||
export type SynchronizedFormTypeId = typeof SynchronizedFormTypeId
|
||||
|
||||
export interface SynchronizedForm<
|
||||
in out A,
|
||||
in out I = A,
|
||||
in out R = never,
|
||||
in out TER = never,
|
||||
in out TEW = never,
|
||||
in out TRR = never,
|
||||
in out TRW = never,
|
||||
> extends Form.Form<readonly [], A, I, never, never> {
|
||||
readonly [SynchronizedFormTypeId]: SynchronizedFormTypeId
|
||||
|
||||
readonly schema: Schema.Schema<A, I, R>
|
||||
readonly context: Context.Context<Scope.Scope | R | TRR | TRW>
|
||||
readonly target: Lens.Lens<A, TER, TEW, TRR, TRW>
|
||||
readonly validationFiber: Subscribable.Subscribable<Option.Option<Fiber.Fiber<A, ParseResult.ParseError>>, never, never>
|
||||
|
||||
readonly run: Effect.Effect<void, ParseResult.ParseError | TER | TEW>
|
||||
}
|
||||
|
||||
export class SynchronizedFormImpl<
|
||||
in out A,
|
||||
in out I = A,
|
||||
in out R = never,
|
||||
in out TER = never,
|
||||
in out TEW = never,
|
||||
in out TRR = never,
|
||||
in out TRW = never,
|
||||
> extends Pipeable.Class() implements SynchronizedForm<A, I, R, TER, TEW, TRR, TRW> {
|
||||
readonly [Form.FormTypeId]: Form.FormTypeId = Form.FormTypeId
|
||||
readonly [SynchronizedFormTypeId]: SynchronizedFormTypeId = SynchronizedFormTypeId
|
||||
|
||||
readonly path = [] as const
|
||||
|
||||
constructor(
|
||||
readonly schema: Schema.Schema<A, I, R>,
|
||||
readonly context: Context.Context<Scope.Scope | R | TRR | TRW>,
|
||||
readonly target: Lens.Lens<A, TER, TEW, TRR, TRW>,
|
||||
|
||||
readonly value: Lens.Lens<Option.Option<A>, never, never, never, never>,
|
||||
readonly encodedValue: Lens.Lens<I, 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 isValidating: Subscribable.Subscribable<boolean, never, never>,
|
||||
|
||||
readonly canCommit: Subscribable.Subscribable<boolean, never, never>,
|
||||
readonly isCommitting: Lens.Lens<boolean, never, never>,
|
||||
|
||||
readonly runSemaphore: Effect.Semaphore,
|
||||
) {
|
||||
super()
|
||||
}
|
||||
|
||||
get run(): Effect.Effect<void, ParseResult.ParseError | TER | TEW> {
|
||||
return this.runSemaphore.withPermits(1)(Effect.provide(
|
||||
Effect.all([
|
||||
Stream.runForEach(
|
||||
this.encodedValue.changes,
|
||||
|
||||
encodedValue => Lens.get(this.validationFiber).pipe(
|
||||
Effect.andThen(Option.match({
|
||||
onSome: Fiber.interrupt,
|
||||
onNone: () => Effect.void,
|
||||
})),
|
||||
Effect.andThen(
|
||||
Effect.forkScoped(Effect.onExit(
|
||||
Schema.decode(this.schema, { errors: "all" })(encodedValue),
|
||||
exit => Effect.andThen(
|
||||
Exit.matchEffect(exit, {
|
||||
onSuccess: v => Lens.set(this.value, Option.some(v)).pipe(
|
||||
Effect.andThen(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()),
|
||||
),
|
||||
)).pipe(
|
||||
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.forkScoped,
|
||||
)
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
Stream.runForEach(
|
||||
Stream.drop(this.target.changes, 1),
|
||||
|
||||
targetValue => Schema.encode(this.schema, { errors: "all" })(targetValue).pipe(
|
||||
Effect.flatMap(encodedValue => Effect.whenEffect(
|
||||
Lens.set(this.encodedValue, encodedValue),
|
||||
Effect.map(
|
||||
Lens.get(this.encodedValue),
|
||||
currentEncodedValue => !Equal.equals(encodedValue, currentEncodedValue),
|
||||
),
|
||||
)),
|
||||
),
|
||||
),
|
||||
], { concurrency: "unbounded", discard: true }),
|
||||
|
||||
this.context,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export const isSynchronizedForm = (u: unknown): u is SynchronizedForm<unknown, unknown, unknown, unknown, unknown, unknown, unknown> => Predicate.hasProperty(u, SynchronizedFormTypeId)
|
||||
|
||||
export declare namespace makeSynchronized {
|
||||
export interface Options<in out A, in out I = A, in out R = never, in out TER = never, in out TEW = never, in out TRR = never, in out TRW = never> {
|
||||
readonly schema: Schema.Schema<A, I, R>
|
||||
readonly target: Lens.Lens<A, TER, TEW, TRR, TRW>
|
||||
readonly initialEncodedValue: NoInfer<I>
|
||||
}
|
||||
}
|
||||
|
||||
export const makeSynchronized = Effect.fnUntraced(function* <A, I = A, R = never, TER = never, TEW = never, TRR = never, TRW = never>(
|
||||
options: makeSynchronized.Options<A, I, R, TER, TEW, TRR, TRW>
|
||||
): Effect.fn.Return<
|
||||
SynchronizedForm<A, I, R, TER, TEW, TRR, TRW>,
|
||||
ParseResult.ParseError | TER,
|
||||
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 = yield* Lens.get(options.target).pipe(
|
||||
Effect.flatMap(Schema.encode(options.schema, { errors: "all" })),
|
||||
)
|
||||
|
||||
return new SynchronizedFormImpl(
|
||||
options.schema,
|
||||
yield* Effect.context<Scope.Scope | R | TRR | TRW>(),
|
||||
options.target,
|
||||
|
||||
valueLens,
|
||||
Lens.fromSubscriptionRef(yield* SubscriptionRef.make(initialEncodedValue)),
|
||||
issuesLens,
|
||||
validationFiberLens,
|
||||
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),
|
||||
)
|
||||
})
|
||||
|
||||
export declare namespace serviceSynchronized {
|
||||
export interface Options<in out A, in out I = A, in out R = never, in out TER = never, in out TEW = never, in out TRR = never, in out TRW = never>
|
||||
extends makeSynchronized.Options<A, I, R, TER, TEW, TRR, TRW> {}
|
||||
}
|
||||
|
||||
export const serviceSynchronized = <A, I = A, R = never, TER = never, TEW = never, TRR = never, TRW = never>(
|
||||
options: serviceSynchronized.Options<A, I, R, TER, TEW, TRR, TRW>
|
||||
): Effect.Effect<
|
||||
SynchronizedForm<A, I, R, TER, TEW, TRR, TRW>,
|
||||
ParseResult.ParseError | TER,
|
||||
Scope.Scope | R | TRR | TRW
|
||||
> => Effect.tap(
|
||||
makeSynchronized(options),
|
||||
form => Effect.forkScoped(form.run),
|
||||
)
|
||||
@@ -13,6 +13,8 @@ export * as ReactRuntime from "./ReactRuntime.js"
|
||||
export * as Result from "./Result.js"
|
||||
export * as SetStateAction from "./SetStateAction.js"
|
||||
export * as Stream from "./Stream.js"
|
||||
export * as SubmittableForm from "./SubmittableForm.js"
|
||||
export * as Subscribable from "./Subscribable.js"
|
||||
export * as SubscriptionRef from "./SubscriptionRef.js"
|
||||
export * as SubscriptionSubRef from "./SubscriptionSubRef.js"
|
||||
export * as SynchronizedForm from "./SynchronizedForm.js"
|
||||
|
||||
Reference in New Issue
Block a user