0.2.0 (#18)
Co-authored-by: Renovate Bot <renovate-bot@valverde.cloud> Co-authored-by: Julien Valverdé <julien.valverde@mailo.com> Reviewed-on: Thilawyn/effect-fc#18
This commit was merged in pull request #18.
This commit is contained in:
@@ -1,14 +1,15 @@
|
||||
import * as AsyncData from "@typed/async-data"
|
||||
import { Array, Cause, Chunk, type Duration, Effect, Equal, Exit, Fiber, flow, identity, Option, ParseResult, Pipeable, Predicate, Ref, Schema, type Scope, Stream, SubscriptionRef } from "effect"
|
||||
import { Array, Cause, Chunk, type Duration, Effect, Equal, Exit, Fiber, flow, identity, Option, ParseResult, Pipeable, Predicate, Ref, Schema, type Scope, Stream } from "effect"
|
||||
import type { NoSuchElementException } from "effect/Cause"
|
||||
import * as React from "react"
|
||||
import * as Hooks from "./Hooks/index.js"
|
||||
import * as Component from "./Component.js"
|
||||
import * as PropertyPath from "./PropertyPath.js"
|
||||
import * as Subscribable from "./Subscribable.js"
|
||||
import * as SubscriptionRef from "./SubscriptionRef.js"
|
||||
import * as SubscriptionSubRef from "./SubscriptionSubRef.js"
|
||||
|
||||
|
||||
export const FormTypeId: unique symbol = Symbol.for("effect-fc/Form")
|
||||
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, out R = never, in out SA = void, in out SE = A, out SR = never>
|
||||
@@ -17,6 +18,7 @@ extends Pipeable.Pipeable {
|
||||
|
||||
readonly schema: Schema.Schema<A, I, R>
|
||||
readonly onSubmit: (value: NoInfer<A>) => Effect.Effect<SA, SE, SR>
|
||||
readonly autosubmit: boolean
|
||||
readonly debounce: Option.Option<Duration.DurationInput>
|
||||
|
||||
readonly valueRef: SubscriptionRef.SubscriptionRef<Option.Option<A>>
|
||||
@@ -35,6 +37,7 @@ extends Pipeable.Class() implements Form<A, I, R, SA, SE, SR> {
|
||||
constructor(
|
||||
readonly schema: Schema.Schema<A, I, R>,
|
||||
readonly onSubmit: (value: NoInfer<A>) => Effect.Effect<SA, SE, SR>,
|
||||
readonly autosubmit: boolean,
|
||||
readonly debounce: Option.Option<Duration.DurationInput>,
|
||||
|
||||
readonly valueRef: SubscriptionRef.SubscriptionRef<Option.Option<A>>,
|
||||
@@ -52,11 +55,15 @@ extends Pipeable.Class() implements Form<A, I, R, SA, SE, SR> {
|
||||
export const isForm = (u: unknown): u is Form<unknown, unknown, unknown, unknown, unknown, unknown> => Predicate.hasProperty(u, FormTypeId)
|
||||
|
||||
export namespace make {
|
||||
export interface Options<in out A, in out I, out R, in out SA = void, in out SE = A, out SR = never> {
|
||||
export interface Options<in out A, in out I, in out R, in out SA = void, in out SE = A, out SR = never> {
|
||||
readonly schema: Schema.Schema<A, I, R>
|
||||
readonly initialEncodedValue: NoInfer<I>
|
||||
readonly onSubmit: (value: NoInfer<A>) => Effect.Effect<SA, SE, SR>,
|
||||
readonly debounce?: Duration.DurationInput,
|
||||
readonly onSubmit: (
|
||||
this: Form<NoInfer<A>, NoInfer<I>, NoInfer<R>, unknown, unknown, unknown>,
|
||||
value: NoInfer<A>,
|
||||
) => Effect.Effect<SA, SE, SR>
|
||||
readonly autosubmit?: boolean
|
||||
readonly debounce?: Duration.DurationInput
|
||||
}
|
||||
}
|
||||
|
||||
@@ -75,6 +82,7 @@ export const make: {
|
||||
return new FormImpl(
|
||||
options.schema,
|
||||
options.onSubmit,
|
||||
options.autosubmit ?? false,
|
||||
Option.fromNullable(options.debounce),
|
||||
|
||||
valueRef,
|
||||
@@ -97,7 +105,7 @@ export const make: {
|
||||
|
||||
export const run = <A, I, R, SA, SE, SR>(
|
||||
self: Form<A, I, R, SA, SE, SR>
|
||||
): Effect.Effect<void, never, Scope.Scope | R> => Stream.runForEach(
|
||||
): Effect.Effect<void, never, Scope.Scope | R | SR> => Stream.runForEach(
|
||||
self.encodedValueRef.changes.pipe(
|
||||
Option.isSome(self.debounce) ? Stream.debounce(self.debounce.value) : identity
|
||||
),
|
||||
@@ -108,30 +116,35 @@ export const run = <A, I, R, SA, SE, SR>(
|
||||
onNone: () => Effect.void,
|
||||
})),
|
||||
Effect.andThen(
|
||||
Effect.addFinalizer(() => SubscriptionRef.set(self.validationFiberRef, Option.none())).pipe(
|
||||
Effect.addFinalizer(() => Ref.set(self.validationFiberRef, Option.none())).pipe(
|
||||
Effect.andThen(Schema.decode(self.schema, { errors: "all" })(encodedValue)),
|
||||
Effect.exit,
|
||||
Effect.andThen(flow(
|
||||
Exit.matchEffect({
|
||||
onSuccess: v => Effect.andThen(
|
||||
SubscriptionRef.set(self.valueRef, Option.some(v)),
|
||||
SubscriptionRef.set(self.errorRef, Option.none()),
|
||||
onSuccess: v => Ref.set(self.valueRef, Option.some(v)).pipe(
|
||||
Effect.andThen(Ref.set(self.errorRef, Option.none())),
|
||||
Effect.as(Option.some(v)),
|
||||
),
|
||||
onFailure: c => Option.match(
|
||||
Chunk.findFirst(Cause.failures(c), e => e._tag === "ParseError"),
|
||||
{
|
||||
onSome: e => SubscriptionRef.set(self.errorRef, Option.some(e)),
|
||||
onFailure: c => Chunk.findFirst(Cause.failures(c), e => e._tag === "ParseError").pipe(
|
||||
Option.match({
|
||||
onSome: e => Ref.set(self.errorRef, Option.some(e)),
|
||||
onNone: () => Effect.void,
|
||||
},
|
||||
}),
|
||||
Effect.as(Option.none<A>()),
|
||||
),
|
||||
}),
|
||||
Effect.uninterruptible,
|
||||
)),
|
||||
Effect.scoped,
|
||||
|
||||
Effect.andThen(value => Option.isSome(value) && self.autosubmit
|
||||
? Effect.asVoid(Effect.forkScoped(submit(self)))
|
||||
: Effect.void
|
||||
),
|
||||
Effect.forkScoped,
|
||||
)
|
||||
),
|
||||
Effect.andThen(fiber => SubscriptionRef.set(self.validationFiberRef, Option.some(fiber)))
|
||||
Effect.andThen(fiber => Ref.set(self.validationFiberRef, Option.some(fiber)))
|
||||
),
|
||||
)
|
||||
|
||||
@@ -157,13 +170,13 @@ export const submit = <A, I, R, SA, SE, SR>(
|
||||
)
|
||||
|
||||
export namespace service {
|
||||
export interface Options<in out A, in out I, out R, in out SA = void, in out SE = A, out SR = never>
|
||||
export interface Options<in out A, in out I, in out R, in out SA = void, in out SE = A, out SR = never>
|
||||
extends make.Options<A, I, R, SA, SE, SR> {}
|
||||
}
|
||||
|
||||
export const service = <A, I = A, R = never, SA = void, SE = A, SR = never>(
|
||||
options: service.Options<A, I, R, SA, SE, SR>
|
||||
): Effect.Effect<Form<A, I, R, SA, SE, SR>, never, R | Scope.Scope> => Effect.tap(
|
||||
): Effect.Effect<Form<A, I, R, SA, SE, SR>, never, Scope.Scope | R | SR> => Effect.tap(
|
||||
make(options),
|
||||
form => Effect.forkScoped(run(form)),
|
||||
)
|
||||
@@ -220,24 +233,6 @@ extends Pipeable.Class() implements FormField<A, I> {
|
||||
|
||||
export const isFormField = (u: unknown): u is FormField<unknown, unknown> => Predicate.hasProperty(u, FormFieldTypeId)
|
||||
|
||||
export namespace useForm {
|
||||
export interface Options<in out A, in out I, out R, in out SA = void, in out SE = A, out SR = never>
|
||||
extends make.Options<A, I, R, SA, SE, SR> {}
|
||||
}
|
||||
|
||||
export const useForm: {
|
||||
<A, I = A, R = never, SA = void, SE = A, SR = never>(
|
||||
options: make.Options<A, I, R, SA, SE, SR>,
|
||||
deps: React.DependencyList,
|
||||
): Effect.Effect<Form<A, I, R, SA, SE, SR>, never, R>
|
||||
} = Effect.fnUntraced(function* <A, I = A, R = never, SA = void, SE = A, SR = never>(
|
||||
options: make.Options<A, I, R, SA, SE, SR>,
|
||||
deps: React.DependencyList,
|
||||
) {
|
||||
const form = yield* Hooks.useMemo(() => make(options), [options.debounce, ...deps])
|
||||
yield* Hooks.useFork(() => run(form), [form])
|
||||
return form
|
||||
})
|
||||
|
||||
export const useSubmit = <A, I, R, SA, SE, SR>(
|
||||
self: Form<A, I, R, SA, SE, SR>
|
||||
@@ -245,7 +240,7 @@ export const useSubmit = <A, I, R, SA, SE, SR>(
|
||||
() => Promise<Option.Option<AsyncData.AsyncData<SA, SE>>>,
|
||||
never,
|
||||
SR
|
||||
> => Hooks.useCallbackPromise(() => submit(self), [self])
|
||||
> => Component.useCallbackPromise(() => submit(self), [self])
|
||||
|
||||
export const useField = <A, I, R, SA, SE, SR, const P extends PropertyPath.Paths<NoInfer<I>>>(
|
||||
self: Form<A, I, R, SA, SE, SR>,
|
||||
@@ -271,33 +266,34 @@ export const useInput: {
|
||||
<A, I>(
|
||||
field: FormField<A, I>,
|
||||
options?: useInput.Options,
|
||||
): Effect.Effect<useInput.Result<I>, NoSuchElementException>
|
||||
): Effect.Effect<useInput.Result<I>, NoSuchElementException, Scope.Scope>
|
||||
} = Effect.fnUntraced(function* <A, I>(
|
||||
field: FormField<A, I>,
|
||||
options?: useInput.Options,
|
||||
) {
|
||||
const internalValueRef = yield* Hooks.useMemo(() => Effect.andThen(field.encodedValueRef, SubscriptionRef.make), [field])
|
||||
const [value, setValue] = yield* Hooks.useRefState(internalValueRef)
|
||||
|
||||
yield* Hooks.useFork(() => Effect.all([
|
||||
Stream.runForEach(
|
||||
Stream.drop(field.encodedValueRef, 1),
|
||||
upstreamEncodedValue => Effect.whenEffect(
|
||||
Ref.set(internalValueRef, upstreamEncodedValue),
|
||||
Effect.andThen(internalValueRef, internalValue => !Equal.equals(upstreamEncodedValue, internalValue)),
|
||||
const internalValueRef = yield* Component.useOnChange(() => Effect.tap(
|
||||
Effect.andThen(field.encodedValueRef, SubscriptionRef.make),
|
||||
internalValueRef => Effect.forkScoped(Effect.all([
|
||||
Stream.runForEach(
|
||||
Stream.drop(field.encodedValueRef, 1),
|
||||
upstreamEncodedValue => Effect.whenEffect(
|
||||
Ref.set(internalValueRef, upstreamEncodedValue),
|
||||
Effect.andThen(internalValueRef, internalValue => !Equal.equals(upstreamEncodedValue, internalValue)),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
Stream.runForEach(
|
||||
internalValueRef.changes.pipe(
|
||||
Stream.drop(1),
|
||||
Stream.changesWith(Equal.equivalence()),
|
||||
options?.debounce ? Stream.debounce(options.debounce) : identity,
|
||||
Stream.runForEach(
|
||||
internalValueRef.changes.pipe(
|
||||
Stream.drop(1),
|
||||
Stream.changesWith(Equal.equivalence()),
|
||||
options?.debounce ? Stream.debounce(options.debounce) : identity,
|
||||
),
|
||||
internalValue => Ref.set(field.encodedValueRef, internalValue),
|
||||
),
|
||||
internalValue => Ref.set(field.encodedValueRef, internalValue),
|
||||
),
|
||||
], { concurrency: "unbounded" }), [field, internalValueRef, options?.debounce])
|
||||
], { concurrency: "unbounded" })),
|
||||
), [field, options?.debounce])
|
||||
|
||||
const [value, setValue] = yield* SubscriptionRef.useSubscriptionRefState(internalValueRef)
|
||||
return { value, setValue }
|
||||
})
|
||||
|
||||
@@ -316,55 +312,56 @@ export const useOptionalInput: {
|
||||
<A, I>(
|
||||
field: FormField<A, Option.Option<I>>,
|
||||
options: useOptionalInput.Options<I>,
|
||||
): Effect.Effect<useOptionalInput.Result<I>, NoSuchElementException>
|
||||
): Effect.Effect<useOptionalInput.Result<I>, NoSuchElementException, Scope.Scope>
|
||||
} = Effect.fnUntraced(function* <A, I>(
|
||||
field: FormField<A, Option.Option<I>>,
|
||||
options: useOptionalInput.Options<I>,
|
||||
) {
|
||||
const [enabledRef, internalValueRef] = yield* Hooks.useMemo(() => Effect.andThen(
|
||||
field.encodedValueRef,
|
||||
Option.match({
|
||||
onSome: v => Effect.all([SubscriptionRef.make(true), SubscriptionRef.make(v)]),
|
||||
onNone: () => Effect.all([SubscriptionRef.make(false), SubscriptionRef.make(options.defaultValue)]),
|
||||
}),
|
||||
), [field])
|
||||
const [enabledRef, internalValueRef] = yield* Component.useOnChange(() => Effect.tap(
|
||||
Effect.andThen(
|
||||
field.encodedValueRef,
|
||||
Option.match({
|
||||
onSome: v => Effect.all([SubscriptionRef.make(true), SubscriptionRef.make(v)]),
|
||||
onNone: () => Effect.all([SubscriptionRef.make(false), SubscriptionRef.make(options.defaultValue)]),
|
||||
}),
|
||||
),
|
||||
|
||||
const [enabled, setEnabled] = yield* Hooks.useRefState(enabledRef)
|
||||
const [value, setValue] = yield* Hooks.useRefState(internalValueRef)
|
||||
([enabledRef, internalValueRef]) => Effect.forkScoped(Effect.all([
|
||||
Stream.runForEach(
|
||||
Stream.drop(field.encodedValueRef, 1),
|
||||
|
||||
yield* Hooks.useFork(() => Effect.all([
|
||||
Stream.runForEach(
|
||||
Stream.drop(field.encodedValueRef, 1),
|
||||
upstreamEncodedValue => Effect.whenEffect(
|
||||
Option.match(upstreamEncodedValue, {
|
||||
onSome: v => Effect.andThen(
|
||||
Ref.set(enabledRef, true),
|
||||
Ref.set(internalValueRef, v),
|
||||
),
|
||||
onNone: () => Effect.andThen(
|
||||
Ref.set(enabledRef, false),
|
||||
Ref.set(internalValueRef, options.defaultValue),
|
||||
),
|
||||
}),
|
||||
|
||||
upstreamEncodedValue => Effect.whenEffect(
|
||||
Option.match(upstreamEncodedValue, {
|
||||
onSome: v => Effect.andThen(
|
||||
Ref.set(enabledRef, true),
|
||||
Ref.set(internalValueRef, v),
|
||||
Effect.andThen(
|
||||
Effect.all([enabledRef, internalValueRef]),
|
||||
([enabled, internalValue]) => !Equal.equals(upstreamEncodedValue, enabled ? Option.some(internalValue) : Option.none()),
|
||||
),
|
||||
onNone: () => Effect.andThen(
|
||||
Ref.set(enabledRef, false),
|
||||
Ref.set(internalValueRef, options.defaultValue),
|
||||
),
|
||||
}),
|
||||
|
||||
Effect.andThen(
|
||||
Effect.all([enabledRef, internalValueRef]),
|
||||
([enabled, internalValue]) => !Equal.equals(upstreamEncodedValue, enabled ? Option.some(internalValue) : Option.none()),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
Stream.runForEach(
|
||||
enabledRef.changes.pipe(
|
||||
Stream.zipLatest(internalValueRef.changes),
|
||||
Stream.drop(1),
|
||||
Stream.changesWith(Equal.equivalence()),
|
||||
options?.debounce ? Stream.debounce(options.debounce) : identity,
|
||||
Stream.runForEach(
|
||||
enabledRef.changes.pipe(
|
||||
Stream.zipLatest(internalValueRef.changes),
|
||||
Stream.drop(1),
|
||||
Stream.changesWith(Equal.equivalence()),
|
||||
options?.debounce ? Stream.debounce(options.debounce) : identity,
|
||||
),
|
||||
([enabled, internalValue]) => Ref.set(field.encodedValueRef, enabled ? Option.some(internalValue) : Option.none()),
|
||||
),
|
||||
([enabled, internalValue]) => Ref.set(field.encodedValueRef, enabled ? Option.some(internalValue) : Option.none()),
|
||||
),
|
||||
], { concurrency: "unbounded" }), [field, enabledRef, internalValueRef, options.debounce])
|
||||
], { concurrency: "unbounded" })),
|
||||
), [field, options.debounce])
|
||||
|
||||
const [enabled, setEnabled] = yield* SubscriptionRef.useSubscriptionRefState(enabledRef)
|
||||
const [value, setValue] = yield* SubscriptionRef.useSubscriptionRefState(internalValueRef)
|
||||
return { enabled, setEnabled, value, setValue }
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user