0.2.2 #31
@@ -1,10 +1,10 @@
|
|||||||
import * as AsyncData from "@typed/async-data"
|
import * as AsyncData from "@typed/async-data"
|
||||||
import { Array, Cause, Chunk, type Duration, Effect, Equal, Exit, Fiber, flow, identity, Option, ParseResult, Pipeable, Predicate, pipe, Ref, Schema, type Scope, Stream, type Subscribable, SubscriptionRef } from "effect"
|
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 type { NoSuchElementException } from "effect/Cause"
|
import type { NoSuchElementException } from "effect/Cause"
|
||||||
import * as React from "react"
|
import * as React from "react"
|
||||||
import * as Hooks from "./Hooks/index.js"
|
import * as Hooks from "./Hooks/index.js"
|
||||||
import * as PropertyPath from "./PropertyPath.js"
|
import * as PropertyPath from "./PropertyPath.js"
|
||||||
import * as SubscribableInternal from "./Subscribable.js"
|
import * as Subscribable from "./Subscribable.js"
|
||||||
import * as SubscriptionSubRef from "./SubscriptionSubRef.js"
|
import * as SubscriptionSubRef from "./SubscriptionSubRef.js"
|
||||||
|
|
||||||
|
|
||||||
@@ -83,28 +83,14 @@ export const make: {
|
|||||||
validationFiberRef,
|
validationFiberRef,
|
||||||
submitStateRef,
|
submitStateRef,
|
||||||
|
|
||||||
pipe(
|
Subscribable.map(
|
||||||
<A>([value, error, validationFiber, submitState]: readonly [
|
Subscribable.zipLatestAll(valueRef, errorRef, validationFiberRef, submitStateRef),
|
||||||
Option.Option<A>,
|
([value, error, validationFiber, submitState]) => (
|
||||||
Option.Option<ParseResult.ParseError>,
|
Option.isSome(value) &&
|
||||||
Option.Option<Fiber.Fiber<void, never>>,
|
Option.isNone(error) &&
|
||||||
AsyncData.AsyncData<SA, SE>,
|
Option.isNone(validationFiber) &&
|
||||||
]) => Option.isSome(value) && Option.isNone(error) && Option.isNone(validationFiber) && !AsyncData.isLoading(submitState),
|
!AsyncData.isLoading(submitState)
|
||||||
|
),
|
||||||
filter => SubscribableInternal.make({
|
|
||||||
get: Effect.map(Effect.all([valueRef, errorRef, validationFiberRef, submitStateRef]), filter),
|
|
||||||
get changes() {
|
|
||||||
return Stream.map(
|
|
||||||
Stream.zipLatestAll(
|
|
||||||
valueRef.changes,
|
|
||||||
errorRef.changes,
|
|
||||||
validationFiberRef.changes,
|
|
||||||
submitStateRef.changes,
|
|
||||||
),
|
|
||||||
filter,
|
|
||||||
)
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
@@ -186,42 +172,20 @@ export const field = <A, I, R, SA, SE, SR, const P extends PropertyPath.Paths<No
|
|||||||
self: Form<A, I, R, SA, SE, SR>,
|
self: Form<A, I, R, SA, SE, SR>,
|
||||||
path: P,
|
path: P,
|
||||||
): FormField<PropertyPath.ValueFromPath<A, P>, PropertyPath.ValueFromPath<I, P>> => new FormFieldImpl(
|
): FormField<PropertyPath.ValueFromPath<A, P>, PropertyPath.ValueFromPath<I, P>> => new FormFieldImpl(
|
||||||
pipe(
|
Subscribable.mapEffect(self.valueRef, Option.match({
|
||||||
Option.match({
|
onSome: v => Option.map(PropertyPath.get(v, path), Option.some),
|
||||||
onSome: (v: A) => Option.map(PropertyPath.get(v, path), Option.some),
|
onNone: () => Option.some(Option.none()),
|
||||||
onNone: () => Option.some(Option.none()),
|
})),
|
||||||
}),
|
|
||||||
filter => SubscribableInternal.make({
|
|
||||||
get: Effect.flatMap(self.valueRef, filter),
|
|
||||||
get changes() { return Stream.flatMap(self.valueRef.changes, filter) },
|
|
||||||
}),
|
|
||||||
),
|
|
||||||
|
|
||||||
SubscriptionSubRef.makeFromPath(self.encodedValueRef, path),
|
SubscriptionSubRef.makeFromPath(self.encodedValueRef, path),
|
||||||
|
Subscribable.mapEffect(self.errorRef, Option.match({
|
||||||
SubscribableInternal.flatMapSubscriptionRef(self.errorRef, 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),
|
||||||
pipe(
|
Subscribable.map(self.submitStateRef, AsyncData.isLoading)
|
||||||
Option.isSome,
|
|
||||||
filter => SubscribableInternal.make({
|
|
||||||
get: Effect.map(self.validationFiberRef.get, filter),
|
|
||||||
get changes() { return Stream.map(self.validationFiberRef.changes, filter) },
|
|
||||||
}),
|
|
||||||
),
|
|
||||||
|
|
||||||
pipe(
|
|
||||||
AsyncData.isLoading,
|
|
||||||
filter => SubscribableInternal.make({
|
|
||||||
get: Effect.map(self.submitStateRef, filter),
|
|
||||||
get changes() { return Stream.map(self.submitStateRef.changes, filter) },
|
|
||||||
}),
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,32 +1,17 @@
|
|||||||
import { Effect, Effectable, Readable, Stream, Subscribable, type SubscriptionRef } from "effect"
|
import { Effect, Stream, Subscribable } from "effect"
|
||||||
|
|
||||||
|
|
||||||
class SubscribableImpl<A, E, R>
|
export const zipLatestAll = <T extends ReadonlyArray<Subscribable.Subscribable<any, any, any>>>(
|
||||||
extends Effectable.Class<A, E, R> implements Subscribable.Subscribable<A, E, R> {
|
...subscribables: T
|
||||||
readonly [Readable.TypeId]: Readable.TypeId = Readable.TypeId
|
): Subscribable.Subscribable<
|
||||||
readonly [Subscribable.TypeId]: Subscribable.TypeId = Subscribable.TypeId
|
[T[number]] extends [never]
|
||||||
|
? never
|
||||||
|
: { [K in keyof T]: T[K] extends Subscribable.Subscribable<infer A, infer _E, infer _R> ? A : never },
|
||||||
|
[T[number]] extends [never] ? never : T[number] extends Subscribable.Subscribable<infer _A, infer _E, infer _R> ? _E : never,
|
||||||
|
[T[number]] extends [never] ? never : T[number] extends Subscribable.Subscribable<infer _A, infer _E, infer _R> ? _R : never
|
||||||
|
> => Subscribable.make({
|
||||||
|
get: Effect.all(subscribables.map(v => v.get)),
|
||||||
|
changes: Stream.zipLatestAll(...subscribables.map(v => v.changes)),
|
||||||
|
}) as any
|
||||||
|
|
||||||
constructor(
|
export * from "effect/Subscribable"
|
||||||
readonly get: Effect.Effect<A, E, R>,
|
|
||||||
readonly changes: Stream.Stream<A, E, R>,
|
|
||||||
) {
|
|
||||||
super()
|
|
||||||
}
|
|
||||||
|
|
||||||
commit() {
|
|
||||||
return this.get
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export const make = <A, E, R>(values: {
|
|
||||||
readonly get: Effect.Effect<A, E, R>
|
|
||||||
readonly changes: Stream.Stream<A, E, R>
|
|
||||||
}): Subscribable.Subscribable<A, E, R> => new SubscribableImpl(values.get, values.changes)
|
|
||||||
|
|
||||||
export const flatMapSubscriptionRef = <A, B, E, R>(
|
|
||||||
ref: SubscriptionRef.SubscriptionRef<A>,
|
|
||||||
flatMap: (value: NoInfer<A>) => Effect.Effect<B, E, R>,
|
|
||||||
): Subscribable.Subscribable<B, E, R> => make({
|
|
||||||
get: Effect.flatMap(ref, flatMap),
|
|
||||||
get changes() { return Stream.flatMap(ref.changes, flatMap) },
|
|
||||||
})
|
|
||||||
|
|||||||
Reference in New Issue
Block a user