Compare commits
1 Commits
ca0b4d7da5
...
b80043a4ec
| Author | SHA1 | Date | |
|---|---|---|---|
| b80043a4ec |
4
bun.lock
4
bun.lock
@@ -6,7 +6,7 @@
|
||||
"name": "@effect-fc/monorepo",
|
||||
"devDependencies": {
|
||||
"@biomejs/biome": "^2.3.4",
|
||||
"@effect/language-service": "^0.58.0",
|
||||
"@effect/language-service": "^0.57.0",
|
||||
"@types/bun": "^1.3.2",
|
||||
"npm-check-updates": "^19.1.2",
|
||||
"npm-sort": "^0.0.4",
|
||||
@@ -131,7 +131,7 @@
|
||||
|
||||
"@effect-fc/example": ["@effect-fc/example@workspace:packages/example"],
|
||||
|
||||
"@effect/language-service": ["@effect/language-service@0.58.0", "", { "bin": { "effect-language-service": "cli.js" } }, "sha512-M5T9zEEu6sLuzXOIp+bQ8B1pMcX3A9gyahTTWlv9idr+b2SlZOfydomwgXkod4vlXw7mYhLLcXgCsnHcBUz9rw=="],
|
||||
"@effect/language-service": ["@effect/language-service@0.57.0", "", { "bin": { "effect-language-service": "cli.js" } }, "sha512-Zwdw7UkEcOE0bTRoX7FRHzTYHVeDphsqycis/b8XH35ccjwo+uMvP7GfLof9kFfucmebvk895qsoOgSWYVQp4A=="],
|
||||
|
||||
"@effect/platform": ["@effect/platform@0.93.0", "", { "dependencies": { "find-my-way-ts": "^0.1.6", "msgpackr": "^1.11.4", "multipasta": "^0.2.7" }, "peerDependencies": { "effect": "^3.19.0" } }, "sha512-VaIv0duA+Dk2h8XYDPxCLCXGbMyd6hwuHUQt9THL1ZEqv1C3Fypg/Gi2UkzRys6TQsSnC9fJbdpMb7haPURYkQ=="],
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"@biomejs/biome": "^2.3.4",
|
||||
"@effect/language-service": "^0.58.0",
|
||||
"@effect/language-service": "^0.57.0",
|
||||
"@types/bun": "^1.3.2",
|
||||
"npm-check-updates": "^19.1.2",
|
||||
"npm-sort": "^0.0.4",
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
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 { Array, Cause, Chunk, type Duration, Effect, Equal, Exit, Fiber, flow, Hash, HashMap, identity, Option, ParseResult, Pipeable, Predicate, Ref, Schema, type Scope, Stream } from "effect"
|
||||
import type { NoSuchElementException } from "effect/Cause"
|
||||
import type * as React from "react"
|
||||
import * as Component from "./Component.js"
|
||||
import type * as Mutation from "./Mutation.js"
|
||||
import * as PropertyPath from "./PropertyPath.js"
|
||||
import * as Result from "./Result.js"
|
||||
import * as Subscribable from "./Subscribable.js"
|
||||
@@ -12,88 +12,54 @@ import * as SubscriptionSubRef from "./SubscriptionSubRef.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 MA, in out I = A, in out R = never, in out ME = never, in out MR = never, in out MP = never>
|
||||
export interface Form<in out A, in out I = A, out R = never, in out SA = void, in out SE = A, out SR = never, in out SP = never>
|
||||
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], MA, ME, MR, MP>
|
||||
readonly onSubmit: (value: NoInfer<A>) => Effect.Effect<SA, SE, SR>
|
||||
readonly initialSubmitProgress: SP
|
||||
readonly autosubmit: boolean
|
||||
readonly debounce: Option.Option<Duration.DurationInput>
|
||||
|
||||
readonly value: Subscribable.Subscribable<Option.Option<A>>
|
||||
readonly encodedValue: Subscribable.Subscribable<I>
|
||||
readonly error: Subscribable.Subscribable<Option.Option<ParseResult.ParseError>>
|
||||
readonly validationFiber: Subscribable.Subscribable<Option.Option<Fiber.Fiber<A, ParseResult.ParseError>>>
|
||||
readonly fieldCacheRef: Ref.Ref<HashMap.HashMap<FormFieldKey, FormField<unknown, unknown>>>
|
||||
readonly valueRef: SubscriptionRef.SubscriptionRef<Option.Option<A>>
|
||||
readonly encodedValueRef: SubscriptionRef.SubscriptionRef<I>
|
||||
readonly errorRef: SubscriptionRef.SubscriptionRef<Option.Option<ParseResult.ParseError>>
|
||||
readonly validationFiberRef: SubscriptionRef.SubscriptionRef<Option.Option<Fiber.Fiber<A, ParseResult.ParseError>>>
|
||||
readonly submitResultRef: SubscriptionRef.SubscriptionRef<Result.Result<SA, SE, SP>>
|
||||
|
||||
readonly canSubmit: Subscribable.Subscribable<boolean>
|
||||
|
||||
readonly submit: Effect.Effect<Option.Option<Result.Final<MA, ME, MP>>, Cause.NoSuchElementException>
|
||||
readonly canSubmitSubscribable: Subscribable.Subscribable<boolean>
|
||||
}
|
||||
|
||||
export class FormImpl<in out A, in out MA, in out I = A, in out R = never, in out ME = never, in out MR = never, in out MP = never>
|
||||
extends Pipeable.Class() implements Form<A, MA, I, R, ME, MR, MP> {
|
||||
class FormImpl<in out A, in out I = A, out R = never, in out SA = void, in out SE = A, out SR = never, in out SP = never>
|
||||
extends Pipeable.Class() implements Form<A, I, R, SA, SE, SR, SP> {
|
||||
readonly [FormTypeId]: FormTypeId = FormTypeId
|
||||
|
||||
constructor(
|
||||
readonly schema: Schema.Schema<A, I, R>,
|
||||
readonly mutation: Mutation.Mutation<readonly [value: A], MA, ME, MR, MP>,
|
||||
readonly onSubmit: (value: NoInfer<A>) => Effect.Effect<SA, SE, SR>,
|
||||
readonly initialSubmitProgress: SP,
|
||||
readonly autosubmit: boolean,
|
||||
readonly debounce: Option.Option<Duration.DurationInput>,
|
||||
|
||||
readonly value: SubscriptionRef.SubscriptionRef<Option.Option<A>>,
|
||||
readonly encodedValue: SubscriptionRef.SubscriptionRef<I>,
|
||||
readonly error: SubscriptionRef.SubscriptionRef<Option.Option<ParseResult.ParseError>>,
|
||||
readonly validationFiber: SubscriptionRef.SubscriptionRef<Option.Option<Fiber.Fiber<A, ParseResult.ParseError>>>,
|
||||
readonly fieldCacheRef: Ref.Ref<HashMap.HashMap<FormFieldKey, FormField<unknown, unknown>>>,
|
||||
readonly valueRef: SubscriptionRef.SubscriptionRef<Option.Option<A>>,
|
||||
readonly encodedValueRef: SubscriptionRef.SubscriptionRef<I>,
|
||||
readonly errorRef: SubscriptionRef.SubscriptionRef<Option.Option<ParseResult.ParseError>>,
|
||||
readonly validationFiberRef: SubscriptionRef.SubscriptionRef<Option.Option<Fiber.Fiber<A, ParseResult.ParseError>>>,
|
||||
readonly submitResultRef: SubscriptionRef.SubscriptionRef<Result.Result<SA, SE, SP>>,
|
||||
|
||||
readonly runSemaphore: Effect.Semaphore,
|
||||
readonly fieldCache: Ref.Ref<HashMap.HashMap<FormFieldKey, FormField<unknown, unknown>>>,
|
||||
readonly canSubmitSubscribable: Subscribable.Subscribable<boolean>,
|
||||
) {
|
||||
super()
|
||||
}
|
||||
|
||||
get canSubmit(): Subscribable.Subscribable<boolean> {
|
||||
return Subscribable.map(
|
||||
Subscribable.zipLatestAll(this.value, this.error, this.validationFiber, this.mutation.result),
|
||||
([value, error, validationFiber, submitResult]) => (
|
||||
Option.isSome(value) &&
|
||||
Option.isNone(error) &&
|
||||
Option.isNone(validationFiber) &&
|
||||
!(Result.isRunning(submitResult) || Result.isRefreshing(submitResult))
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
get submit(): Effect.Effect<Option.Option<Result.Final<MA, ME, MP>>, Cause.NoSuchElementException> {
|
||||
return Effect.whenEffect(
|
||||
this.value.pipe(
|
||||
Effect.andThen(identity),
|
||||
Effect.andThen(value => this.mutation.mutate([value])),
|
||||
Effect.tap(result => Result.isFailure(result)
|
||||
? Option.match(
|
||||
Chunk.findFirst(
|
||||
Cause.failures(result.cause as Cause.Cause<ParseResult.ParseError>),
|
||||
e => e._tag === "ParseError",
|
||||
),
|
||||
{
|
||||
onSome: e => Ref.set(this.error, Option.some(e)),
|
||||
onNone: () => Effect.void,
|
||||
},
|
||||
)
|
||||
: Effect.void
|
||||
),
|
||||
),
|
||||
|
||||
this.canSubmit.get,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
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 MA, in out I = A, in out R = never, in out ME = never, in out MR = never, in out MP = never> {
|
||||
export interface Options<in out A, in out I, in out R, in out SA = void, in out SE = A, out SR = never, in out SP = never> {
|
||||
readonly schema: Schema.Schema<A, I, R>
|
||||
readonly initialEncodedValue: NoInfer<I>
|
||||
readonly onSubmit: (
|
||||
@@ -116,9 +82,12 @@ export const make = Effect.fnUntraced(function* <A, I = A, R = never, SA = void,
|
||||
const valueRef = yield* SubscriptionRef.make(Option.none<A>())
|
||||
const errorRef = yield* SubscriptionRef.make(Option.none<ParseResult.ParseError>())
|
||||
const validationFiberRef = yield* SubscriptionRef.make(Option.none<Fiber.Fiber<A, ParseResult.ParseError>>())
|
||||
const submitResultRef = yield* SubscriptionRef.make<Result.Result<SA, SE, SP>>(Result.initial())
|
||||
|
||||
return new FormImpl(
|
||||
options.schema,
|
||||
options.onSubmit as any,
|
||||
options.initialSubmitProgress as SP,
|
||||
options.autosubmit ?? false,
|
||||
Option.fromNullable(options.debounce),
|
||||
|
||||
@@ -127,52 +96,97 @@ export const make = Effect.fnUntraced(function* <A, I = A, R = never, SA = void,
|
||||
yield* SubscriptionRef.make(options.initialEncodedValue),
|
||||
errorRef,
|
||||
validationFiberRef,
|
||||
submitResultRef,
|
||||
|
||||
Subscribable.map(
|
||||
Subscribable.zipLatestAll(valueRef, errorRef, validationFiberRef, submitResultRef),
|
||||
([value, error, validationFiber, submitResult]) => (
|
||||
Option.isSome(value) &&
|
||||
Option.isNone(error) &&
|
||||
Option.isNone(validationFiber) &&
|
||||
!(Result.isRunning(submitResult) || Result.isRefreshing(submitResult))
|
||||
),
|
||||
),
|
||||
)
|
||||
})
|
||||
|
||||
export const run = <A, MA, I, R, ME, MR, MP>(
|
||||
self: Form<A, MA, I, R, ME, MR, MP>
|
||||
): Effect.Effect<void> => {
|
||||
const _self = self as FormImpl<A, MA, I, R, ME, MR, MP>
|
||||
return _self.runSemaphore.withPermits(1)(Stream.runForEach(
|
||||
_self.encodedValue.changes.pipe(
|
||||
Option.isSome(self.debounce) ? Stream.debounce(self.debounce.value) : identity
|
||||
),
|
||||
export const run = <A, I, R, SA, SE, SR, SP>(
|
||||
self: Form<A, I, R, SA, SE, SR, SP>
|
||||
): Effect.Effect<void, never, Scope.Scope | R | SR> => Stream.runForEach(
|
||||
self.encodedValueRef.changes.pipe(
|
||||
Option.isSome(self.debounce) ? Stream.debounce(self.debounce.value) : identity
|
||||
),
|
||||
|
||||
encodedValue => _self.validationFiber.pipe(
|
||||
Effect.andThen(Option.match({
|
||||
onSome: Fiber.interrupt,
|
||||
onNone: () => Effect.void,
|
||||
})),
|
||||
Effect.andThen(
|
||||
Effect.forkScoped(Effect.onExit(
|
||||
Schema.decode(_self.schema, { errors: "all" })(encodedValue),
|
||||
exit => Effect.andThen(
|
||||
Exit.matchEffect(exit, {
|
||||
onSuccess: v => Effect.andThen(
|
||||
Ref.set(_self.value, Option.some(v)),
|
||||
Ref.set(_self.error, Option.none()),
|
||||
),
|
||||
onFailure: c => Option.match(Chunk.findFirst(Cause.failures(c), e => e._tag === "ParseError"), {
|
||||
onSome: e => Ref.set(_self.error, Option.some(e)),
|
||||
onNone: () => Effect.void,
|
||||
}),
|
||||
encodedValue => self.validationFiberRef.pipe(
|
||||
Effect.andThen(Option.match({
|
||||
onSome: Fiber.interrupt,
|
||||
onNone: () => Effect.void,
|
||||
})),
|
||||
Effect.andThen(
|
||||
Effect.forkScoped(Effect.onExit(
|
||||
Schema.decode(self.schema, { errors: "all" })(encodedValue),
|
||||
exit => Effect.andThen(
|
||||
Exit.matchEffect(exit, {
|
||||
onSuccess: v => Effect.andThen(
|
||||
Ref.set(self.valueRef, Option.some(v)),
|
||||
Ref.set(self.errorRef, Option.none()),
|
||||
),
|
||||
onFailure: c => Option.match(Chunk.findFirst(Cause.failures(c), e => e._tag === "ParseError"), {
|
||||
onSome: e => Ref.set(self.errorRef, Option.some(e)),
|
||||
onNone: () => Effect.void,
|
||||
}),
|
||||
Ref.set(_self.validationFiber, Option.none()),
|
||||
),
|
||||
)).pipe(
|
||||
Effect.tap(fiber => Ref.set(_self.validationFiber, Option.some(fiber))),
|
||||
Effect.andThen(Fiber.join),
|
||||
Effect.andThen(() => self.autosubmit
|
||||
? Effect.asVoid(Effect.forkScoped(submit(self)))
|
||||
: Effect.void
|
||||
),
|
||||
Effect.forkScoped,
|
||||
)
|
||||
),
|
||||
}),
|
||||
Ref.set(self.validationFiberRef, Option.none()),
|
||||
),
|
||||
)).pipe(
|
||||
Effect.tap(fiber => Ref.set(self.validationFiberRef, Option.some(fiber))),
|
||||
Effect.andThen(Fiber.join),
|
||||
Effect.andThen(() => self.autosubmit
|
||||
? Effect.asVoid(Effect.forkScoped(submit(self)))
|
||||
: Effect.void
|
||||
),
|
||||
Effect.forkScoped,
|
||||
)
|
||||
),
|
||||
))
|
||||
}
|
||||
),
|
||||
)
|
||||
|
||||
export const submit = <A, I, R, SA, SE, SR, SP>(
|
||||
self: Form<A, I, R, SA, SE, SR, SP>
|
||||
): Effect.Effect<
|
||||
Option.Option<Result.Result<SA, SE, SP>>,
|
||||
NoSuchElementException,
|
||||
Scope.Scope | SR
|
||||
> => Effect.whenEffect(
|
||||
self.valueRef.pipe(
|
||||
Effect.andThen(identity),
|
||||
Effect.andThen(value => Result.unsafeForkEffect(
|
||||
self.onSubmit(value),
|
||||
{ initialProgress: self.initialSubmitProgress },
|
||||
)),
|
||||
Effect.andThen(([sub]) => Effect.all([Effect.succeed(sub), sub.get])),
|
||||
Effect.andThen(([sub, initial]) => Stream.runFoldEffect(
|
||||
sub.changes,
|
||||
initial,
|
||||
(_, result) => Effect.as(Ref.set(self.submitResultRef, result), result),
|
||||
)),
|
||||
Effect.tap(result => Result.isFailure(result)
|
||||
? Option.match(
|
||||
Chunk.findFirst(
|
||||
Cause.failures(result.cause as Cause.Cause<ParseResult.ParseError>),
|
||||
e => e._tag === "ParseError",
|
||||
),
|
||||
{
|
||||
onSome: e => Ref.set(self.errorRef, Option.some(e)),
|
||||
onNone: () => Effect.void,
|
||||
},
|
||||
)
|
||||
: Effect.void
|
||||
),
|
||||
),
|
||||
|
||||
self.canSubmitSubscribable.get,
|
||||
)
|
||||
|
||||
export namespace service {
|
||||
export interface Options<in out A, in out I, in out R, in out SA = void, in out SE = A, out SR = never, in out SP = never>
|
||||
@@ -214,11 +228,11 @@ 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: SubscriptionRef.SubscriptionRef<I>
|
||||
readonly issues: Subscribable.Subscribable<readonly ParseResult.ArrayFormatterIssue[]>
|
||||
readonly isValidating: Subscribable.Subscribable<boolean>
|
||||
readonly isSubmitting: Subscribable.Subscribable<boolean>
|
||||
readonly valueSubscribable: Subscribable.Subscribable<Option.Option<A>, NoSuchElementException>
|
||||
readonly encodedValueRef: SubscriptionRef.SubscriptionRef<I>
|
||||
readonly issuesSubscribable: Subscribable.Subscribable<readonly ParseResult.ArrayFormatterIssue[]>
|
||||
readonly isValidatingSubscribable: Subscribable.Subscribable<boolean>
|
||||
readonly isSubmittingSubscribable: Subscribable.Subscribable<boolean>
|
||||
}
|
||||
|
||||
class FormFieldImpl<in out A, in out I = A>
|
||||
@@ -226,11 +240,11 @@ extends Pipeable.Class() implements FormField<A, I> {
|
||||
readonly [FormFieldTypeId]: FormFieldTypeId = FormFieldTypeId
|
||||
|
||||
constructor(
|
||||
readonly value: Subscribable.Subscribable<Option.Option<A>, Cause.NoSuchElementException>,
|
||||
readonly encodedValue: SubscriptionRef.SubscriptionRef<I>,
|
||||
readonly issues: Subscribable.Subscribable<readonly ParseResult.ArrayFormatterIssue[]>,
|
||||
readonly isValidating: Subscribable.Subscribable<boolean>,
|
||||
readonly isSubmitting: Subscribable.Subscribable<boolean>,
|
||||
readonly valueSubscribable: Subscribable.Subscribable<Option.Option<A>, NoSuchElementException>,
|
||||
readonly encodedValueRef: SubscriptionRef.SubscriptionRef<I>,
|
||||
readonly issuesSubscribable: Subscribable.Subscribable<readonly ParseResult.ArrayFormatterIssue[]>,
|
||||
readonly isValidatingSubscribable: Subscribable.Subscribable<boolean>,
|
||||
readonly isSubmittingSubscribable: Subscribable.Subscribable<boolean>,
|
||||
) {
|
||||
super()
|
||||
}
|
||||
@@ -254,28 +268,25 @@ class FormFieldKey implements Equal.Equal {
|
||||
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, MA, I, R, ME, MR, MP, const P extends PropertyPath.Paths<NoInfer<I>>>(
|
||||
self: Form<A, MA, I, R, ME, MR, MP>,
|
||||
export const makeFormField = <A, I, R, SA, SE, SR, SP, const P extends PropertyPath.Paths<NoInfer<I>>>(
|
||||
self: Form<A, I, R, SA, SE, SR, SP>,
|
||||
path: P,
|
||||
): FormField<PropertyPath.ValueFromPath<A, P>, PropertyPath.ValueFromPath<I, P>> => {
|
||||
const _self = self as FormImpl<A, MA, I, R, ME, MR, MP>
|
||||
return new FormFieldImpl(
|
||||
Subscribable.mapEffect(_self.value, Option.match({
|
||||
onSome: v => Option.map(PropertyPath.get(v, path), Option.some),
|
||||
onNone: () => Option.some(Option.none()),
|
||||
})),
|
||||
SubscriptionSubRef.makeFromPath(_self.encodedValue, path),
|
||||
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.isRefreshing(result)),
|
||||
)
|
||||
}
|
||||
): FormField<PropertyPath.ValueFromPath<A, P>, PropertyPath.ValueFromPath<I, P>> => new FormFieldImpl(
|
||||
Subscribable.mapEffect(self.valueRef, Option.match({
|
||||
onSome: v => Option.map(PropertyPath.get(v, path), Option.some),
|
||||
onNone: () => Option.some(Option.none()),
|
||||
})),
|
||||
SubscriptionSubRef.makeFromPath(self.encodedValueRef, path),
|
||||
Subscribable.mapEffect(self.errorRef, Option.match({
|
||||
onSome: flow(
|
||||
ParseResult.ArrayFormatter.formatError,
|
||||
Effect.map(Array.filter(issue => PropertyPath.equivalence(issue.path, path))),
|
||||
),
|
||||
onNone: () => Effect.succeed([]),
|
||||
})),
|
||||
Subscribable.map(self.validationFiberRef, Option.isSome),
|
||||
Subscribable.map(self.submitResultRef, result => Result.isRunning(result) || Result.isRefreshing(result)),
|
||||
)
|
||||
|
||||
|
||||
export namespace useInput {
|
||||
|
||||
@@ -17,7 +17,7 @@ extends Pipeable.Pipeable {
|
||||
readonly fiber: Subscribable.Subscribable<Option.Option<Fiber.Fiber<A, E>>>
|
||||
readonly result: Subscribable.Subscribable<Result.Result<A, E, P>>
|
||||
|
||||
mutate(key: K): Effect.Effect<Result.Final<A, E, P>>
|
||||
mutate(key: K): Effect.Effect<Result.Result<A, E, P>>
|
||||
mutateSubscribable(key: K): Effect.Effect<Subscribable.Subscribable<Result.Result<A, E, P>>>
|
||||
}
|
||||
|
||||
@@ -37,12 +37,13 @@ extends Pipeable.Class() implements Mutation<K, A, E, R, P> {
|
||||
super()
|
||||
}
|
||||
|
||||
mutate(key: K): Effect.Effect<Result.Final<A, E, P>> {
|
||||
mutate(key: K): Effect.Effect<Result.Result<A, E, P>> {
|
||||
return SubscriptionRef.set(this.latestKey, Option.some(key)).pipe(
|
||||
Effect.andThen(Effect.provide(this.start(key), this.context)),
|
||||
Effect.andThen(sub => this.watch(sub)),
|
||||
)
|
||||
}
|
||||
|
||||
mutateSubscribable(key: K): Effect.Effect<Subscribable.Subscribable<Result.Result<A, E, P>>> {
|
||||
return Effect.andThen(
|
||||
SubscriptionRef.set(this.latestKey, Option.some(key)),
|
||||
@@ -56,7 +57,7 @@ extends Pipeable.Class() implements Mutation<K, A, E, R, P> {
|
||||
Scope.Scope | R
|
||||
> {
|
||||
return this.result.pipe(
|
||||
Effect.map(previous => Result.isFinal(previous)
|
||||
Effect.map(previous => (Result.isSuccess(previous) || Result.isFailure(previous))
|
||||
? previous
|
||||
: undefined
|
||||
),
|
||||
@@ -83,7 +84,7 @@ extends Pipeable.Class() implements Mutation<K, A, E, R, P> {
|
||||
|
||||
watch(
|
||||
sub: Subscribable.Subscribable<Result.Result<A, E, P>>
|
||||
): Effect.Effect<Result.Final<A, E, P>> {
|
||||
): Effect.Effect<Result.Result<A, E, P>> {
|
||||
return Effect.andThen(
|
||||
sub.get,
|
||||
initial => Stream.runFoldEffect(
|
||||
@@ -91,20 +92,20 @@ extends Pipeable.Class() implements Mutation<K, A, E, R, P> {
|
||||
initial,
|
||||
(_, result) => Effect.as(SubscriptionRef.set(this.result, result), result),
|
||||
),
|
||||
) as Effect.Effect<Result.Final<A, E, P>>
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export const isMutation = (u: unknown): u is Mutation<unknown[], unknown, unknown, unknown, unknown> => Predicate.hasProperty(u, MutationTypeId)
|
||||
|
||||
export declare namespace make {
|
||||
export interface Options<K extends readonly any[] = never, A = void, E = never, R = never, P = never> {
|
||||
readonly f: (key: K) => Effect.Effect<A, E, Result.forkEffect.InputContext<R, NoInfer<P>>>
|
||||
export interface Options<K extends readonly any[], A, E = never, R = never, P = never> {
|
||||
readonly f: (key: NoInfer<K>) => Effect.Effect<A, E, Result.forkEffect.InputContext<R, NoInfer<P>>>
|
||||
readonly initialProgress?: P
|
||||
}
|
||||
}
|
||||
|
||||
export const make = Effect.fnUntraced(function* <const K extends readonly any[] = never, A = void, E = never, R = never, P = never>(
|
||||
export const make = Effect.fnUntraced(function* <K extends readonly any[], A, E = never, R = never, P = never>(
|
||||
options: make.Options<K, A, E, R, P>
|
||||
): Effect.fn.Return<
|
||||
Mutation<K, A, E, Result.forkEffect.OutputContext<A, E, R, P>, P>,
|
||||
|
||||
@@ -18,12 +18,9 @@ extends Pipeable.Pipeable {
|
||||
readonly fiber: Subscribable.Subscribable<Option.Option<Fiber.Fiber<A, E>>>
|
||||
readonly result: Subscribable.Subscribable<Result.Result<A, E, P>>
|
||||
|
||||
fetch(key: K): Effect.Effect<Result.Final<A, E, P>>
|
||||
fetchSubscribable(key: K): Effect.Effect<Subscribable.Subscribable<Result.Result<A, E, P>>>
|
||||
readonly refetch: Effect.Effect<Result.Final<A, E, P>, Cause.NoSuchElementException>
|
||||
readonly refetchSubscribable: Effect.Effect<Subscribable.Subscribable<Result.Result<A, E, P>>, Cause.NoSuchElementException>
|
||||
readonly refresh: Effect.Effect<Result.Final<A, E, P>, Cause.NoSuchElementException>
|
||||
readonly refreshSubscribable: Effect.Effect<Subscribable.Subscribable<Result.Result<A, E, P>>, Cause.NoSuchElementException>
|
||||
fetch(key: K): Effect.Effect<Result.Result<A, E, P>>
|
||||
readonly refetch: Effect.Effect<Result.Result<A, E, P>, Cause.NoSuchElementException>
|
||||
readonly refresh: Effect.Effect<Result.Result<A, E, P>, Cause.NoSuchElementException>
|
||||
}
|
||||
|
||||
export class QueryImpl<in out K extends readonly any[], in out A, in out E = never, in out R = never, in out P = never>
|
||||
@@ -39,8 +36,6 @@ extends Pipeable.Class() implements Query<K, A, E, R, P> {
|
||||
readonly latestKey: SubscriptionRef.SubscriptionRef<Option.Option<K>>,
|
||||
readonly fiber: SubscriptionRef.SubscriptionRef<Option.Option<Fiber.Fiber<A, E>>>,
|
||||
readonly result: SubscriptionRef.SubscriptionRef<Result.Result<A, E, P>>,
|
||||
|
||||
readonly runSemaphore: Effect.Semaphore,
|
||||
) {
|
||||
super()
|
||||
}
|
||||
@@ -52,20 +47,15 @@ extends Pipeable.Class() implements Query<K, A, E, R, P> {
|
||||
}))
|
||||
}
|
||||
|
||||
fetch(key: K): Effect.Effect<Result.Final<A, E, P>> {
|
||||
fetch(key: K): Effect.Effect<Result.Result<A, E, P>> {
|
||||
return this.interrupt.pipe(
|
||||
Effect.andThen(SubscriptionRef.set(this.latestKey, Option.some(key))),
|
||||
Effect.andThen(Effect.provide(this.start(key), this.context)),
|
||||
Effect.andThen(sub => this.watch(sub)),
|
||||
)
|
||||
}
|
||||
fetchSubscribable(key: K): Effect.Effect<Subscribable.Subscribable<Result.Result<A, E, P>>> {
|
||||
return this.interrupt.pipe(
|
||||
Effect.andThen(SubscriptionRef.set(this.latestKey, Option.some(key))),
|
||||
Effect.andThen(Effect.provide(this.start(key), this.context)),
|
||||
)
|
||||
}
|
||||
get refetch(): Effect.Effect<Result.Final<A, E, P>, Cause.NoSuchElementException> {
|
||||
|
||||
get refetch(): Effect.Effect<Result.Result<A, E, P>, Cause.NoSuchElementException> {
|
||||
return this.interrupt.pipe(
|
||||
Effect.andThen(this.latestKey),
|
||||
Effect.andThen(identity),
|
||||
@@ -73,14 +63,8 @@ extends Pipeable.Class() implements Query<K, A, E, R, P> {
|
||||
Effect.andThen(sub => this.watch(sub)),
|
||||
)
|
||||
}
|
||||
get refetchSubscribable(): Effect.Effect<Subscribable.Subscribable<Result.Result<A, E, P>>, Cause.NoSuchElementException> {
|
||||
return this.interrupt.pipe(
|
||||
Effect.andThen(this.latestKey),
|
||||
Effect.andThen(identity),
|
||||
Effect.andThen(key => Effect.provide(this.start(key), this.context)),
|
||||
)
|
||||
}
|
||||
get refresh(): Effect.Effect<Result.Final<A, E, P>, Cause.NoSuchElementException> {
|
||||
|
||||
get refresh(): Effect.Effect<Result.Result<A, E, P>, Cause.NoSuchElementException> {
|
||||
return this.interrupt.pipe(
|
||||
Effect.andThen(this.latestKey),
|
||||
Effect.andThen(identity),
|
||||
@@ -88,13 +72,6 @@ extends Pipeable.Class() implements Query<K, A, E, R, P> {
|
||||
Effect.andThen(sub => this.watch(sub)),
|
||||
)
|
||||
}
|
||||
get refreshSubscribable(): Effect.Effect<Subscribable.Subscribable<Result.Result<A, E, P>>, Cause.NoSuchElementException> {
|
||||
return this.interrupt.pipe(
|
||||
Effect.andThen(this.latestKey),
|
||||
Effect.andThen(identity),
|
||||
Effect.andThen(key => Effect.provide(this.start(key, true), this.context)),
|
||||
)
|
||||
}
|
||||
|
||||
start(
|
||||
key: K,
|
||||
@@ -105,7 +82,7 @@ extends Pipeable.Class() implements Query<K, A, E, R, P> {
|
||||
Scope.Scope | R
|
||||
> {
|
||||
return this.result.pipe(
|
||||
Effect.map(previous => Result.isFinal(previous)
|
||||
Effect.map(previous => (Result.isSuccess(previous) || Result.isFailure(previous))
|
||||
? previous
|
||||
: undefined
|
||||
),
|
||||
@@ -124,7 +101,7 @@ extends Pipeable.Class() implements Query<K, A, E, R, P> {
|
||||
|
||||
watch(
|
||||
sub: Subscribable.Subscribable<Result.Result<A, E, P>>
|
||||
): Effect.Effect<Result.Final<A, E, P>> {
|
||||
): Effect.Effect<Result.Result<A, E, P>> {
|
||||
return Effect.andThen(
|
||||
sub.get,
|
||||
initial => Stream.runFoldEffect(
|
||||
@@ -132,7 +109,7 @@ extends Pipeable.Class() implements Query<K, A, E, R, P> {
|
||||
initial,
|
||||
(_, result) => Effect.as(SubscriptionRef.set(this.result, result), result),
|
||||
),
|
||||
) as Effect.Effect<Result.Final<A, E, P>>
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -162,8 +139,6 @@ export const make = Effect.fnUntraced(function* <K extends readonly any[], A, E
|
||||
yield* SubscriptionRef.make(Option.none<K>()),
|
||||
yield* SubscriptionRef.make(Option.none<Fiber.Fiber<A, E>>()),
|
||||
yield* SubscriptionRef.make(Result.initial<A, E, P>()),
|
||||
|
||||
yield* Effect.makeSemaphore(1),
|
||||
)
|
||||
})
|
||||
|
||||
@@ -187,6 +162,5 @@ export const run = <K extends readonly any[], A, E, R, P>(
|
||||
Effect.andThen(_self.start(key)),
|
||||
Effect.andThen(sub => Effect.forkScoped(_self.watch(sub))),
|
||||
Effect.provide(_self.context),
|
||||
_self.runSemaphore.withPermits(1),
|
||||
))
|
||||
}
|
||||
|
||||
@@ -100,7 +100,6 @@ const ResultPrototype = Object.freeze({
|
||||
|
||||
|
||||
export const isResult = (u: unknown): u is Result<unknown, unknown, unknown> => Predicate.hasProperty(u, ResultTypeId)
|
||||
export const isFinal = (u: unknown): u is Final<unknown, unknown, unknown> => isResult(u) && (isSuccess(u) || isFailure(u))
|
||||
export const isInitial = (u: unknown): u is Initial => isResult(u) && u._tag === "Initial"
|
||||
export const isRunning = (u: unknown): u is Running<unknown> => isResult(u) && u._tag === "Running"
|
||||
export const isSuccess = (u: unknown): u is Success<unknown> => isResult(u) && u._tag === "Success"
|
||||
@@ -122,7 +121,6 @@ export const fail = <E, A = never>(
|
||||
cause,
|
||||
previousSuccess: Option.fromNullable(previousSuccess),
|
||||
}, ResultPrototype)
|
||||
|
||||
export const refreshing = <R extends Success<any> | Failure<any, any>, P = never>(
|
||||
result: R,
|
||||
progress?: P,
|
||||
@@ -201,11 +199,11 @@ export namespace unsafeForkEffect {
|
||||
|
||||
export type Options<A, E, P> = {
|
||||
readonly initialProgress?: P
|
||||
readonly previous?: Final<A, E, P>
|
||||
readonly previous?: Success<A> | Failure<A, E>
|
||||
} & (
|
||||
| {
|
||||
readonly refresh: true
|
||||
readonly previous: Final<A, E, P>
|
||||
readonly previous: Success<A> | Failure<A, E>
|
||||
}
|
||||
| {
|
||||
readonly refresh?: false
|
||||
|
||||
@@ -2,7 +2,7 @@ import { HttpClient, type HttpClientError } from "@effect/platform"
|
||||
import { Button, Container, Flex, Heading, Slider, Text } from "@radix-ui/themes"
|
||||
import { createFileRoute } from "@tanstack/react-router"
|
||||
import { Array, Cause, Chunk, Console, Effect, flow, Match, Option, Schema, Stream } from "effect"
|
||||
import { Component, ErrorObserver, Mutation, Query, Result, Subscribable, SubscriptionRef } from "effect-fc"
|
||||
import { Component, ErrorObserver, Query, Result, Subscribable, SubscriptionRef } from "effect-fc"
|
||||
import { runtime } from "@/runtime"
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ const Post = Schema.Struct({
|
||||
const ResultView = Component.makeUntraced("Result")(function*() {
|
||||
const runPromise = yield* Component.useRunPromise()
|
||||
|
||||
const [idRef, query, mutation] = yield* Component.useOnMount(() => Effect.gen(function*() {
|
||||
const [idRef, query] = yield* Component.useOnMount(() => Effect.gen(function*() {
|
||||
const idRef = yield* SubscriptionRef.make(1)
|
||||
const key = Stream.zipLatest(Stream.make("posts" as const), idRef.changes)
|
||||
|
||||
@@ -30,20 +30,11 @@ const ResultView = Component.makeUntraced("Result")(function*() {
|
||||
),
|
||||
})
|
||||
|
||||
const mutation = yield* Mutation.make({
|
||||
f: ([id]: readonly [id: number]) => HttpClient.HttpClient.pipe(
|
||||
Effect.tap(Effect.sleep("500 millis")),
|
||||
Effect.andThen(client => client.get(`https://jsonplaceholder.typicode.com/posts/${ id }`)),
|
||||
Effect.andThen(response => response.json),
|
||||
Effect.andThen(Schema.decodeUnknown(Post)),
|
||||
),
|
||||
})
|
||||
|
||||
return [idRef, query, mutation] as const
|
||||
return [idRef, query] as const
|
||||
}))
|
||||
|
||||
const [id, setId] = yield* SubscriptionRef.useSubscriptionRefState(idRef)
|
||||
const [queryResult, mutationResult] = yield* Subscribable.useSubscribables([query.result, mutation.result])
|
||||
const [result] = yield* Subscribable.useSubscribables([query.result])
|
||||
|
||||
yield* Component.useOnMount(() => ErrorObserver.ErrorObserver<HttpClientError.HttpClientError>().pipe(
|
||||
Effect.andThen(observer => observer.subscribe),
|
||||
@@ -68,44 +59,23 @@ const ResultView = Component.makeUntraced("Result")(function*() {
|
||||
onValueChange={flow(Array.head, Option.getOrThrow, setId)}
|
||||
/>
|
||||
|
||||
<div>
|
||||
{Match.value(queryResult).pipe(
|
||||
Match.tag("Running", () => <Text>Loading...</Text>),
|
||||
Match.tag("Success", result => <>
|
||||
<Heading>{result.value.title}</Heading>
|
||||
<Text>{result.value.body}</Text>
|
||||
{Result.isRefreshing(result) && <Text>Refreshing...</Text>}
|
||||
</>),
|
||||
Match.tag("Failure", result =>
|
||||
<Text>An error has occured: {result.cause.toString()}</Text>
|
||||
),
|
||||
Match.orElse(() => <></>),
|
||||
)}
|
||||
</div>
|
||||
{Match.value(result).pipe(
|
||||
Match.tag("Running", () => <Text>Loading...</Text>),
|
||||
Match.tag("Success", result => <>
|
||||
<Heading>{result.value.title}</Heading>
|
||||
<Text>{result.value.body}</Text>
|
||||
{Result.isRefreshing(result) && <Text>Refreshing...</Text>}
|
||||
</>),
|
||||
Match.tag("Failure", result =>
|
||||
<Text>An error has occured: {result.cause.toString()}</Text>
|
||||
),
|
||||
Match.orElse(() => <></>),
|
||||
)}
|
||||
|
||||
<Flex direction="row" justify="center" align="center" gap="1">
|
||||
<Button onClick={() => runPromise(query.refresh)}>Refresh</Button>
|
||||
<Button onClick={() => runPromise(query.refetch)}>Refetch</Button>
|
||||
</Flex>
|
||||
|
||||
<div>
|
||||
{Match.value(mutationResult).pipe(
|
||||
Match.tag("Running", () => <Text>Loading...</Text>),
|
||||
Match.tag("Success", result => <>
|
||||
<Heading>{result.value.title}</Heading>
|
||||
<Text>{result.value.body}</Text>
|
||||
{Result.isRefreshing(result) && <Text>Refreshing...</Text>}
|
||||
</>),
|
||||
Match.tag("Failure", result =>
|
||||
<Text>An error has occured: {result.cause.toString()}</Text>
|
||||
),
|
||||
Match.orElse(() => <></>),
|
||||
)}
|
||||
</div>
|
||||
|
||||
<Flex direction="row" justify="center" align="center" gap="1">
|
||||
<Button onClick={() => runPromise(Effect.andThen(idRef, id => mutation.mutate([id])))}>Mutate</Button>
|
||||
</Flex>
|
||||
</Flex>
|
||||
</Container>
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user