Compare commits
24 Commits
renovate/b
...
4f28153529
| Author | SHA1 | Date | |
|---|---|---|---|
| 4f28153529 | |||
|
|
312c103e71 | ||
|
|
a252cfec27 | ||
|
|
4a5f4c329d | ||
|
|
6f96608f64 | ||
| 0bc29b2cb9 | |||
|
|
8642619a6a | ||
|
|
e8b8df9449 | ||
|
|
3695128923 | ||
|
|
1f14e8be6b | ||
|
|
adc8835304 | ||
|
|
8b06c56ec0 | ||
|
|
003d2f19a2 | ||
|
|
15f6d695f8 | ||
|
|
64583601dc | ||
|
|
cf4ba5805f | ||
|
|
90db94e905 | ||
|
|
336ea67ea2 | ||
|
|
1af839f036 | ||
|
|
6bdf2a4d87 | ||
| 8d55a67e75 | |||
|
|
a1ec5c4781 | ||
| 756b652861 | |||
| 59f9358b9a |
6
bun.lock
6
bun.lock
@@ -5,7 +5,7 @@
|
|||||||
"name": "@effect-fc/monorepo",
|
"name": "@effect-fc/monorepo",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@biomejs/biome": "^2.2.5",
|
"@biomejs/biome": "^2.2.5",
|
||||||
"@effect/language-service": "^0.45.0",
|
"@effect/language-service": "^0.47.0",
|
||||||
"@types/bun": "^1.2.23",
|
"@types/bun": "^1.2.23",
|
||||||
"npm-check-updates": "^19.0.0",
|
"npm-check-updates": "^19.0.0",
|
||||||
"npm-sort": "^0.0.4",
|
"npm-sort": "^0.0.4",
|
||||||
@@ -15,7 +15,7 @@
|
|||||||
},
|
},
|
||||||
"packages/effect-fc": {
|
"packages/effect-fc": {
|
||||||
"name": "effect-fc",
|
"name": "effect-fc",
|
||||||
"version": "0.1.4",
|
"version": "0.1.5",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@typed/async-data": "^0.13.1",
|
"@typed/async-data": "^0.13.1",
|
||||||
},
|
},
|
||||||
@@ -135,7 +135,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.45.1", "", { "bin": { "effect-language-service": "cli.js" } }, "sha512-SEZ9TaVCpRKYumTQJPApg3os9O94bN2lCYQLgZbyK/xD+NSfYPPJZQ+6T5LkpcNgW8BRk1ACI7S1W2/noxm7Qg=="],
|
"@effect/language-service": ["@effect/language-service@0.47.1", "", { "bin": { "effect-language-service": "cli.js" } }, "sha512-g+abcBxQneS+mm1HnLA98WU2tuFyBlPaDLSOoc1GZ6MqoGyWICfvMaONeoqfmaOBSkVobFnOwvHEW5rXsPIE+Q=="],
|
||||||
|
|
||||||
"@effect/platform": ["@effect/platform@0.92.1", "", { "dependencies": { "find-my-way-ts": "^0.1.6", "msgpackr": "^1.11.4", "multipasta": "^0.2.7" }, "peerDependencies": { "effect": "^3.18.1" } }, "sha512-XXWCBVwyhaKZISN7aM1fv/3fWDGyxr84ObywnUrL8aHvJLoIeskWFAP/fqw3c5MFCrJ3ZV97RWLbv6JiBQugdg=="],
|
"@effect/platform": ["@effect/platform@0.92.1", "", { "dependencies": { "find-my-way-ts": "^0.1.6", "msgpackr": "^1.11.4", "multipasta": "^0.2.7" }, "peerDependencies": { "effect": "^3.18.1" } }, "sha512-XXWCBVwyhaKZISN7aM1fv/3fWDGyxr84ObywnUrL8aHvJLoIeskWFAP/fqw3c5MFCrJ3ZV97RWLbv6JiBQugdg=="],
|
||||||
|
|
||||||
|
|||||||
@@ -16,7 +16,7 @@
|
|||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@biomejs/biome": "^2.2.5",
|
"@biomejs/biome": "^2.2.5",
|
||||||
"@effect/language-service": "^0.45.0",
|
"@effect/language-service": "^0.47.0",
|
||||||
"@types/bun": "^1.2.23",
|
"@types/bun": "^1.2.23",
|
||||||
"npm-check-updates": "^19.0.0",
|
"npm-check-updates": "^19.0.0",
|
||||||
"npm-sort": "^0.0.4",
|
"npm-sort": "^0.0.4",
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
/** biome-ignore-all lint/complexity/noBannedTypes: {} is the default type for React props */
|
/** biome-ignore-all lint/complexity/noBannedTypes: {} is the default type for React props */
|
||||||
/** biome-ignore-all lint/complexity/useArrowFunction: necessary for class prototypes */
|
/** biome-ignore-all lint/complexity/useArrowFunction: necessary for class prototypes */
|
||||||
import { Context, Effect, Effectable, ExecutionStrategy, Function, Predicate, Runtime, Scope, Tracer, type Types, type Utils } from "effect"
|
import { Context, Effect, Effectable, ExecutionStrategy, Exit, Function, Layer, ManagedRuntime, Predicate, Ref, Runtime, Scope, Tracer, type Types, type Utils } from "effect"
|
||||||
import * as React from "react"
|
import * as React from "react"
|
||||||
import * as Hooks from "./Hooks/index.js"
|
|
||||||
import { Memoized } from "./index.js"
|
import { Memoized } from "./index.js"
|
||||||
|
|
||||||
|
|
||||||
@@ -46,6 +45,11 @@ export namespace Component {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface ScopeOptions {
|
||||||
|
readonly finalizerExecutionMode?: "sync" | "fork"
|
||||||
|
readonly finalizerExecutionStrategy?: ExecutionStrategy.ExecutionStrategy
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
const ComponentProto = Object.freeze({
|
const ComponentProto = Object.freeze({
|
||||||
...Effectable.CommitPrototype,
|
...Effectable.CommitPrototype,
|
||||||
@@ -60,7 +64,7 @@ const ComponentProto = Object.freeze({
|
|||||||
runtimeRef.current = yield* Effect.runtime<Exclude<R, Scope.Scope>>()
|
runtimeRef.current = yield* Effect.runtime<Exclude<R, Scope.Scope>>()
|
||||||
|
|
||||||
return React.useRef(function ScopeProvider(props: P) {
|
return React.useRef(function ScopeProvider(props: P) {
|
||||||
const scope = Runtime.runSync(runtimeRef.current)(Hooks.useScope(
|
const scope = Runtime.runSync(runtimeRef.current)(useScope(
|
||||||
Array.from(
|
Array.from(
|
||||||
Context.omit(...nonReactiveTags)(runtimeRef.current.context).unsafeMap.values()
|
Context.omit(...nonReactiveTags)(runtimeRef.current.context).unsafeMap.values()
|
||||||
),
|
),
|
||||||
@@ -91,7 +95,7 @@ const ComponentProto = Object.freeze({
|
|||||||
} as const)
|
} as const)
|
||||||
|
|
||||||
const defaultOptions = {
|
const defaultOptions = {
|
||||||
finalizerExecutionMode: "sync",
|
finalizerExecutionMode: "fork",
|
||||||
finalizerExecutionStrategy: ExecutionStrategy.sequential,
|
finalizerExecutionStrategy: ExecutionStrategy.sequential,
|
||||||
} as const
|
} as const
|
||||||
|
|
||||||
@@ -407,3 +411,179 @@ export const withRuntime: {
|
|||||||
props,
|
props,
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
|
export const useScope: {
|
||||||
|
(
|
||||||
|
deps: React.DependencyList,
|
||||||
|
options?: ScopeOptions,
|
||||||
|
): Effect.Effect<Scope.Scope>
|
||||||
|
} = Effect.fnUntraced(function*(deps, options) {
|
||||||
|
const runtime = yield* Effect.runtime()
|
||||||
|
|
||||||
|
// biome-ignore lint/correctness/useExhaustiveDependencies: no reactivity needed
|
||||||
|
const [isInitialRun, initialScope] = React.useMemo(() => Runtime.runSync(runtime)(Effect.all([
|
||||||
|
Ref.make(true),
|
||||||
|
Scope.make(options?.finalizerExecutionStrategy ?? ExecutionStrategy.sequential),
|
||||||
|
])), [])
|
||||||
|
const [scope, setScope] = React.useState(initialScope)
|
||||||
|
|
||||||
|
React.useEffect(() => Runtime.runSync(runtime)(
|
||||||
|
Effect.if(isInitialRun, {
|
||||||
|
onTrue: () => Effect.as(
|
||||||
|
Ref.set(isInitialRun, false),
|
||||||
|
() => closeScope(scope, runtime, options),
|
||||||
|
),
|
||||||
|
|
||||||
|
onFalse: () => Scope.make(options?.finalizerExecutionStrategy ?? ExecutionStrategy.sequential).pipe(
|
||||||
|
Effect.tap(scope => Effect.sync(() => setScope(scope))),
|
||||||
|
Effect.map(scope => () => closeScope(scope, runtime, options)),
|
||||||
|
),
|
||||||
|
})
|
||||||
|
// biome-ignore lint/correctness/useExhaustiveDependencies: use of React.DependencyList
|
||||||
|
), deps)
|
||||||
|
|
||||||
|
return scope
|
||||||
|
})
|
||||||
|
|
||||||
|
const closeScope = (
|
||||||
|
scope: Scope.CloseableScope,
|
||||||
|
runtime: Runtime.Runtime<never>,
|
||||||
|
options?: ScopeOptions,
|
||||||
|
) => {
|
||||||
|
switch (options?.finalizerExecutionMode ?? "sync") {
|
||||||
|
case "sync":
|
||||||
|
Runtime.runSync(runtime)(Scope.close(scope, Exit.void))
|
||||||
|
break
|
||||||
|
case "fork":
|
||||||
|
Runtime.runFork(runtime)(Scope.close(scope, Exit.void))
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const useOnMount: {
|
||||||
|
<A, E, R>(
|
||||||
|
f: () => Effect.Effect<A, E, R>
|
||||||
|
): Effect.Effect<A, E, R>
|
||||||
|
} = Effect.fnUntraced(function* <A, E, R>(
|
||||||
|
f: () => Effect.Effect<A, E, R>
|
||||||
|
) {
|
||||||
|
const runtime = yield* Effect.runtime<R>()
|
||||||
|
return yield* React.useState(() => Runtime.runSync(runtime)(Effect.cached(f())))[0]
|
||||||
|
})
|
||||||
|
|
||||||
|
export const useOnChange: {
|
||||||
|
<A, E, R>(
|
||||||
|
f: () => Effect.Effect<A, E, R>,
|
||||||
|
deps: React.DependencyList,
|
||||||
|
): Effect.Effect<A, E, R>
|
||||||
|
} = Effect.fnUntraced(function* <A, E, R>(
|
||||||
|
f: () => Effect.Effect<A, E, R>,
|
||||||
|
deps: React.DependencyList,
|
||||||
|
) {
|
||||||
|
const runtime = yield* Effect.runtime<R>()
|
||||||
|
// biome-ignore lint/correctness/useExhaustiveDependencies: use of React.DependencyList
|
||||||
|
return yield* React.useMemo(() => Runtime.runSync(runtime)(Effect.cached(f())), deps)
|
||||||
|
})
|
||||||
|
|
||||||
|
export const useReactEffect: {
|
||||||
|
<E, R>(
|
||||||
|
f: () => Effect.Effect<void, E, R>,
|
||||||
|
deps?: React.DependencyList,
|
||||||
|
options?: ScopeOptions,
|
||||||
|
): Effect.Effect<void, never, Exclude<R, Scope.Scope>>
|
||||||
|
} = Effect.fnUntraced(function* <E, R>(
|
||||||
|
f: () => Effect.Effect<void, E, R>,
|
||||||
|
deps?: React.DependencyList,
|
||||||
|
options?: ScopeOptions,
|
||||||
|
) {
|
||||||
|
const runtime = yield* Effect.runtime<Exclude<R, Scope.Scope>>()
|
||||||
|
|
||||||
|
React.useEffect(() => Effect.Do.pipe(
|
||||||
|
Effect.bind("scope", () => Scope.make(options?.finalizerExecutionStrategy ?? ExecutionStrategy.sequential)),
|
||||||
|
Effect.bind("exit", ({ scope }) => Effect.exit(Effect.provideService(f(), Scope.Scope, scope))),
|
||||||
|
Effect.map(({ scope }) =>
|
||||||
|
() => closeScope(scope, runtime, options)
|
||||||
|
),
|
||||||
|
Runtime.runSync(runtime),
|
||||||
|
// biome-ignore lint/correctness/useExhaustiveDependencies: use of React.DependencyList
|
||||||
|
), deps)
|
||||||
|
})
|
||||||
|
|
||||||
|
export const useReactLayoutEffect: {
|
||||||
|
<E, R>(
|
||||||
|
f: () => Effect.Effect<void, E, R>,
|
||||||
|
deps?: React.DependencyList,
|
||||||
|
options?: ScopeOptions,
|
||||||
|
): Effect.Effect<void, never, Exclude<R, Scope.Scope>>
|
||||||
|
} = Effect.fnUntraced(function* <E, R>(
|
||||||
|
f: () => Effect.Effect<void, E, R>,
|
||||||
|
deps?: React.DependencyList,
|
||||||
|
options?: ScopeOptions,
|
||||||
|
) {
|
||||||
|
const runtime = yield* Effect.runtime<Exclude<R, Scope.Scope>>()
|
||||||
|
|
||||||
|
React.useLayoutEffect(() => Effect.Do.pipe(
|
||||||
|
Effect.bind("scope", () => Scope.make(options?.finalizerExecutionStrategy ?? ExecutionStrategy.sequential)),
|
||||||
|
Effect.bind("exit", ({ scope }) => Effect.exit(Effect.provideService(f(), Scope.Scope, scope))),
|
||||||
|
Effect.map(({ scope }) =>
|
||||||
|
() => closeScope(scope, runtime, options)
|
||||||
|
),
|
||||||
|
Runtime.runSync(runtime),
|
||||||
|
// biome-ignore lint/correctness/useExhaustiveDependencies: use of React.DependencyList
|
||||||
|
), deps)
|
||||||
|
})
|
||||||
|
|
||||||
|
export const useCallbackSync: {
|
||||||
|
<Args extends unknown[], A, E, R>(
|
||||||
|
f: (...args: Args) => Effect.Effect<A, E, R>,
|
||||||
|
deps: React.DependencyList,
|
||||||
|
): Effect.Effect<(...args: Args) => A, never, R>
|
||||||
|
} = Effect.fnUntraced(function* <Args extends unknown[], A, E, R>(
|
||||||
|
f: (...args: Args) => Effect.Effect<A, E, R>,
|
||||||
|
deps: React.DependencyList,
|
||||||
|
) {
|
||||||
|
// biome-ignore lint/style/noNonNullAssertion: context initialization
|
||||||
|
const runtimeRef = React.useRef<Runtime.Runtime<R>>(null!)
|
||||||
|
runtimeRef.current = yield* Effect.runtime<R>()
|
||||||
|
|
||||||
|
// biome-ignore lint/correctness/useExhaustiveDependencies: use of React.DependencyList
|
||||||
|
return React.useCallback((...args: Args) => Runtime.runSync(runtimeRef.current)(f(...args)), deps)
|
||||||
|
})
|
||||||
|
|
||||||
|
export const useCallbackPromise: {
|
||||||
|
<Args extends unknown[], A, E, R>(
|
||||||
|
f: (...args: Args) => Effect.Effect<A, E, R>,
|
||||||
|
deps: React.DependencyList,
|
||||||
|
): Effect.Effect<(...args: Args) => Promise<A>, never, R>
|
||||||
|
} = Effect.fnUntraced(function* <Args extends unknown[], A, E, R>(
|
||||||
|
f: (...args: Args) => Effect.Effect<A, E, R>,
|
||||||
|
deps: React.DependencyList,
|
||||||
|
) {
|
||||||
|
// biome-ignore lint/style/noNonNullAssertion: context initialization
|
||||||
|
const runtimeRef = React.useRef<Runtime.Runtime<R>>(null!)
|
||||||
|
runtimeRef.current = yield* Effect.runtime<R>()
|
||||||
|
|
||||||
|
// biome-ignore lint/correctness/useExhaustiveDependencies: use of React.DependencyList
|
||||||
|
return React.useCallback((...args: Args) => Runtime.runPromise(runtimeRef.current)(f(...args)), deps)
|
||||||
|
})
|
||||||
|
|
||||||
|
export const useContext: {
|
||||||
|
<ROut, E, RIn>(
|
||||||
|
layer: Layer.Layer<ROut, E, RIn>,
|
||||||
|
options?: ScopeOptions,
|
||||||
|
): Effect.Effect<Context.Context<ROut>, E, RIn>
|
||||||
|
} = Effect.fnUntraced(function* <ROut, E, RIn>(
|
||||||
|
layer: Layer.Layer<ROut, E, RIn>,
|
||||||
|
options?: ScopeOptions,
|
||||||
|
) {
|
||||||
|
const scope = yield* useScope([layer], options)
|
||||||
|
|
||||||
|
return yield* useOnChange(() => Effect.context<RIn>().pipe(
|
||||||
|
Effect.map(context => ManagedRuntime.make(Layer.provide(layer, Layer.succeedContext(context)))),
|
||||||
|
Effect.tap(runtime => Effect.addFinalizer(() => runtime.disposeEffect)),
|
||||||
|
Effect.andThen(runtime => runtime.runtimeEffect),
|
||||||
|
Effect.andThen(runtime => runtime.context),
|
||||||
|
Effect.provideService(Scope.Scope, scope),
|
||||||
|
), [scope])
|
||||||
|
})
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
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, 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 type { NoSuchElementException } from "effect/Cause"
|
||||||
import * as React from "react"
|
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 PropertyPath from "./PropertyPath.js"
|
||||||
import * as Subscribable from "./Subscribable.js"
|
import * as Subscribable from "./Subscribable.js"
|
||||||
|
import * as SubscriptionRef from "./SubscriptionRef.js"
|
||||||
import * as SubscriptionSubRef from "./SubscriptionSubRef.js"
|
import * as SubscriptionSubRef from "./SubscriptionSubRef.js"
|
||||||
|
|
||||||
|
|
||||||
@@ -17,6 +18,7 @@ extends Pipeable.Pipeable {
|
|||||||
|
|
||||||
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 onSubmit: (value: NoInfer<A>) => Effect.Effect<SA, SE, SR>
|
||||||
|
readonly autosubmit: boolean
|
||||||
readonly debounce: Option.Option<Duration.DurationInput>
|
readonly debounce: Option.Option<Duration.DurationInput>
|
||||||
|
|
||||||
readonly valueRef: SubscriptionRef.SubscriptionRef<Option.Option<A>>
|
readonly valueRef: SubscriptionRef.SubscriptionRef<Option.Option<A>>
|
||||||
@@ -35,6 +37,7 @@ extends Pipeable.Class() implements Form<A, I, R, SA, SE, SR> {
|
|||||||
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 onSubmit: (value: NoInfer<A>) => Effect.Effect<SA, SE, SR>,
|
||||||
|
readonly autosubmit: boolean,
|
||||||
readonly debounce: Option.Option<Duration.DurationInput>,
|
readonly debounce: Option.Option<Duration.DurationInput>,
|
||||||
|
|
||||||
readonly valueRef: SubscriptionRef.SubscriptionRef<Option.Option<A>>,
|
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 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, 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 schema: Schema.Schema<A, I, R>
|
||||||
readonly initialEncodedValue: NoInfer<I>
|
readonly initialEncodedValue: NoInfer<I>
|
||||||
readonly onSubmit: (value: NoInfer<A>) => Effect.Effect<SA, SE, SR>,
|
readonly onSubmit: (
|
||||||
readonly debounce?: Duration.DurationInput,
|
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(
|
return new FormImpl(
|
||||||
options.schema,
|
options.schema,
|
||||||
options.onSubmit,
|
options.onSubmit,
|
||||||
|
options.autosubmit ?? false,
|
||||||
Option.fromNullable(options.debounce),
|
Option.fromNullable(options.debounce),
|
||||||
|
|
||||||
valueRef,
|
valueRef,
|
||||||
@@ -97,7 +105,7 @@ export const make: {
|
|||||||
|
|
||||||
export const run = <A, I, R, SA, SE, SR>(
|
export const run = <A, I, R, SA, SE, SR>(
|
||||||
self: Form<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(
|
self.encodedValueRef.changes.pipe(
|
||||||
Option.isSome(self.debounce) ? Stream.debounce(self.debounce.value) : identity
|
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,
|
onNone: () => Effect.void,
|
||||||
})),
|
})),
|
||||||
Effect.andThen(
|
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.andThen(Schema.decode(self.schema, { errors: "all" })(encodedValue)),
|
||||||
Effect.exit,
|
Effect.exit,
|
||||||
Effect.andThen(flow(
|
Effect.andThen(flow(
|
||||||
Exit.matchEffect({
|
Exit.matchEffect({
|
||||||
onSuccess: v => Effect.andThen(
|
onSuccess: v => Ref.set(self.valueRef, Option.some(v)).pipe(
|
||||||
SubscriptionRef.set(self.valueRef, Option.some(v)),
|
Effect.andThen(Ref.set(self.errorRef, Option.none())),
|
||||||
SubscriptionRef.set(self.errorRef, Option.none()),
|
Effect.as(Option.some(v)),
|
||||||
),
|
),
|
||||||
onFailure: c => Option.match(
|
onFailure: c => Chunk.findFirst(Cause.failures(c), e => e._tag === "ParseError").pipe(
|
||||||
Chunk.findFirst(Cause.failures(c), e => e._tag === "ParseError"),
|
Option.match({
|
||||||
{
|
onSome: e => Ref.set(self.errorRef, Option.some(e)),
|
||||||
onSome: e => SubscriptionRef.set(self.errorRef, Option.some(e)),
|
|
||||||
onNone: () => Effect.void,
|
onNone: () => Effect.void,
|
||||||
},
|
}),
|
||||||
|
Effect.as(Option.none<A>()),
|
||||||
),
|
),
|
||||||
}),
|
}),
|
||||||
Effect.uninterruptible,
|
Effect.uninterruptible,
|
||||||
)),
|
)),
|
||||||
Effect.scoped,
|
Effect.scoped,
|
||||||
|
|
||||||
|
Effect.andThen(value => Option.isSome(value) && self.autosubmit
|
||||||
|
? Effect.asVoid(Effect.forkScoped(submit(self)))
|
||||||
|
: Effect.void
|
||||||
|
),
|
||||||
Effect.forkScoped,
|
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 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> {}
|
extends make.Options<A, I, R, SA, SE, SR> {}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const service = <A, I = A, R = never, SA = void, SE = A, SR = never>(
|
export const service = <A, I = A, R = never, SA = void, SE = A, SR = never>(
|
||||||
options: service.Options<A, I, R, SA, SE, SR>
|
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),
|
make(options),
|
||||||
form => Effect.forkScoped(run(form)),
|
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 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>(
|
export const useSubmit = <A, I, R, SA, SE, SR>(
|
||||||
self: Form<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>>>,
|
() => Promise<Option.Option<AsyncData.AsyncData<SA, SE>>>,
|
||||||
never,
|
never,
|
||||||
SR
|
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>>>(
|
export const useField = <A, I, R, SA, SE, SR, const P extends PropertyPath.Paths<NoInfer<I>>>(
|
||||||
self: Form<A, I, R, SA, SE, SR>,
|
self: Form<A, I, R, SA, SE, SR>,
|
||||||
@@ -271,33 +266,34 @@ export const useInput: {
|
|||||||
<A, I>(
|
<A, I>(
|
||||||
field: FormField<A, I>,
|
field: FormField<A, I>,
|
||||||
options?: useInput.Options,
|
options?: useInput.Options,
|
||||||
): Effect.Effect<useInput.Result<I>, NoSuchElementException>
|
): Effect.Effect<useInput.Result<I>, NoSuchElementException, Scope.Scope>
|
||||||
} = Effect.fnUntraced(function* <A, I>(
|
} = Effect.fnUntraced(function* <A, I>(
|
||||||
field: FormField<A, I>,
|
field: FormField<A, I>,
|
||||||
options?: useInput.Options,
|
options?: useInput.Options,
|
||||||
) {
|
) {
|
||||||
const internalValueRef = yield* Hooks.useMemo(() => Effect.andThen(field.encodedValueRef, SubscriptionRef.make), [field])
|
const internalValueRef = yield* Component.useOnChange(() => Effect.tap(
|
||||||
const [value, setValue] = yield* Hooks.useRefState(internalValueRef)
|
Effect.andThen(field.encodedValueRef, SubscriptionRef.make),
|
||||||
|
internalValueRef => Effect.forkScoped(Effect.all([
|
||||||
yield* Hooks.useFork(() => Effect.all([
|
Stream.runForEach(
|
||||||
Stream.runForEach(
|
Stream.drop(field.encodedValueRef, 1),
|
||||||
Stream.drop(field.encodedValueRef, 1),
|
upstreamEncodedValue => Effect.whenEffect(
|
||||||
upstreamEncodedValue => Effect.whenEffect(
|
Ref.set(internalValueRef, upstreamEncodedValue),
|
||||||
Ref.set(internalValueRef, upstreamEncodedValue),
|
Effect.andThen(internalValueRef, internalValue => !Equal.equals(upstreamEncodedValue, internalValue)),
|
||||||
Effect.andThen(internalValueRef, internalValue => !Equal.equals(upstreamEncodedValue, internalValue)),
|
),
|
||||||
),
|
),
|
||||||
),
|
|
||||||
|
|
||||||
Stream.runForEach(
|
Stream.runForEach(
|
||||||
internalValueRef.changes.pipe(
|
internalValueRef.changes.pipe(
|
||||||
Stream.drop(1),
|
Stream.drop(1),
|
||||||
Stream.changesWith(Equal.equivalence()),
|
Stream.changesWith(Equal.equivalence()),
|
||||||
options?.debounce ? Stream.debounce(options.debounce) : identity,
|
options?.debounce ? Stream.debounce(options.debounce) : identity,
|
||||||
|
),
|
||||||
|
internalValue => Ref.set(field.encodedValueRef, internalValue),
|
||||||
),
|
),
|
||||||
internalValue => Ref.set(field.encodedValueRef, internalValue),
|
], { concurrency: "unbounded" })),
|
||||||
),
|
), [field, options?.debounce])
|
||||||
], { concurrency: "unbounded" }), [field, internalValueRef, options?.debounce])
|
|
||||||
|
|
||||||
|
const [value, setValue] = yield* SubscriptionRef.useSubscriptionRefState(internalValueRef)
|
||||||
return { value, setValue }
|
return { value, setValue }
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -316,55 +312,56 @@ export const useOptionalInput: {
|
|||||||
<A, I>(
|
<A, I>(
|
||||||
field: FormField<A, Option.Option<I>>,
|
field: FormField<A, Option.Option<I>>,
|
||||||
options: useOptionalInput.Options<I>,
|
options: useOptionalInput.Options<I>,
|
||||||
): Effect.Effect<useOptionalInput.Result<I>, NoSuchElementException>
|
): Effect.Effect<useOptionalInput.Result<I>, NoSuchElementException, Scope.Scope>
|
||||||
} = Effect.fnUntraced(function* <A, I>(
|
} = Effect.fnUntraced(function* <A, I>(
|
||||||
field: FormField<A, Option.Option<I>>,
|
field: FormField<A, Option.Option<I>>,
|
||||||
options: useOptionalInput.Options<I>,
|
options: useOptionalInput.Options<I>,
|
||||||
) {
|
) {
|
||||||
const [enabledRef, internalValueRef] = yield* Hooks.useMemo(() => Effect.andThen(
|
const [enabledRef, internalValueRef] = yield* Component.useOnChange(() => Effect.tap(
|
||||||
field.encodedValueRef,
|
Effect.andThen(
|
||||||
Option.match({
|
field.encodedValueRef,
|
||||||
onSome: v => Effect.all([SubscriptionRef.make(true), SubscriptionRef.make(v)]),
|
Option.match({
|
||||||
onNone: () => Effect.all([SubscriptionRef.make(false), SubscriptionRef.make(options.defaultValue)]),
|
onSome: v => Effect.all([SubscriptionRef.make(true), SubscriptionRef.make(v)]),
|
||||||
}),
|
onNone: () => Effect.all([SubscriptionRef.make(false), SubscriptionRef.make(options.defaultValue)]),
|
||||||
), [field])
|
}),
|
||||||
|
),
|
||||||
|
|
||||||
const [enabled, setEnabled] = yield* Hooks.useRefState(enabledRef)
|
([enabledRef, internalValueRef]) => Effect.forkScoped(Effect.all([
|
||||||
const [value, setValue] = yield* Hooks.useRefState(internalValueRef)
|
Stream.runForEach(
|
||||||
|
Stream.drop(field.encodedValueRef, 1),
|
||||||
|
|
||||||
yield* Hooks.useFork(() => Effect.all([
|
upstreamEncodedValue => Effect.whenEffect(
|
||||||
Stream.runForEach(
|
Option.match(upstreamEncodedValue, {
|
||||||
Stream.drop(field.encodedValueRef, 1),
|
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(
|
Effect.andThen(
|
||||||
Option.match(upstreamEncodedValue, {
|
Effect.all([enabledRef, internalValueRef]),
|
||||||
onSome: v => Effect.andThen(
|
([enabled, internalValue]) => !Equal.equals(upstreamEncodedValue, enabled ? Option.some(internalValue) : Option.none()),
|
||||||
Ref.set(enabledRef, true),
|
|
||||||
Ref.set(internalValueRef, v),
|
|
||||||
),
|
),
|
||||||
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(
|
Stream.runForEach(
|
||||||
enabledRef.changes.pipe(
|
enabledRef.changes.pipe(
|
||||||
Stream.zipLatest(internalValueRef.changes),
|
Stream.zipLatest(internalValueRef.changes),
|
||||||
Stream.drop(1),
|
Stream.drop(1),
|
||||||
Stream.changesWith(Equal.equivalence()),
|
Stream.changesWith(Equal.equivalence()),
|
||||||
options?.debounce ? Stream.debounce(options.debounce) : identity,
|
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, options.debounce])
|
||||||
], { concurrency: "unbounded" }), [field, enabledRef, internalValueRef, options.debounce])
|
|
||||||
|
|
||||||
|
const [enabled, setEnabled] = yield* SubscriptionRef.useSubscriptionRefState(enabledRef)
|
||||||
|
const [value, setValue] = yield* SubscriptionRef.useSubscriptionRefState(internalValueRef)
|
||||||
return { enabled, setEnabled, value, setValue }
|
return { enabled, setEnabled, value, setValue }
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,17 +1,47 @@
|
|||||||
import { Effect, Stream, Subscribable } from "effect"
|
import { Effect, Equivalence, pipe, type Scope, Stream, Subscribable } from "effect"
|
||||||
|
import * as React from "react"
|
||||||
|
import * as Component from "./Component.js"
|
||||||
|
|
||||||
|
|
||||||
export const zipLatestAll = <T extends ReadonlyArray<Subscribable.Subscribable<any, any, any>>>(
|
export const zipLatestAll = <const T extends readonly Subscribable.Subscribable<any, any, any>[]>(
|
||||||
...subscribables: T
|
...elements: T
|
||||||
): Subscribable.Subscribable<
|
): Subscribable.Subscribable<
|
||||||
[T[number]] extends [never]
|
[T[number]] extends [never]
|
||||||
? never
|
? never
|
||||||
: { [K in keyof T]: T[K] extends Subscribable.Subscribable<infer A, infer _E, infer _R> ? A : 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> ? E : never,
|
||||||
[T[number]] extends [never] ? never : T[number] extends Subscribable.Subscribable<infer _A, infer _E, infer _R> ? _R : never
|
[T[number]] extends [never] ? never : T[number] extends Subscribable.Subscribable<infer _A, infer _E, infer R> ? R : never
|
||||||
> => Subscribable.make({
|
> => Subscribable.make({
|
||||||
get: Effect.all(subscribables.map(v => v.get)),
|
get: Effect.all(elements.map(v => v.get)),
|
||||||
changes: Stream.zipLatestAll(...subscribables.map(v => v.changes)),
|
changes: Stream.zipLatestAll(...elements.map(v => v.changes)),
|
||||||
}) as any
|
}) as any
|
||||||
|
|
||||||
|
export const useSubscribables: {
|
||||||
|
<const T extends readonly Subscribable.Subscribable<any, any, any>[]>(
|
||||||
|
...elements: T
|
||||||
|
): Effect.Effect<
|
||||||
|
[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) | Scope.Scope
|
||||||
|
>
|
||||||
|
} = Effect.fnUntraced(function* <const T extends readonly Subscribable.Subscribable<any, any, any>[]>(
|
||||||
|
...elements: T
|
||||||
|
) {
|
||||||
|
const [reactStateValue, setReactStateValue] = React.useState(
|
||||||
|
yield* Component.useOnMount(() => Effect.all(elements.map(v => v.get)))
|
||||||
|
)
|
||||||
|
|
||||||
|
yield* Component.useOnChange(() => Effect.forkScoped(pipe(
|
||||||
|
elements.map(ref => Stream.changesWith(ref.changes, Equivalence.strict())),
|
||||||
|
streams => Stream.zipLatestAll(...streams),
|
||||||
|
Stream.runForEach(v =>
|
||||||
|
Effect.sync(() => setReactStateValue(v))
|
||||||
|
),
|
||||||
|
)), elements)
|
||||||
|
|
||||||
|
return reactStateValue as any
|
||||||
|
})
|
||||||
|
|
||||||
export * from "effect/Subscribable"
|
export * from "effect/Subscribable"
|
||||||
|
|||||||
43
packages/effect-fc/src/SubscriptionRef.ts
Normal file
43
packages/effect-fc/src/SubscriptionRef.ts
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
import { Effect, Equivalence, Ref, type Scope, Stream, SubscriptionRef } from "effect"
|
||||||
|
import * as React from "react"
|
||||||
|
import * as Component from "./Component.js"
|
||||||
|
import * as SetStateAction from "./SetStateAction.js"
|
||||||
|
|
||||||
|
|
||||||
|
export const useSubscriptionRefState: {
|
||||||
|
<A>(
|
||||||
|
ref: SubscriptionRef.SubscriptionRef<A>
|
||||||
|
): Effect.Effect<readonly [A, React.Dispatch<React.SetStateAction<A>>], never, Scope.Scope>
|
||||||
|
} = Effect.fnUntraced(function* <A>(ref: SubscriptionRef.SubscriptionRef<A>) {
|
||||||
|
const [reactStateValue, setReactStateValue] = React.useState(yield* Component.useOnMount(() => ref))
|
||||||
|
|
||||||
|
yield* Component.useOnChange(() => Effect.forkScoped(Stream.runForEach(
|
||||||
|
Stream.changesWith(ref.changes, Equivalence.strict()),
|
||||||
|
v => Effect.sync(() => setReactStateValue(v)),
|
||||||
|
)), [ref])
|
||||||
|
|
||||||
|
const setValue = yield* Component.useCallbackSync((setStateAction: React.SetStateAction<A>) =>
|
||||||
|
Effect.andThen(
|
||||||
|
Ref.updateAndGet(ref, prevState => SetStateAction.value(setStateAction, prevState)),
|
||||||
|
v => setReactStateValue(v),
|
||||||
|
),
|
||||||
|
[ref])
|
||||||
|
|
||||||
|
return [reactStateValue, setValue]
|
||||||
|
})
|
||||||
|
|
||||||
|
export const useSubscriptionRefFromState: {
|
||||||
|
<A>(state: readonly [A, React.Dispatch<React.SetStateAction<A>>]): Effect.Effect<SubscriptionRef.SubscriptionRef<A>, never, Scope.Scope>
|
||||||
|
} = Effect.fnUntraced(function*([value, setValue]) {
|
||||||
|
const ref = yield* Component.useOnMount(() => SubscriptionRef.make(value))
|
||||||
|
|
||||||
|
yield* Component.useOnChange(() => Effect.forkScoped(Stream.runForEach(
|
||||||
|
Stream.changesWith(ref.changes, Equivalence.strict()),
|
||||||
|
v => Effect.sync(() => setValue(v)),
|
||||||
|
)), [setValue])
|
||||||
|
yield* Component.useReactEffect(() => Ref.set(ref, value), [value])
|
||||||
|
|
||||||
|
return ref
|
||||||
|
})
|
||||||
|
|
||||||
|
export * from "effect/SubscriptionRef"
|
||||||
@@ -7,4 +7,5 @@ export * as PropertyPath from "./PropertyPath.js"
|
|||||||
export * as ReactRuntime from "./ReactRuntime.js"
|
export * as ReactRuntime from "./ReactRuntime.js"
|
||||||
export * as SetStateAction from "./SetStateAction.js"
|
export * as SetStateAction from "./SetStateAction.js"
|
||||||
export * as Subscribable from "./Subscribable.js"
|
export * as Subscribable from "./Subscribable.js"
|
||||||
|
export * as SubscriptionRef from "./SubscriptionRef.js"
|
||||||
export * as SubscriptionSubRef from "./SubscriptionSubRef.js"
|
export * as SubscriptionSubRef from "./SubscriptionSubRef.js"
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { assertEncodedJsonifiable } from "@/lib/schema"
|
|
||||||
import { Schema } from "effect"
|
import { Schema } from "effect"
|
||||||
|
import { assertEncodedJsonifiable } from "@/lib/schema"
|
||||||
|
|
||||||
|
|
||||||
export class Todo extends Schema.Class<Todo>("Todo")({
|
export class Todo extends Schema.Class<Todo>("Todo")({
|
||||||
|
|||||||
@@ -1,41 +0,0 @@
|
|||||||
/** biome-ignore-all lint/correctness/useHookAtTopLevel: effect-fc HOC */
|
|
||||||
import { Callout, Flex, TextArea, type TextAreaProps } from "@radix-ui/themes"
|
|
||||||
import { Array, type Equivalence, Option, ParseResult, type Schema, Struct } from "effect"
|
|
||||||
import { Component } from "effect-fc"
|
|
||||||
import { useInput } from "effect-fc/Hooks"
|
|
||||||
import * as React from "react"
|
|
||||||
|
|
||||||
|
|
||||||
export type TextAreaInputProps<A, R> = Omit<useInput.Options<A, R>, "schema" | "equivalence"> & Omit<TextAreaProps, "ref">
|
|
||||||
|
|
||||||
export const TextAreaInput = <A, R>(options: {
|
|
||||||
readonly schema: Schema.Schema<A, string, R>
|
|
||||||
readonly equivalence?: Equivalence.Equivalence<A>
|
|
||||||
}): Component.Component<
|
|
||||||
TextAreaInputProps<A, R>,
|
|
||||||
React.JSX.Element,
|
|
||||||
ParseResult.ParseError,
|
|
||||||
R
|
|
||||||
> => Component.makeUntraced("TextFieldInput")(function*(props) {
|
|
||||||
const input = yield* useInput({ ...options, ...props })
|
|
||||||
const issue = React.useMemo(() => input.error.pipe(
|
|
||||||
Option.map(ParseResult.ArrayFormatter.formatErrorSync),
|
|
||||||
Option.flatMap(Array.head),
|
|
||||||
), [input.error])
|
|
||||||
|
|
||||||
return (
|
|
||||||
<Flex direction="column" gap="1">
|
|
||||||
<TextArea
|
|
||||||
value={input.value}
|
|
||||||
onChange={e => input.setValue(e.target.value)}
|
|
||||||
{...Struct.omit(props, "ref")}
|
|
||||||
/>
|
|
||||||
|
|
||||||
{Option.isSome(issue) &&
|
|
||||||
<Callout.Root color="red" role="alert">
|
|
||||||
<Callout.Text>{issue.value.message}</Callout.Text>
|
|
||||||
</Callout.Root>
|
|
||||||
}
|
|
||||||
</Flex>
|
|
||||||
)
|
|
||||||
})
|
|
||||||
@@ -1,69 +0,0 @@
|
|||||||
/** biome-ignore-all lint/correctness/useHookAtTopLevel: effect-fc HOC */
|
|
||||||
import { Callout, Checkbox, Flex, TextField } from "@radix-ui/themes"
|
|
||||||
import { Array, type Equivalence, Option, ParseResult, type Schema, Struct } from "effect"
|
|
||||||
import { Component } from "effect-fc"
|
|
||||||
import { useInput, useOptionalInput } from "effect-fc/Hooks"
|
|
||||||
import * as React from "react"
|
|
||||||
|
|
||||||
|
|
||||||
export type TextFieldInputProps<A, R> = (
|
|
||||||
& Omit<useInput.Options<A, R>, "schema" | "equivalence">
|
|
||||||
& Omit<TextField.RootProps, "ref">
|
|
||||||
)
|
|
||||||
export type TextFieldOptionalInputProps<A, R> = (
|
|
||||||
& Omit<useOptionalInput.Options<A, R>, "schema" | "equivalence">
|
|
||||||
& Omit<TextField.RootProps, "ref" | "defaultValue">
|
|
||||||
)
|
|
||||||
|
|
||||||
export const TextFieldInput = <A, R, O extends boolean = false>(options: {
|
|
||||||
readonly optional?: O
|
|
||||||
readonly schema: Schema.Schema<A, string, R>
|
|
||||||
readonly equivalence?: Equivalence.Equivalence<A>
|
|
||||||
}) => Component.makeUntraced("TextFieldInput")(function*(props: O extends true
|
|
||||||
? TextFieldOptionalInputProps<A, R>
|
|
||||||
: TextFieldInputProps<A, R>
|
|
||||||
) {
|
|
||||||
const input: (
|
|
||||||
| { readonly optional: true } & useOptionalInput.Result
|
|
||||||
| { readonly optional: false } & useInput.Result
|
|
||||||
) = options.optional
|
|
||||||
? {
|
|
||||||
optional: true,
|
|
||||||
...yield* useOptionalInput({ ...options, ...props as TextFieldOptionalInputProps<A, R> }),
|
|
||||||
}
|
|
||||||
: {
|
|
||||||
optional: false,
|
|
||||||
...yield* useInput({ ...options, ...props as TextFieldInputProps<A, R> }),
|
|
||||||
}
|
|
||||||
|
|
||||||
const issue = React.useMemo(() => input.error.pipe(
|
|
||||||
Option.map(ParseResult.ArrayFormatter.formatErrorSync),
|
|
||||||
Option.flatMap(Array.head),
|
|
||||||
), [input.error])
|
|
||||||
|
|
||||||
return (
|
|
||||||
<Flex direction="column" gap="1">
|
|
||||||
<Flex direction="row" align="center" gap="1">
|
|
||||||
{input.optional &&
|
|
||||||
<Checkbox
|
|
||||||
checked={input.enabled}
|
|
||||||
onCheckedChange={checked => input.setEnabled(checked !== "indeterminate" && checked)}
|
|
||||||
/>
|
|
||||||
}
|
|
||||||
|
|
||||||
<TextField.Root
|
|
||||||
value={input.value}
|
|
||||||
onChange={e => input.setValue(e.target.value)}
|
|
||||||
disabled={input.optional ? !input.enabled : undefined}
|
|
||||||
{...Struct.omit(props as TextFieldOptionalInputProps<A, R> | TextFieldInputProps<A, R>, "ref", "defaultValue")}
|
|
||||||
/>
|
|
||||||
</Flex>
|
|
||||||
|
|
||||||
{(!(input.optional && !input.enabled) && Option.isSome(issue)) &&
|
|
||||||
<Callout.Root color="red" role="alert">
|
|
||||||
<Callout.Text>{issue.value.message}</Callout.Text>
|
|
||||||
</Callout.Root>
|
|
||||||
}
|
|
||||||
</Flex>
|
|
||||||
)
|
|
||||||
})
|
|
||||||
@@ -13,7 +13,6 @@ import { Route as FormRouteImport } from './routes/form'
|
|||||||
import { Route as BlankRouteImport } from './routes/blank'
|
import { Route as BlankRouteImport } from './routes/blank'
|
||||||
import { Route as IndexRouteImport } from './routes/index'
|
import { Route as IndexRouteImport } from './routes/index'
|
||||||
import { Route as DevMemoRouteImport } from './routes/dev/memo'
|
import { Route as DevMemoRouteImport } from './routes/dev/memo'
|
||||||
import { Route as DevInputRouteImport } from './routes/dev/input'
|
|
||||||
import { Route as DevAsyncRenderingRouteImport } from './routes/dev/async-rendering'
|
import { Route as DevAsyncRenderingRouteImport } from './routes/dev/async-rendering'
|
||||||
|
|
||||||
const FormRoute = FormRouteImport.update({
|
const FormRoute = FormRouteImport.update({
|
||||||
@@ -36,11 +35,6 @@ const DevMemoRoute = DevMemoRouteImport.update({
|
|||||||
path: '/dev/memo',
|
path: '/dev/memo',
|
||||||
getParentRoute: () => rootRouteImport,
|
getParentRoute: () => rootRouteImport,
|
||||||
} as any)
|
} as any)
|
||||||
const DevInputRoute = DevInputRouteImport.update({
|
|
||||||
id: '/dev/input',
|
|
||||||
path: '/dev/input',
|
|
||||||
getParentRoute: () => rootRouteImport,
|
|
||||||
} as any)
|
|
||||||
const DevAsyncRenderingRoute = DevAsyncRenderingRouteImport.update({
|
const DevAsyncRenderingRoute = DevAsyncRenderingRouteImport.update({
|
||||||
id: '/dev/async-rendering',
|
id: '/dev/async-rendering',
|
||||||
path: '/dev/async-rendering',
|
path: '/dev/async-rendering',
|
||||||
@@ -52,7 +46,6 @@ export interface FileRoutesByFullPath {
|
|||||||
'/blank': typeof BlankRoute
|
'/blank': typeof BlankRoute
|
||||||
'/form': typeof FormRoute
|
'/form': typeof FormRoute
|
||||||
'/dev/async-rendering': typeof DevAsyncRenderingRoute
|
'/dev/async-rendering': typeof DevAsyncRenderingRoute
|
||||||
'/dev/input': typeof DevInputRoute
|
|
||||||
'/dev/memo': typeof DevMemoRoute
|
'/dev/memo': typeof DevMemoRoute
|
||||||
}
|
}
|
||||||
export interface FileRoutesByTo {
|
export interface FileRoutesByTo {
|
||||||
@@ -60,7 +53,6 @@ export interface FileRoutesByTo {
|
|||||||
'/blank': typeof BlankRoute
|
'/blank': typeof BlankRoute
|
||||||
'/form': typeof FormRoute
|
'/form': typeof FormRoute
|
||||||
'/dev/async-rendering': typeof DevAsyncRenderingRoute
|
'/dev/async-rendering': typeof DevAsyncRenderingRoute
|
||||||
'/dev/input': typeof DevInputRoute
|
|
||||||
'/dev/memo': typeof DevMemoRoute
|
'/dev/memo': typeof DevMemoRoute
|
||||||
}
|
}
|
||||||
export interface FileRoutesById {
|
export interface FileRoutesById {
|
||||||
@@ -69,33 +61,19 @@ export interface FileRoutesById {
|
|||||||
'/blank': typeof BlankRoute
|
'/blank': typeof BlankRoute
|
||||||
'/form': typeof FormRoute
|
'/form': typeof FormRoute
|
||||||
'/dev/async-rendering': typeof DevAsyncRenderingRoute
|
'/dev/async-rendering': typeof DevAsyncRenderingRoute
|
||||||
'/dev/input': typeof DevInputRoute
|
|
||||||
'/dev/memo': typeof DevMemoRoute
|
'/dev/memo': typeof DevMemoRoute
|
||||||
}
|
}
|
||||||
export interface FileRouteTypes {
|
export interface FileRouteTypes {
|
||||||
fileRoutesByFullPath: FileRoutesByFullPath
|
fileRoutesByFullPath: FileRoutesByFullPath
|
||||||
fullPaths:
|
fullPaths: '/' | '/blank' | '/form' | '/dev/async-rendering' | '/dev/memo'
|
||||||
| '/'
|
|
||||||
| '/blank'
|
|
||||||
| '/form'
|
|
||||||
| '/dev/async-rendering'
|
|
||||||
| '/dev/input'
|
|
||||||
| '/dev/memo'
|
|
||||||
fileRoutesByTo: FileRoutesByTo
|
fileRoutesByTo: FileRoutesByTo
|
||||||
to:
|
to: '/' | '/blank' | '/form' | '/dev/async-rendering' | '/dev/memo'
|
||||||
| '/'
|
|
||||||
| '/blank'
|
|
||||||
| '/form'
|
|
||||||
| '/dev/async-rendering'
|
|
||||||
| '/dev/input'
|
|
||||||
| '/dev/memo'
|
|
||||||
id:
|
id:
|
||||||
| '__root__'
|
| '__root__'
|
||||||
| '/'
|
| '/'
|
||||||
| '/blank'
|
| '/blank'
|
||||||
| '/form'
|
| '/form'
|
||||||
| '/dev/async-rendering'
|
| '/dev/async-rendering'
|
||||||
| '/dev/input'
|
|
||||||
| '/dev/memo'
|
| '/dev/memo'
|
||||||
fileRoutesById: FileRoutesById
|
fileRoutesById: FileRoutesById
|
||||||
}
|
}
|
||||||
@@ -104,7 +82,6 @@ export interface RootRouteChildren {
|
|||||||
BlankRoute: typeof BlankRoute
|
BlankRoute: typeof BlankRoute
|
||||||
FormRoute: typeof FormRoute
|
FormRoute: typeof FormRoute
|
||||||
DevAsyncRenderingRoute: typeof DevAsyncRenderingRoute
|
DevAsyncRenderingRoute: typeof DevAsyncRenderingRoute
|
||||||
DevInputRoute: typeof DevInputRoute
|
|
||||||
DevMemoRoute: typeof DevMemoRoute
|
DevMemoRoute: typeof DevMemoRoute
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -138,13 +115,6 @@ declare module '@tanstack/react-router' {
|
|||||||
preLoaderRoute: typeof DevMemoRouteImport
|
preLoaderRoute: typeof DevMemoRouteImport
|
||||||
parentRoute: typeof rootRouteImport
|
parentRoute: typeof rootRouteImport
|
||||||
}
|
}
|
||||||
'/dev/input': {
|
|
||||||
id: '/dev/input'
|
|
||||||
path: '/dev/input'
|
|
||||||
fullPath: '/dev/input'
|
|
||||||
preLoaderRoute: typeof DevInputRouteImport
|
|
||||||
parentRoute: typeof rootRouteImport
|
|
||||||
}
|
|
||||||
'/dev/async-rendering': {
|
'/dev/async-rendering': {
|
||||||
id: '/dev/async-rendering'
|
id: '/dev/async-rendering'
|
||||||
path: '/dev/async-rendering'
|
path: '/dev/async-rendering'
|
||||||
@@ -160,7 +130,6 @@ const rootRouteChildren: RootRouteChildren = {
|
|||||||
BlankRoute: BlankRoute,
|
BlankRoute: BlankRoute,
|
||||||
FormRoute: FormRoute,
|
FormRoute: FormRoute,
|
||||||
DevAsyncRenderingRoute: DevAsyncRenderingRoute,
|
DevAsyncRenderingRoute: DevAsyncRenderingRoute,
|
||||||
DevInputRoute: DevInputRoute,
|
|
||||||
DevMemoRoute: DevMemoRoute,
|
DevMemoRoute: DevMemoRoute,
|
||||||
}
|
}
|
||||||
export const routeTree = rootRouteImport
|
export const routeTree = rootRouteImport
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import { Flex, Text, TextField } from "@radix-ui/themes"
|
|||||||
import { createFileRoute } from "@tanstack/react-router"
|
import { createFileRoute } from "@tanstack/react-router"
|
||||||
import { GetRandomValues, makeUuid4 } from "@typed/id"
|
import { GetRandomValues, makeUuid4 } from "@typed/id"
|
||||||
import { Effect } from "effect"
|
import { Effect } from "effect"
|
||||||
import { Async, Component, Hooks, Memoized } from "effect-fc"
|
import { Async, Component, Memoized } from "effect-fc"
|
||||||
import * as React from "react"
|
import * as React from "react"
|
||||||
import { runtime } from "@/runtime"
|
import { runtime } from "@/runtime"
|
||||||
|
|
||||||
@@ -69,7 +69,7 @@ class AsyncComponent extends Component.makeUntraced("AsyncComponent")(function*(
|
|||||||
class MemoizedAsyncComponent extends Memoized.memoized(AsyncComponent) {}
|
class MemoizedAsyncComponent extends Memoized.memoized(AsyncComponent) {}
|
||||||
|
|
||||||
class SubComponent extends Component.makeUntraced("SubComponent")(function*() {
|
class SubComponent extends Component.makeUntraced("SubComponent")(function*() {
|
||||||
const [state] = React.useState(yield* Hooks.useOnce(() => Effect.provide(makeUuid4, GetRandomValues.CryptoRandom)))
|
const [state] = React.useState(yield* Component.useOnMount(() => Effect.provide(makeUuid4, GetRandomValues.CryptoRandom)))
|
||||||
return <Text>{state}</Text>
|
return <Text>{state}</Text>
|
||||||
}) {}
|
}) {}
|
||||||
|
|
||||||
|
|||||||
@@ -1,41 +0,0 @@
|
|||||||
import { Container } from "@radix-ui/themes"
|
|
||||||
import { createFileRoute } from "@tanstack/react-router"
|
|
||||||
import { Schema, SubscriptionRef } from "effect"
|
|
||||||
import { Component, Hooks, Memoized } from "effect-fc"
|
|
||||||
import { TextFieldInput } from "@/lib/input/TextFieldInput"
|
|
||||||
import { runtime } from "@/runtime"
|
|
||||||
|
|
||||||
|
|
||||||
const IntFromString = Schema.NumberFromString.pipe(Schema.int())
|
|
||||||
|
|
||||||
const IntTextFieldInput = TextFieldInput({ schema: IntFromString })
|
|
||||||
const StringTextFieldInput = TextFieldInput({ schema: Schema.String })
|
|
||||||
|
|
||||||
const Input = Component.makeUntraced("Input")(function*() {
|
|
||||||
const IntTextFieldInputFC = yield* IntTextFieldInput
|
|
||||||
const StringTextFieldInputFC = yield* StringTextFieldInput
|
|
||||||
|
|
||||||
const intRef1 = yield* Hooks.useOnce(() => SubscriptionRef.make(0))
|
|
||||||
// const intRef2 = yield* useOnce(() => SubscriptionRef.make(0))
|
|
||||||
const stringRef = yield* Hooks.useOnce(() => SubscriptionRef.make(""))
|
|
||||||
// yield* useFork(() => Stream.runForEach(intRef1.changes, Console.log), [intRef1])
|
|
||||||
|
|
||||||
// const input2 = yield* useInput({ schema: IntFromString, ref: intRef2 })
|
|
||||||
|
|
||||||
// const [str, setStr] = yield* useRefState(stringRef)
|
|
||||||
|
|
||||||
return (
|
|
||||||
<Container>
|
|
||||||
<IntTextFieldInputFC ref={intRef1} />
|
|
||||||
<StringTextFieldInputFC ref={stringRef} />
|
|
||||||
<StringTextFieldInputFC ref={stringRef} />
|
|
||||||
</Container>
|
|
||||||
)
|
|
||||||
}).pipe(
|
|
||||||
Memoized.memoized,
|
|
||||||
Component.withRuntime(runtime.context)
|
|
||||||
)
|
|
||||||
|
|
||||||
export const Route = createFileRoute("/dev/input")({
|
|
||||||
component: Input,
|
|
||||||
})
|
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
import { Button, Container, Flex } from "@radix-ui/themes"
|
import { Button, Container, Flex } from "@radix-ui/themes"
|
||||||
import { createFileRoute } from "@tanstack/react-router"
|
import { createFileRoute } from "@tanstack/react-router"
|
||||||
import { Console, Effect, Option, ParseResult, Schema } from "effect"
|
import { Console, Effect, Option, ParseResult, Schema } from "effect"
|
||||||
import { Component, Form, Hooks } from "effect-fc"
|
import { Component, Form, Subscribable } from "effect-fc"
|
||||||
import { TextFieldFormInput } from "@/lib/form/TextFieldFormInput"
|
import { TextFieldFormInput } from "@/lib/form/TextFieldFormInput"
|
||||||
import { DateTimeUtcFromZonedInput } from "@/lib/schema"
|
import { DateTimeUtcFromZonedInput } from "@/lib/schema"
|
||||||
import { runtime } from "@/runtime"
|
import { runtime } from "@/runtime"
|
||||||
@@ -50,7 +50,7 @@ class RegisterForm extends Effect.Service<RegisterForm>()("RegisterForm", {
|
|||||||
class RegisterFormView extends Component.makeUntraced("RegisterFormView")(function*() {
|
class RegisterFormView extends Component.makeUntraced("RegisterFormView")(function*() {
|
||||||
const form = yield* RegisterForm
|
const form = yield* RegisterForm
|
||||||
const submit = yield* Form.useSubmit(form)
|
const submit = yield* Form.useSubmit(form)
|
||||||
const [canSubmit] = yield* Hooks.useSubscribables(form.canSubmitSubscribable)
|
const [canSubmit] = yield* Subscribable.useSubscribables(form.canSubmitSubscribable)
|
||||||
|
|
||||||
const TextFieldFormInputFC = yield* TextFieldFormInput
|
const TextFieldFormInputFC = yield* TextFieldFormInput
|
||||||
|
|
||||||
@@ -87,7 +87,7 @@ class RegisterFormView extends Component.makeUntraced("RegisterFormView")(functi
|
|||||||
const RegisterPage = Component.makeUntraced("RegisterPage")(function*() {
|
const RegisterPage = Component.makeUntraced("RegisterPage")(function*() {
|
||||||
const RegisterFormViewFC = yield* Effect.provide(
|
const RegisterFormViewFC = yield* Effect.provide(
|
||||||
RegisterFormView,
|
RegisterFormView,
|
||||||
yield* Hooks.useContext(RegisterForm.Default, { finalizerExecutionMode: "fork" }),
|
yield* Component.useContext(RegisterForm.Default, { finalizerExecutionMode: "fork" }),
|
||||||
)
|
)
|
||||||
|
|
||||||
return <RegisterFormViewFC />
|
return <RegisterFormViewFC />
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { createFileRoute } from "@tanstack/react-router"
|
import { createFileRoute } from "@tanstack/react-router"
|
||||||
import { Effect } from "effect"
|
import { Effect } from "effect"
|
||||||
import { Component, Hooks } from "effect-fc"
|
import { Component } from "effect-fc"
|
||||||
import { runtime } from "@/runtime"
|
import { runtime } from "@/runtime"
|
||||||
import { Todos } from "@/todo/Todos"
|
import { Todos } from "@/todo/Todos"
|
||||||
import { TodosState } from "@/todo/TodosState.service"
|
import { TodosState } from "@/todo/TodosState.service"
|
||||||
@@ -11,7 +11,7 @@ const TodosStateLive = TodosState.Default("todos")
|
|||||||
const Index = Component.makeUntraced("Index")(function*() {
|
const Index = Component.makeUntraced("Index")(function*() {
|
||||||
const TodosFC = yield* Effect.provide(
|
const TodosFC = yield* Effect.provide(
|
||||||
Todos,
|
Todos,
|
||||||
yield* Hooks.useContext(TodosStateLive, { finalizerExecutionMode: "fork" }),
|
yield* Component.useContext(TodosStateLive, { finalizerExecutionMode: "fork" }),
|
||||||
)
|
)
|
||||||
|
|
||||||
return <TodosFC />
|
return <TodosFC />
|
||||||
|
|||||||
@@ -1,18 +1,19 @@
|
|||||||
import { Box, Button, Flex, IconButton } from "@radix-ui/themes"
|
import { Box, Button, Flex, IconButton } from "@radix-ui/themes"
|
||||||
import { GetRandomValues, makeUuid4 } from "@typed/id"
|
import { GetRandomValues, makeUuid4 } from "@typed/id"
|
||||||
import { Chunk, DateTime, Effect, Match, Option, Ref, Runtime, Schema, Stream, SubscriptionRef } from "effect"
|
import { Chunk, Effect, Match, Option, Ref, Runtime, Schema, Stream } from "effect"
|
||||||
import { Component, Hooks, Memoized, Subscribable, SubscriptionSubRef } from "effect-fc"
|
import { Component, Form, Subscribable } from "effect-fc"
|
||||||
import { FaArrowDown, FaArrowUp } from "react-icons/fa"
|
import { FaArrowDown, FaArrowUp } from "react-icons/fa"
|
||||||
import { FaDeleteLeft } from "react-icons/fa6"
|
import { FaDeleteLeft } from "react-icons/fa6"
|
||||||
import * as Domain from "@/domain"
|
import * as Domain from "@/domain"
|
||||||
import { TextAreaInput } from "@/lib/input/TextAreaInput"
|
import { TextFieldFormInput } from "@/lib/form/TextFieldFormInput"
|
||||||
import { TextFieldInput } from "@/lib/input/TextFieldInput"
|
|
||||||
import { DateTimeUtcFromZonedInput } from "@/lib/schema"
|
import { DateTimeUtcFromZonedInput } from "@/lib/schema"
|
||||||
import { TodosState } from "./TodosState.service"
|
import { TodosState } from "./TodosState.service"
|
||||||
|
|
||||||
|
|
||||||
const StringTextAreaInput = TextAreaInput({ schema: Schema.String })
|
const TodoFormSchema = Schema.compose(Schema.Struct({
|
||||||
const OptionalDateTimeInput = TextFieldInput({ optional: true, schema: DateTimeUtcFromZonedInput })
|
...Domain.Todo.Todo.fields,
|
||||||
|
completedAt: Schema.OptionFromSelf(DateTimeUtcFromZonedInput),
|
||||||
|
}), Domain.Todo.Todo)
|
||||||
|
|
||||||
const makeTodo = makeUuid4.pipe(
|
const makeTodo = makeUuid4.pipe(
|
||||||
Effect.map(id => Domain.Todo.Todo.make({
|
Effect.map(id => Domain.Todo.Todo.make({
|
||||||
@@ -33,49 +34,71 @@ export class Todo extends Component.makeUntraced("Todo")(function*(props: TodoPr
|
|||||||
const runtime = yield* Effect.runtime()
|
const runtime = yield* Effect.runtime()
|
||||||
const state = yield* TodosState
|
const state = yield* TodosState
|
||||||
|
|
||||||
const { ref, indexRef, contentRef, completedAtRef } = yield* Hooks.useMemo(() => Match.value(props).pipe(
|
const [
|
||||||
Match.tag("new", () => Effect.Do.pipe(
|
indexRef,
|
||||||
Effect.bind("ref", () => Effect.andThen(makeTodo, SubscriptionRef.make)),
|
form,
|
||||||
Effect.let("indexRef", () => Subscribable.make({ get: Effect.succeed(-1), changes: Stream.empty })),
|
contentField,
|
||||||
)),
|
completedAtField,
|
||||||
Match.tag("edit", ({ id }) => Effect.Do.pipe(
|
] = yield* Component.useOnChange(() => Effect.gen(function*() {
|
||||||
Effect.let("ref", () => state.getElementRef(id)),
|
const indexRef = Match.value(props).pipe(
|
||||||
Effect.let("indexRef", () => state.getIndexSubscribable(id)),
|
Match.tag("new", () => Subscribable.make({ get: Effect.succeed(-1), changes: Stream.empty })),
|
||||||
)),
|
Match.tag("edit", ({ id }) => state.getIndexSubscribable(id)),
|
||||||
Match.exhaustive,
|
Match.exhaustive,
|
||||||
|
)
|
||||||
|
|
||||||
Effect.let("contentRef", ({ ref }) => SubscriptionSubRef.makeFromPath(ref, ["content"])),
|
const form = yield* Form.service({
|
||||||
Effect.let("completedAtRef", ({ ref }) => SubscriptionSubRef.makeFromPath(ref, ["completedAt"])),
|
schema: TodoFormSchema,
|
||||||
), [props._tag, props._tag === "edit" ? props.id : undefined])
|
initialEncodedValue: yield* Schema.encode(TodoFormSchema)(
|
||||||
|
yield* Match.value(props).pipe(
|
||||||
|
Match.tag("new", () => makeTodo),
|
||||||
|
Match.tag("edit", ({ id }) => state.getElementRef(id)),
|
||||||
|
Match.exhaustive,
|
||||||
|
)
|
||||||
|
),
|
||||||
|
onSubmit: function(todo) {
|
||||||
|
return Match.value(props).pipe(
|
||||||
|
Match.tag("new", () => Ref.update(state.ref, Chunk.prepend(todo)).pipe(
|
||||||
|
Effect.andThen(makeTodo),
|
||||||
|
Effect.andThen(Schema.encode(TodoFormSchema)),
|
||||||
|
Effect.andThen(v => Ref.set(this.encodedValueRef, v)),
|
||||||
|
)),
|
||||||
|
Match.tag("edit", ({ id }) => Ref.set(state.getElementRef(id), todo)),
|
||||||
|
Match.exhaustive,
|
||||||
|
)
|
||||||
|
},
|
||||||
|
autosubmit: props._tag === "edit",
|
||||||
|
debounce: "250 millis",
|
||||||
|
})
|
||||||
|
|
||||||
const [index, size] = yield* Hooks.useSubscribables(indexRef, state.sizeSubscribable)
|
return [
|
||||||
|
indexRef,
|
||||||
|
form,
|
||||||
|
Form.field(form, ["content"]),
|
||||||
|
Form.field(form, ["completedAt"]),
|
||||||
|
] as const
|
||||||
|
}), [props._tag, props._tag === "edit" ? props.id : undefined])
|
||||||
|
|
||||||
const StringTextAreaInputFC = yield* StringTextAreaInput
|
const [index, size, canSubmit] = yield* Subscribable.useSubscribables(indexRef, state.sizeSubscribable, form.canSubmitSubscribable)
|
||||||
const OptionalDateTimeInputFC = yield* OptionalDateTimeInput
|
const submit = yield* Form.useSubmit(form)
|
||||||
|
const TextFieldFormInputFC = yield* TextFieldFormInput
|
||||||
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Flex direction="row" align="center" gap="2">
|
<Flex direction="row" align="center" gap="2">
|
||||||
<Box flexGrow="1">
|
<Box flexGrow="1">
|
||||||
<Flex direction="column" align="stretch" gap="2">
|
<Flex direction="column" align="stretch" gap="2">
|
||||||
<StringTextAreaInputFC ref={contentRef} />
|
<TextFieldFormInputFC field={contentField} />
|
||||||
|
|
||||||
<Flex direction="row" justify="center" align="center" gap="2">
|
<Flex direction="row" justify="center" align="center" gap="2">
|
||||||
<OptionalDateTimeInputFC
|
<TextFieldFormInputFC
|
||||||
|
optional
|
||||||
|
field={completedAtField}
|
||||||
type="datetime-local"
|
type="datetime-local"
|
||||||
ref={completedAtRef}
|
defaultValue=""
|
||||||
defaultValue={yield* Hooks.useOnce(() => DateTime.now)}
|
|
||||||
/>
|
/>
|
||||||
|
|
||||||
{props._tag === "new" &&
|
{props._tag === "new" &&
|
||||||
<Button
|
<Button disabled={!canSubmit} onClick={() => submit()}>
|
||||||
onClick={() => ref.pipe(
|
|
||||||
Effect.andThen(todo => Ref.update(state.ref, Chunk.prepend(todo))),
|
|
||||||
Effect.andThen(makeTodo),
|
|
||||||
Effect.andThen(todo => Ref.set(ref, todo)),
|
|
||||||
Runtime.runSync(runtime),
|
|
||||||
)}
|
|
||||||
>
|
|
||||||
Add
|
Add
|
||||||
</Button>
|
</Button>
|
||||||
}
|
}
|
||||||
@@ -106,6 +129,4 @@ export class Todo extends Component.makeUntraced("Todo")(function*(props: TodoPr
|
|||||||
}
|
}
|
||||||
</Flex>
|
</Flex>
|
||||||
)
|
)
|
||||||
}).pipe(
|
}) {}
|
||||||
Memoized.memoized
|
|
||||||
) {}
|
|
||||||
|
|||||||
@@ -1,15 +1,15 @@
|
|||||||
import { Container, Flex, Heading } from "@radix-ui/themes"
|
import { Container, Flex, Heading } from "@radix-ui/themes"
|
||||||
import { Chunk, Console, Effect } from "effect"
|
import { Chunk, Console, Effect } from "effect"
|
||||||
import { Component, Hooks } from "effect-fc"
|
import { Component, Subscribable } from "effect-fc"
|
||||||
import { Todo } from "./Todo"
|
import { Todo } from "./Todo"
|
||||||
import { TodosState } from "./TodosState.service"
|
import { TodosState } from "./TodosState.service"
|
||||||
|
|
||||||
|
|
||||||
export class Todos extends Component.makeUntraced("Todos")(function*() {
|
export class Todos extends Component.makeUntraced("Todos")(function*() {
|
||||||
const state = yield* TodosState
|
const state = yield* TodosState
|
||||||
const [todos] = yield* Hooks.useSubscribables(state.ref)
|
const [todos] = yield* Subscribable.useSubscribables(state.ref)
|
||||||
|
|
||||||
yield* Hooks.useOnce(() => Effect.andThen(
|
yield* Component.useOnMount(() => Effect.andThen(
|
||||||
Console.log("Todos mounted"),
|
Console.log("Todos mounted"),
|
||||||
Effect.addFinalizer(() => Console.log("Todos unmounted")),
|
Effect.addFinalizer(() => Console.log("Todos unmounted")),
|
||||||
))
|
))
|
||||||
|
|||||||
Reference in New Issue
Block a user