This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import { Array, Cause, Chunk, type Context, Effect, Exit, Fiber, identity, Option, ParseResult, Pipeable, Predicate, Schema, type Scope, Stream, SubscriptionRef } from "effect"
|
import { Array, Cause, Chunk, type Context, Effect, Fiber, flow, identity, 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 Mutation from "./Mutation.js"
|
import * as Mutation from "./Mutation.js"
|
||||||
@@ -21,7 +21,6 @@ extends Form.Form<readonly [], A, I, never, never> {
|
|||||||
>
|
>
|
||||||
readonly validationFiber: Subscribable.Subscribable<Option.Option<Fiber.Fiber<A, ParseResult.ParseError>>, never, never>
|
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>
|
readonly submit: Effect.Effect<Option.Option<Result.Final<MA, ME, MP>>, Cause.NoSuchElementException>
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -32,6 +31,11 @@ extends Pipeable.Class() implements SubmittableForm<A, I, R, MA, ME, MR, MP> {
|
|||||||
|
|
||||||
readonly path = [] as const
|
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(
|
constructor(
|
||||||
readonly schema: Schema.Schema<A, I, R>,
|
readonly schema: Schema.Schema<A, I, R>,
|
||||||
readonly context: Context.Context<Scope.Scope | R>,
|
readonly context: Context.Context<Scope.Scope | R>,
|
||||||
@@ -40,68 +44,99 @@ extends Pipeable.Class() implements SubmittableForm<A, I, R, MA, ME, MR, MP> {
|
|||||||
MA, ME, MR, MP
|
MA, ME, MR, MP
|
||||||
>,
|
>,
|
||||||
readonly value: Lens.Lens<Option.Option<A>, never, never, never, never>,
|
readonly value: Lens.Lens<Option.Option<A>, never, never, never, never>,
|
||||||
readonly encodedValue: 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: Subscribable.Subscribable<boolean, never, never>,
|
|
||||||
|
|
||||||
readonly runSemaphore: Effect.Semaphore,
|
readonly runSemaphore: Effect.Semaphore,
|
||||||
) {
|
) {
|
||||||
super()
|
super()
|
||||||
|
|
||||||
|
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.value, self.issues, self.validationFiber, self.mutation.result),
|
||||||
|
([value, issues, validationFiber, result]) => (
|
||||||
|
Option.isSome(value) &&
|
||||||
|
Array.isEmptyReadonlyArray(issues) &&
|
||||||
|
Option.isNone(validationFiber) &&
|
||||||
|
!(Result.isRunning(result) || Result.hasRefreshingFlag(result))
|
||||||
|
),
|
||||||
|
)),
|
||||||
|
Subscribable.unwrap,
|
||||||
|
)
|
||||||
|
this.isCommitting = Effect.succeed(this).pipe(
|
||||||
|
Effect.map(self => Subscribable.map(
|
||||||
|
self.mutation.result,
|
||||||
|
result => Result.isRunning(result) || Result.hasRefreshingFlag(result),
|
||||||
|
)),
|
||||||
|
Subscribable.unwrap,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
get run(): Effect.Effect<void> {
|
synchronizeEncodedValue(encodedValue: I): Effect.Effect<void, never, never> {
|
||||||
return this.runSemaphore.withPermits(1)(Effect.provide(
|
return Lens.get(this.validationFiber).pipe(
|
||||||
Stream.runForEach(
|
|
||||||
this.encodedValue.changes,
|
|
||||||
|
|
||||||
encodedValue => 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.andThen(Effect.forkScoped(
|
||||||
Effect.forkScoped(Effect.onExit(
|
Effect.ensuring(
|
||||||
Schema.decode(this.schema, { errors: "all" })(encodedValue),
|
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()),
|
Lens.set(this.validationFiber, Option.none()),
|
||||||
),
|
)
|
||||||
))
|
)),
|
||||||
),
|
|
||||||
Effect.tap(fiber => Lens.set(this.validationFiber, Option.some(fiber))),
|
Effect.tap(fiber => Lens.set(this.validationFiber, Option.some(fiber))),
|
||||||
Effect.andThen(Fiber.join),
|
Effect.flatMap(Fiber.join),
|
||||||
Effect.ignore,
|
|
||||||
|
Effect.flatMap(value => Lens.set(this.value, Option.some(value))),
|
||||||
|
Effect.catchIf(
|
||||||
|
ParseResult.isParseError,
|
||||||
|
flow(
|
||||||
|
ParseResult.ArrayFormatter.formatError,
|
||||||
|
Effect.flatMap(v => Lens.set(this.issues, v)),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
|
||||||
this.context,
|
Effect.provide(this.context),
|
||||||
))
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
get submit(): Effect.Effect<Option.Option<Result.Final<MA, ME, MP>>, Cause.NoSuchElementException> {
|
get submit(): Effect.Effect<Option.Option<Result.Final<MA, ME, MP>>, Cause.NoSuchElementException, never> {
|
||||||
return Lens.get(this.value).pipe(
|
return Lens.get(this.value).pipe(
|
||||||
Effect.andThen(identity),
|
Effect.andThen(identity),
|
||||||
Effect.andThen(value => this.submitValue(value)),
|
Effect.andThen(value => this.submitValue(value)),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
submitValue(value: A): Effect.Effect<Option.Option<Result.Final<MA, ME, MP>>> {
|
submitValue(value: A): Effect.Effect<Option.Option<Result.Final<MA, ME, MP>>, never, never> {
|
||||||
return Effect.whenEffect(
|
return Effect.whenEffect(
|
||||||
Effect.tap(
|
Effect.tap(
|
||||||
this.mutation.mutate([value, this as any]),
|
this.mutation.mutate([value, this as any]),
|
||||||
@@ -148,49 +183,16 @@ export const make = Effect.fnUntraced(function* <A, I = A, R = never, MA = void,
|
|||||||
never,
|
never,
|
||||||
Scope.Scope | R | Result.forkEffect.OutputContext<MR, MP>
|
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(
|
return new SubmittableFormImpl(
|
||||||
options.schema,
|
options.schema,
|
||||||
yield* Effect.context<Scope.Scope | R>(),
|
yield* Effect.context<Scope.Scope | R>(),
|
||||||
mutation,
|
yield* Mutation.make(options),
|
||||||
|
|
||||||
valueLens,
|
Lens.fromSubscriptionRef(yield* SubscriptionRef.make(Option.none<A>())),
|
||||||
Lens.fromSubscriptionRef(yield* SubscriptionRef.make(options.initialEncodedValue)),
|
Lens.fromSubscriptionRef(yield* SubscriptionRef.make(options.initialEncodedValue)),
|
||||||
issuesLens,
|
Lens.fromSubscriptionRef(yield* SubscriptionRef.make<readonly ParseResult.ArrayFormatterIssue[]>(Array.empty())),
|
||||||
validationFiberLens,
|
Lens.fromSubscriptionRef(yield* SubscriptionRef.make(Option.none<Fiber.Fiber<A, ParseResult.ParseError>>())),
|
||||||
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),
|
yield* Effect.makeSemaphore(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.forkScoped(form.run),
|
|
||||||
)
|
|
||||||
|
|||||||
Reference in New Issue
Block a user