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