@@ -0,0 +1,212 @@
|
||||
import {
|
||||
Array,
|
||||
Cause,
|
||||
type Context,
|
||||
Effect,
|
||||
Fiber,
|
||||
Option,
|
||||
Pipeable,
|
||||
Predicate,
|
||||
Schema,
|
||||
SchemaIssue,
|
||||
SchemaParser,
|
||||
type Scope,
|
||||
Semaphore,
|
||||
SubscriptionRef,
|
||||
} 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"
|
||||
|
||||
|
||||
type FormSchema<A, I, R> = Schema.Top & {
|
||||
readonly Type: A
|
||||
readonly Encoded: I
|
||||
readonly DecodingServices: R
|
||||
}
|
||||
|
||||
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: FormSchema<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, SchemaIssue.Issue>>, never, never>
|
||||
readonly run: Effect.Effect<void>
|
||||
readonly submit: Effect.Effect<Option.Option<Result.Final<MA, ME, MP>>, Cause.NoSuchElementError>
|
||||
}
|
||||
|
||||
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
|
||||
readonly encodedValue: Lens.Lens<I, 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>
|
||||
|
||||
constructor(
|
||||
readonly schema: FormSchema<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 internalEncodedValue: Lens.Lens<I, never, never, never, never>,
|
||||
readonly issues: Lens.Lens<readonly Form.FormIssue[], never, never, never, never>,
|
||||
readonly validationFiber: Lens.Lens<Option.Option<Fiber.Fiber<A, SchemaIssue.Issue>>, never, never, never, never>,
|
||||
readonly runSemaphore: Semaphore.Semaphore,
|
||||
) {
|
||||
super()
|
||||
this.encodedValue = Lens.make({
|
||||
get: Lens.get(internalEncodedValue),
|
||||
changes: internalEncodedValue.changes,
|
||||
commit: encoded => Effect.andThen(
|
||||
Lens.set(internalEncodedValue, encoded),
|
||||
this.synchronizeEncodedValue(encoded),
|
||||
),
|
||||
lock: Lens.asLensImpl(internalEncodedValue).lock,
|
||||
})
|
||||
this.isValidating = Subscribable.map(validationFiber, Option.isSome)
|
||||
const commitState = Subscribable.zipLatestAll(
|
||||
value as any,
|
||||
issues as any,
|
||||
validationFiber as any,
|
||||
mutation.result as any,
|
||||
) as unknown as Subscribable.Subscribable<readonly [
|
||||
Option.Option<A>,
|
||||
readonly Form.FormIssue[],
|
||||
Option.Option<Fiber.Fiber<A, SchemaIssue.Issue>>,
|
||||
Result.Result<MA, ME, MP>,
|
||||
]>
|
||||
this.canCommit = Subscribable.map(
|
||||
commitState,
|
||||
([current, currentIssues, fiber, result]: readonly [Option.Option<A>, readonly Form.FormIssue[], Option.Option<Fiber.Fiber<A, SchemaIssue.Issue>>, Result.Result<MA, ME, MP>]) => Option.isSome(current)
|
||||
&& currentIssues.length === 0
|
||||
&& Option.isNone(fiber)
|
||||
&& !(Result.isRunning(result) || Result.hasRefreshingFlag(result)),
|
||||
)
|
||||
this.isCommitting = Subscribable.map(
|
||||
mutation.result,
|
||||
result => Result.isRunning(result) || Result.hasRefreshingFlag(result),
|
||||
)
|
||||
}
|
||||
|
||||
synchronizeEncodedValue(encodedValue: I): Effect.Effect<void> {
|
||||
const self = this
|
||||
return Effect.gen(function*() {
|
||||
const current = yield* Lens.get(self.validationFiber)
|
||||
if (Option.isSome(current)) yield* Fiber.interrupt(current.value)
|
||||
const fiber = yield* Effect.forkScoped(
|
||||
Effect.ensuring(
|
||||
SchemaParser.decodeEffect(self.schema)(encodedValue),
|
||||
Lens.set(self.validationFiber, Option.none()),
|
||||
),
|
||||
)
|
||||
yield* Lens.set(self.validationFiber, Option.some(fiber))
|
||||
const decoded = yield* Fiber.join(fiber).pipe(
|
||||
Effect.tap(value => Effect.andThen(
|
||||
Lens.set(self.issues, Array.empty()),
|
||||
Lens.set(self.value, Option.some(value)),
|
||||
)),
|
||||
Effect.catchIf(SchemaIssue.isIssue, issue => Lens.set(self.issues, formatIssue(issue))),
|
||||
)
|
||||
void decoded
|
||||
}).pipe(Effect.provide(this.context)) as Effect.Effect<void>
|
||||
}
|
||||
|
||||
get run(): Effect.Effect<void> {
|
||||
return Effect.flatMap(
|
||||
Lens.get(this.encodedValue),
|
||||
SchemaParser.decodeEffect(this.schema),
|
||||
).pipe(
|
||||
Effect.option,
|
||||
Effect.flatMap(value => Lens.set(this.value, value)),
|
||||
Effect.provide(this.context),
|
||||
this.runSemaphore.withPermits(1),
|
||||
)
|
||||
}
|
||||
|
||||
get submit(): Effect.Effect<Option.Option<Result.Final<MA, ME, MP>>, Cause.NoSuchElementError> {
|
||||
return Effect.flatMap(Lens.get(this.value), value => Effect.flatMap(Effect.fromOption(value), decoded => this.submitValue(decoded)))
|
||||
}
|
||||
|
||||
submitValue(value: A): Effect.Effect<Option.Option<Result.Final<MA, ME, MP>>> {
|
||||
return Effect.flatMap(this.canCommit.get, canCommit => {
|
||||
if (!canCommit) return Effect.succeed(Option.none())
|
||||
return Effect.map(
|
||||
Effect.tap(this.mutation.mutate([value, this as any]), result => {
|
||||
if (!Result.isFailure(result)) return Effect.succeed(undefined)
|
||||
const issue = Cause.findErrorOption(result.cause)
|
||||
return Option.isSome(issue) && SchemaIssue.isIssue(issue.value)
|
||||
? Lens.set(this.issues, formatIssue(issue.value))
|
||||
: Effect.succeed(undefined)
|
||||
}),
|
||||
Option.some,
|
||||
)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
const formatIssue = (issue: SchemaIssue.Issue): readonly Form.FormIssue[] => {
|
||||
const formatted = SchemaIssue.makeFormatterStandardSchemaV1()(issue)
|
||||
return formatted.issues.map(item => ({
|
||||
path: (item.path ?? []) as readonly PropertyKey[],
|
||||
message: item.message,
|
||||
}))
|
||||
}
|
||||
|
||||
export const isSubmittableForm = (u: unknown): u is SubmittableForm<unknown, unknown, unknown, unknown, unknown, unknown, unknown> => Predicate.hasProperty(u, SubmittableFormTypeId)
|
||||
|
||||
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: SubmittableForm<NoInfer<A>, NoInfer<I>, NoInfer<R>, unknown, unknown, unknown>],
|
||||
MA, ME, MR, MP
|
||||
> {
|
||||
readonly schema: FormSchema<A, I, R>
|
||||
readonly initialEncodedValue: NoInfer<I>
|
||||
}
|
||||
}
|
||||
|
||||
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<
|
||||
SubmittableForm<A, I, R, MA, ME, Result.forkEffect.OutputContext<MR, MP>, MP>,
|
||||
never,
|
||||
Scope.Scope | R | Result.forkEffect.OutputContext<MR, MP>
|
||||
> {
|
||||
return new SubmittableFormImpl(
|
||||
options.schema,
|
||||
yield* Effect.context<Scope.Scope | R>(),
|
||||
yield* Mutation.make(options),
|
||||
Lens.fromSubscriptionRef(yield* SubscriptionRef.make(Option.none<A>())),
|
||||
Lens.fromSubscriptionRef(yield* SubscriptionRef.make(options.initialEncodedValue)),
|
||||
Lens.fromSubscriptionRef(yield* SubscriptionRef.make<readonly Form.FormIssue[]>(Array.empty())),
|
||||
Lens.fromSubscriptionRef(yield* SubscriptionRef.make(Option.none<Fiber.Fiber<A, SchemaIssue.Issue>>())),
|
||||
yield* Semaphore.make(1),
|
||||
)
|
||||
})
|
||||
|
||||
export declare namespace service {
|
||||
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 make.Options<A, I, R, MA, ME, MR, MP> {}
|
||||
}
|
||||
|
||||
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<
|
||||
SubmittableForm<A, I, R, MA, ME, Result.forkEffect.OutputContext<MR, MP>, MP>,
|
||||
never,
|
||||
Scope.Scope | R | Result.forkEffect.OutputContext<MR, MP>
|
||||
> => Effect.tap(make(options), form => Effect.asVoid(Effect.forkScoped(form.run)))
|
||||
Reference in New Issue
Block a user