|
|
|
|
@@ -1,9 +1,8 @@
|
|
|
|
|
import { Array, Cause, Chunk, type Context, type Duration, Effect, Equal, Exit, Fiber, flow, Hash, HashMap, identity, Option, ParseResult, Pipeable, Predicate, Ref, Schema, type Scope, Stream } from "effect"
|
|
|
|
|
import { Cause, Chunk, type Context, type Duration, Effect, Equal, Exit, Fiber, identity, Option, type ParseResult, Pipeable, Predicate, Schema, 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 PropertyPath from "./PropertyPath.js"
|
|
|
|
|
import * as Result from "./Result.js"
|
|
|
|
|
import * as Subscribable from "./Subscribable.js"
|
|
|
|
|
import * as SubscriptionRef from "./SubscriptionRef.js"
|
|
|
|
|
@@ -12,92 +11,90 @@ import * as SubscriptionRef from "./SubscriptionRef.js"
|
|
|
|
|
export const FormTypeId: unique symbol = Symbol.for("@effect-fc/Form/Form")
|
|
|
|
|
export type FormTypeId = typeof FormTypeId
|
|
|
|
|
|
|
|
|
|
export interface Form<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>
|
|
|
|
|
export interface Form<in out P extends readonly PropertyKey[], in out A, in out I = A>
|
|
|
|
|
extends Pipeable.Pipeable {
|
|
|
|
|
readonly [FormTypeId]: FormTypeId
|
|
|
|
|
|
|
|
|
|
readonly schema: Schema.Schema<A, I, R>
|
|
|
|
|
readonly context: Context.Context<Scope.Scope | R>
|
|
|
|
|
readonly mutation: Mutation.Mutation<
|
|
|
|
|
readonly [value: A, form: Form<A, I, R, unknown, unknown, unknown>],
|
|
|
|
|
MA, ME, MR, MP
|
|
|
|
|
>
|
|
|
|
|
readonly autosubmit: boolean
|
|
|
|
|
readonly debounce: Option.Option<Duration.DurationInput>
|
|
|
|
|
|
|
|
|
|
readonly path: P
|
|
|
|
|
readonly value: Subscribable.Subscribable<Option.Option<A>>
|
|
|
|
|
readonly encodedValue: Lens.Lens<I>
|
|
|
|
|
readonly error: Subscribable.Subscribable<Option.Option<ParseResult.ParseError>>
|
|
|
|
|
readonly validationFiber: Subscribable.Subscribable<Option.Option<Fiber.Fiber<A, ParseResult.ParseError>>>
|
|
|
|
|
|
|
|
|
|
readonly canSubmit: Subscribable.Subscribable<boolean>
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
field<const P extends PropertyPath.Paths<I>>(
|
|
|
|
|
path: P
|
|
|
|
|
): Effect.Effect<FormField<PropertyPath.ValueFromPath<A, P>, PropertyPath.ValueFromPath<I, P>>>
|
|
|
|
|
export const FormImplTypeId: unique symbol = Symbol.for("@effect-fc/Form/FormImpl")
|
|
|
|
|
export type FormImplTypeId = typeof FormImplTypeId
|
|
|
|
|
|
|
|
|
|
export class FormImpl<in out P extends readonly PropertyKey[], in out A, in out I = A>
|
|
|
|
|
extends Pipeable.Class() implements Form<P, A, I> {
|
|
|
|
|
readonly [FormTypeId]: FormTypeId = FormTypeId
|
|
|
|
|
readonly [FormImplTypeId]: FormImplTypeId = FormImplTypeId
|
|
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
|
readonly path: P,
|
|
|
|
|
readonly value: Lens.Lens<Option.Option<A>>,
|
|
|
|
|
readonly encodedValue: Lens.Lens<I>,
|
|
|
|
|
readonly error: Lens.Lens<Option.Option<ParseResult.ParseError>>,
|
|
|
|
|
readonly validationFiber: Lens.Lens<Option.Option<Fiber.Fiber<A, ParseResult.ParseError>>>,
|
|
|
|
|
readonly canSubmit: Subscribable.Subscribable<boolean>,
|
|
|
|
|
) {
|
|
|
|
|
super()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const RootFormTypeId: unique symbol = Symbol.for("@effect-fc/Form/RootForm")
|
|
|
|
|
export type RootFormTypeId = typeof RootFormTypeId
|
|
|
|
|
|
|
|
|
|
export interface RootForm<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> {
|
|
|
|
|
readonly schema: Schema.Schema<A, I, R>
|
|
|
|
|
readonly context: Context.Context<Scope.Scope | R>
|
|
|
|
|
readonly mutation: Mutation.Mutation<
|
|
|
|
|
readonly [value: A, form: RootForm<A, I, R, unknown, unknown, unknown>],
|
|
|
|
|
MA, ME, MR, MP
|
|
|
|
|
>
|
|
|
|
|
readonly autosubmit: boolean
|
|
|
|
|
|
|
|
|
|
readonly run: Effect.Effect<void>
|
|
|
|
|
readonly submit: Effect.Effect<Option.Option<Result.Final<MA, ME, MP>>, Cause.NoSuchElementException>
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class FormImpl<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 Form<A, I, R, MA, ME, MR, MP> {
|
|
|
|
|
readonly [FormTypeId]: FormTypeId = FormTypeId
|
|
|
|
|
export class RootFormImpl<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 FormImpl<readonly [], A, I> implements RootForm<A, I, R, MA, ME, MR, MP> {
|
|
|
|
|
readonly [RootFormTypeId]: RootFormTypeId = RootFormTypeId
|
|
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
|
readonly schema: Schema.Schema<A, I, R>,
|
|
|
|
|
readonly context: Context.Context<Scope.Scope | R>,
|
|
|
|
|
readonly mutation: Mutation.Mutation<
|
|
|
|
|
readonly [value: A, form: Form<A, I, R, unknown, unknown, unknown>],
|
|
|
|
|
readonly [value: A, form: RootForm<A, I, R, unknown, unknown, unknown>],
|
|
|
|
|
MA, ME, MR, MP
|
|
|
|
|
>,
|
|
|
|
|
readonly autosubmit: boolean,
|
|
|
|
|
readonly debounce: Option.Option<Duration.DurationInput>,
|
|
|
|
|
|
|
|
|
|
readonly value: Lens.Lens<Option.Option<A>>,
|
|
|
|
|
readonly encodedValue: Lens.Lens<I>,
|
|
|
|
|
readonly error: Lens.Lens<Option.Option<ParseResult.ParseError>>,
|
|
|
|
|
readonly validationFiber: Lens.Lens<Option.Option<Fiber.Fiber<A, ParseResult.ParseError>>>,
|
|
|
|
|
value: Lens.Lens<Option.Option<A>>,
|
|
|
|
|
encodedValue: Lens.Lens<I>,
|
|
|
|
|
error: Lens.Lens<Option.Option<ParseResult.ParseError>>,
|
|
|
|
|
validationFiber: Lens.Lens<Option.Option<Fiber.Fiber<A, ParseResult.ParseError>>>,
|
|
|
|
|
canSubmit: Subscribable.Subscribable<boolean>,
|
|
|
|
|
|
|
|
|
|
readonly runSemaphore: Effect.Semaphore,
|
|
|
|
|
readonly fieldCache: Ref.Ref<HashMap.HashMap<FormFieldKey, FormField<unknown, unknown>>>,
|
|
|
|
|
) {
|
|
|
|
|
super()
|
|
|
|
|
|
|
|
|
|
this.canSubmit = Subscribable.map(
|
|
|
|
|
Subscribable.zipLatestAll(this.value, this.error, this.validationFiber, this.mutation.result),
|
|
|
|
|
([value, error, validationFiber, result]) => (
|
|
|
|
|
Option.isSome(value) &&
|
|
|
|
|
Option.isNone(error) &&
|
|
|
|
|
Option.isNone(validationFiber) &&
|
|
|
|
|
!(Result.isRunning(result) || Result.hasRefreshingFlag(result))
|
|
|
|
|
),
|
|
|
|
|
super(
|
|
|
|
|
[],
|
|
|
|
|
value,
|
|
|
|
|
encodedValue,
|
|
|
|
|
error,
|
|
|
|
|
validationFiber,
|
|
|
|
|
canSubmit,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
field<const P extends PropertyPath.Paths<I>>(
|
|
|
|
|
path: P
|
|
|
|
|
): Effect.Effect<FormField<PropertyPath.ValueFromPath<A, P>, PropertyPath.ValueFromPath<I, P>>> {
|
|
|
|
|
const key = new FormFieldKey(path)
|
|
|
|
|
return this.fieldCache.pipe(
|
|
|
|
|
Effect.map(HashMap.get(key)),
|
|
|
|
|
Effect.flatMap(Option.match({
|
|
|
|
|
onSome: v => Effect.succeed(v as FormField<PropertyPath.ValueFromPath<A, P>, PropertyPath.ValueFromPath<I, P>>),
|
|
|
|
|
onNone: () => Effect.tap(
|
|
|
|
|
Effect.succeed(makeFormField(this as Form<A, I, R, MA, ME, MR, MP>, path)),
|
|
|
|
|
v => Ref.update(this.fieldCache, HashMap.set(key, v as FormField<unknown, unknown>)),
|
|
|
|
|
),
|
|
|
|
|
})),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
readonly canSubmit: Subscribable.Subscribable<boolean>
|
|
|
|
|
|
|
|
|
|
get run(): Effect.Effect<void> {
|
|
|
|
|
return this.runSemaphore.withPermits(1)(Stream.runForEach(
|
|
|
|
|
this.encodedValue.changes.pipe(
|
|
|
|
|
Option.isSome(this.debounce) ? Stream.debounce(this.debounce.value) : identity
|
|
|
|
|
),
|
|
|
|
|
this.encodedValue.changes,
|
|
|
|
|
|
|
|
|
|
encodedValue => Lens.get(this.validationFiber).pipe(
|
|
|
|
|
Effect.andThen(Option.match({
|
|
|
|
|
@@ -164,12 +161,16 @@ extends Pipeable.Class() implements Form<A, I, R, MA, ME, MR, MP> {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const isForm = (u: unknown): u is Form<unknown, unknown, unknown, unknown, unknown, unknown> => Predicate.hasProperty(u, FormTypeId)
|
|
|
|
|
|
|
|
|
|
export const isForm = (u: unknown): u is Form<readonly PropertyKey[], unknown, unknown> => Predicate.hasProperty(u, FormTypeId)
|
|
|
|
|
export const isFormImpl = (u: unknown): u is FormImpl<readonly PropertyKey[], unknown, unknown> => Predicate.hasProperty(u, FormImplTypeId)
|
|
|
|
|
export const isRootForm = (u: unknown): u is RootForm<readonly PropertyKey[], unknown, unknown, unknown, unknown, unknown, unknown> => Predicate.hasProperty(u, RootFormTypeId)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export declare namespace make {
|
|
|
|
|
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: Form<NoInfer<A>, NoInfer<I>, NoInfer<R>, unknown, unknown, unknown>],
|
|
|
|
|
readonly [value: NoInfer<A>, form: RootForm<NoInfer<A>, NoInfer<I>, NoInfer<R>, unknown, unknown, unknown>],
|
|
|
|
|
MA, ME, MR, MP
|
|
|
|
|
> {
|
|
|
|
|
readonly schema: Schema.Schema<A, I, R>
|
|
|
|
|
@@ -182,24 +183,36 @@ export declare namespace make {
|
|
|
|
|
export const make = Effect.fnUntraced(function* <A, I = A, R = never, MA = void, ME = never, MR = never, MP = never>(
|
|
|
|
|
options: make.Options<A, I, R, MA, ME, MR, MP>
|
|
|
|
|
): Effect.fn.Return<
|
|
|
|
|
Form<A, I, R, MA, ME, Result.forkEffect.OutputContext<MA, ME, MR, MP>, MP>,
|
|
|
|
|
RootForm<A, I, R, MA, ME, Result.forkEffect.OutputContext<MA, ME, MR, MP>, MP>,
|
|
|
|
|
never,
|
|
|
|
|
Scope.Scope | R | Result.forkEffect.OutputContext<MA, ME, MR, MP>
|
|
|
|
|
> {
|
|
|
|
|
return new FormImpl(
|
|
|
|
|
const mutation = yield* Mutation.make(options)
|
|
|
|
|
const valueLens = Lens.fromSubscriptionRef(yield* SubscriptionRef.make(Option.none<A>()))
|
|
|
|
|
const errorLens = Lens.fromSubscriptionRef(yield* SubscriptionRef.make(Option.none<ParseResult.ParseError>()))
|
|
|
|
|
const validationFiberLens = Lens.fromSubscriptionRef(yield* SubscriptionRef.make(Option.none<Fiber.Fiber<A, ParseResult.ParseError>>()))
|
|
|
|
|
|
|
|
|
|
return new RootFormImpl(
|
|
|
|
|
options.schema,
|
|
|
|
|
yield* Effect.context<Scope.Scope | R>(),
|
|
|
|
|
yield* Mutation.make(options),
|
|
|
|
|
mutation,
|
|
|
|
|
options.autosubmit ?? false,
|
|
|
|
|
Option.fromNullable(options.debounce),
|
|
|
|
|
|
|
|
|
|
Lens.fromSubscriptionRef(yield* SubscriptionRef.make(Option.none<A>())),
|
|
|
|
|
valueLens,
|
|
|
|
|
Lens.fromSubscriptionRef(yield* SubscriptionRef.make(options.initialEncodedValue)),
|
|
|
|
|
Lens.fromSubscriptionRef(yield* SubscriptionRef.make(Option.none<ParseResult.ParseError>())),
|
|
|
|
|
Lens.fromSubscriptionRef(yield* SubscriptionRef.make(Option.none<Fiber.Fiber<A, ParseResult.ParseError>>())),
|
|
|
|
|
errorLens,
|
|
|
|
|
validationFiberLens,
|
|
|
|
|
Subscribable.map(
|
|
|
|
|
Subscribable.zipLatestAll(valueLens, errorLens, validationFiberLens, mutation.result),
|
|
|
|
|
([value, error, validationFiber, result]) => (
|
|
|
|
|
Option.isSome(value) &&
|
|
|
|
|
Option.isNone(error) &&
|
|
|
|
|
Option.isNone(validationFiber) &&
|
|
|
|
|
!(Result.isRunning(result) || Result.hasRefreshingFlag(result))
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
|
|
|
|
|
yield* Effect.makeSemaphore(1),
|
|
|
|
|
yield* Ref.make(HashMap.empty<FormFieldKey, FormField<unknown, unknown>>()),
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
@@ -211,7 +224,7 @@ export declare namespace service {
|
|
|
|
|
export const service = <A, I = A, R = never, MA = void, ME = never, MR = never, MP = never>(
|
|
|
|
|
options: service.Options<A, I, R, MA, ME, MR, MP>
|
|
|
|
|
): Effect.Effect<
|
|
|
|
|
Form<A, I, R, MA, ME, Result.forkEffect.OutputContext<MA, ME, MR, MP>, MP>,
|
|
|
|
|
RootForm<A, I, R, MA, ME, Result.forkEffect.OutputContext<MA, ME, MR, MP>, MP>,
|
|
|
|
|
never,
|
|
|
|
|
Scope.Scope | R | Result.forkEffect.OutputContext<MA, ME, MR, MP>
|
|
|
|
|
> => Effect.tap(
|
|
|
|
|
@@ -220,74 +233,6 @@ export const service = <A, I = A, R = never, MA = void, ME = never, MR = never,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const FormFieldTypeId: unique symbol = Symbol.for("@effect-fc/Form/FormField")
|
|
|
|
|
export type FormFieldTypeId = typeof FormFieldTypeId
|
|
|
|
|
|
|
|
|
|
export interface FormField<in out A, in out I = A>
|
|
|
|
|
extends Pipeable.Pipeable {
|
|
|
|
|
readonly [FormFieldTypeId]: FormFieldTypeId
|
|
|
|
|
|
|
|
|
|
readonly value: Subscribable.Subscribable<Option.Option<A>, Cause.NoSuchElementException>
|
|
|
|
|
readonly encodedValue: Lens.Lens<I>
|
|
|
|
|
readonly issues: Subscribable.Subscribable<readonly ParseResult.ArrayFormatterIssue[]>
|
|
|
|
|
readonly isValidating: Subscribable.Subscribable<boolean>
|
|
|
|
|
readonly isSubmitting: Subscribable.Subscribable<boolean>
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class FormFieldImpl<in out A, in out I = A>
|
|
|
|
|
extends Pipeable.Class() implements FormField<A, I> {
|
|
|
|
|
readonly [FormFieldTypeId]: FormFieldTypeId = FormFieldTypeId
|
|
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
|
readonly value: Subscribable.Subscribable<Option.Option<A>, Cause.NoSuchElementException>,
|
|
|
|
|
readonly encodedValue: Lens.Lens<I>,
|
|
|
|
|
readonly issues: Subscribable.Subscribable<readonly ParseResult.ArrayFormatterIssue[]>,
|
|
|
|
|
readonly isValidating: Subscribable.Subscribable<boolean>,
|
|
|
|
|
readonly isSubmitting: Subscribable.Subscribable<boolean>,
|
|
|
|
|
) {
|
|
|
|
|
super()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const FormFieldKeyTypeId: unique symbol = Symbol.for("@effect-fc/Form/FormFieldKey")
|
|
|
|
|
type FormFieldKeyTypeId = typeof FormFieldKeyTypeId
|
|
|
|
|
|
|
|
|
|
class FormFieldKey implements Equal.Equal {
|
|
|
|
|
readonly [FormFieldKeyTypeId]: FormFieldKeyTypeId = FormFieldKeyTypeId
|
|
|
|
|
constructor(readonly path: PropertyPath.PropertyPath) {}
|
|
|
|
|
|
|
|
|
|
[Equal.symbol](that: Equal.Equal) {
|
|
|
|
|
return isFormFieldKey(that) && PropertyPath.equivalence(this.path, that.path)
|
|
|
|
|
}
|
|
|
|
|
[Hash.symbol]() {
|
|
|
|
|
return Hash.array(this.path)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const isFormField = (u: unknown): u is FormField<unknown, unknown> => Predicate.hasProperty(u, FormFieldTypeId)
|
|
|
|
|
const isFormFieldKey = (u: unknown): u is FormFieldKey => Predicate.hasProperty(u, FormFieldKeyTypeId)
|
|
|
|
|
|
|
|
|
|
export const makeFormField = <A, I, R, MA, ME, MR, MP, const P extends PropertyPath.Paths<NoInfer<I>>>(
|
|
|
|
|
self: Form<A, I, R, MA, ME, MR, MP>,
|
|
|
|
|
path: P,
|
|
|
|
|
): FormField<PropertyPath.ValueFromPath<A, P>, PropertyPath.ValueFromPath<I, P>> => {
|
|
|
|
|
return new FormFieldImpl(
|
|
|
|
|
Subscribable.mapEffect(self.value, Option.match({
|
|
|
|
|
onSome: v => Option.map(PropertyPath.get(v, path), Option.some),
|
|
|
|
|
onNone: () => Option.some(Option.none()),
|
|
|
|
|
})),
|
|
|
|
|
Lens.map(self.encodedValue, a => Option.getOrThrow(PropertyPath.get(a, path)), (a, b) => Option.getOrThrow(PropertyPath.immutableSet(a, path, b))),
|
|
|
|
|
Subscribable.mapEffect(self.error, Option.match({
|
|
|
|
|
onSome: flow(
|
|
|
|
|
ParseResult.ArrayFormatter.formatError,
|
|
|
|
|
Effect.map(Array.filter(issue => PropertyPath.equivalence(issue.path, path))),
|
|
|
|
|
),
|
|
|
|
|
onNone: () => Effect.succeed([]),
|
|
|
|
|
})),
|
|
|
|
|
Subscribable.map(self.validationFiber, Option.isSome),
|
|
|
|
|
Subscribable.map(self.mutation.result, result => Result.isRunning(result) || Result.hasRefreshingFlag(result)),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export namespace useInput {
|
|
|
|
|
|