Compare commits
1 Commits
9c0892078a
...
4f28153529
| Author | SHA1 | Date | |
|---|---|---|---|
| 4f28153529 |
4
bun.lock
4
bun.lock
@@ -5,7 +5,7 @@
|
||||
"name": "@effect-fc/monorepo",
|
||||
"devDependencies": {
|
||||
"@biomejs/biome": "^2.2.5",
|
||||
"@effect/language-service": "^0.48.0",
|
||||
"@effect/language-service": "^0.47.0",
|
||||
"@types/bun": "^1.2.23",
|
||||
"npm-check-updates": "^19.0.0",
|
||||
"npm-sort": "^0.0.4",
|
||||
@@ -135,7 +135,7 @@
|
||||
|
||||
"@effect-fc/example": ["@effect-fc/example@workspace:packages/example"],
|
||||
|
||||
"@effect/language-service": ["@effect/language-service@0.48.0", "", { "bin": { "effect-language-service": "cli.js" } }, "sha512-u7DTPoGFFeDGSdomjY5C2nCGNWSisxpYSqHp3dlSG8kCZh5cay+166bveHRYvuJSJS5yomdkPTJwjwrqMmT7Og=="],
|
||||
"@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=="],
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"@biomejs/biome": "^2.2.5",
|
||||
"@effect/language-service": "^0.48.0",
|
||||
"@effect/language-service": "^0.47.0",
|
||||
"@types/bun": "^1.2.23",
|
||||
"npm-check-updates": "^19.0.0",
|
||||
"npm-sort": "^0.0.4",
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
/** biome-ignore-all lint/complexity/useArrowFunction: necessary for class prototypes */
|
||||
import { Effect, Function, Predicate, Runtime, Scope } from "effect"
|
||||
import * as React from "react"
|
||||
import * as Component from "./Component.js"
|
||||
import type * as Component from "./Component.js"
|
||||
|
||||
|
||||
export const TypeId: unique symbol = Symbol.for("effect-fc/Async/Async")
|
||||
export const TypeId: unique symbol = Symbol.for("effect-fc/Async")
|
||||
export type TypeId = typeof TypeId
|
||||
|
||||
export interface Async extends Async.Options {
|
||||
@@ -26,15 +26,13 @@ const SuspenseProto = Object.freeze({
|
||||
makeFunctionComponent<P extends {}, A extends React.ReactNode, E, R>(
|
||||
this: Component.Component<P, A, E, R> & Async,
|
||||
runtimeRef: React.RefObject<Runtime.Runtime<Exclude<R, Scope.Scope>>>,
|
||||
scope: Scope.Scope,
|
||||
) {
|
||||
const SuspenseInner = (props: { readonly promise: Promise<React.ReactNode> }) => React.use(props.promise)
|
||||
|
||||
return ({ fallback, name, ...props }: Async.Props) => {
|
||||
const promise = Runtime.runPromise(runtimeRef.current)(
|
||||
Effect.andThen(
|
||||
Component.useScope([], this),
|
||||
scope => Effect.provideService(this.body(props as P), Scope.Scope, scope),
|
||||
)
|
||||
Effect.provideService(this.body(props as P), Scope.Scope, scope)
|
||||
)
|
||||
|
||||
return React.createElement(
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
/** biome-ignore-all lint/complexity/noBannedTypes: {} is the default type for React props */
|
||||
/** biome-ignore-all lint/complexity/useArrowFunction: necessary for class prototypes */
|
||||
import { Context, Effect, Effectable, Equivalence, ExecutionStrategy, Exit, Fiber, Function, HashMap, Layer, ManagedRuntime, Option, Predicate, Ref, 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 { Memoized } from "./index.js"
|
||||
|
||||
|
||||
export const TypeId: unique symbol = Symbol.for("effect-fc/Component/Component")
|
||||
export const TypeId: unique symbol = Symbol.for("effect-fc/Component")
|
||||
export type TypeId = typeof TypeId
|
||||
|
||||
export interface Component<P extends {}, A extends React.ReactNode, E, R>
|
||||
@@ -25,7 +25,8 @@ extends
|
||||
|
||||
/** @internal */
|
||||
makeFunctionComponent(
|
||||
runtimeRef: React.Ref<Runtime.Runtime<Exclude<R, Scope.Scope>>>
|
||||
runtimeRef: React.Ref<Runtime.Runtime<Exclude<R, Scope.Scope>>>,
|
||||
scope: Scope.Scope,
|
||||
): (props: P) => A
|
||||
}
|
||||
|
||||
@@ -57,33 +58,38 @@ const ComponentProto = Object.freeze({
|
||||
commit: Effect.fnUntraced(function* <P extends {}, A extends React.ReactNode, E, R>(
|
||||
this: Component<P, A, E, R>
|
||||
) {
|
||||
const self = this
|
||||
// biome-ignore lint/style/noNonNullAssertion: React ref initialization
|
||||
const runtimeRef = React.useRef<Runtime.Runtime<Exclude<R, Scope.Scope>>>(null!)
|
||||
runtimeRef.current = yield* Effect.runtime<Exclude<R, Scope.Scope>>()
|
||||
|
||||
return yield* React.useState(() => Runtime.runSync(runtimeRef.current)(Effect.cachedFunction(
|
||||
(_services: readonly any[]) => Effect.sync(() => {
|
||||
const f: React.FC<P> = this.makeFunctionComponent(runtimeRef)
|
||||
f.displayName = this.displayName ?? "Anonymous"
|
||||
return Memoized.isMemoized(this)
|
||||
? React.memo(f, this.propsAreEqual)
|
||||
return React.useRef(function ScopeProvider(props: P) {
|
||||
const scope = Runtime.runSync(runtimeRef.current)(useScope(
|
||||
Array.from(
|
||||
Context.omit(...nonReactiveTags)(runtimeRef.current.context).unsafeMap.values()
|
||||
),
|
||||
self,
|
||||
))
|
||||
|
||||
const FC = React.useMemo(() => {
|
||||
const f: React.FC<P> = self.makeFunctionComponent(runtimeRef, scope)
|
||||
f.displayName = self.displayName ?? "Anonymous"
|
||||
return Memoized.isMemoized(self)
|
||||
? React.memo(f, self.propsAreEqual)
|
||||
: f
|
||||
}),
|
||||
Equivalence.array(Equivalence.strict()),
|
||||
)))[0](Array.from(
|
||||
Context.omit(...nonReactiveTags)(runtimeRef.current.context).unsafeMap.values()
|
||||
))
|
||||
}, [scope])
|
||||
|
||||
return React.createElement(FC, props)
|
||||
}).current
|
||||
}),
|
||||
|
||||
makeFunctionComponent<P extends {}, A extends React.ReactNode, E, R>(
|
||||
this: Component<P, A, E, R>,
|
||||
runtimeRef: React.RefObject<Runtime.Runtime<Exclude<R, Scope.Scope>>>,
|
||||
scope: Scope.Scope,
|
||||
) {
|
||||
return (props: P) => Runtime.runSync(runtimeRef.current)(
|
||||
Effect.andThen(
|
||||
useScope([], this),
|
||||
scope => Effect.provideService(this.body(props), Scope.Scope, scope),
|
||||
)
|
||||
Effect.provideService(this.body(props), Scope.Scope, scope)
|
||||
)
|
||||
},
|
||||
} as const)
|
||||
@@ -407,69 +413,35 @@ export const withRuntime: {
|
||||
})
|
||||
|
||||
|
||||
export class ScopeMap extends Effect.Service<ScopeMap>()("effect-fc/Component/ScopeMap", {
|
||||
effect: Effect.bind(Effect.Do, "ref", () => Ref.make(HashMap.empty<string, ScopeMap.Entry>()))
|
||||
}) {}
|
||||
|
||||
export namespace ScopeMap {
|
||||
export interface Entry {
|
||||
readonly scope: Scope.CloseableScope
|
||||
readonly closeFiber: Option.Option<Fiber.RuntimeFiber<void>>
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export const useScope: {
|
||||
(
|
||||
deps: React.DependencyList,
|
||||
options?: ScopeOptions,
|
||||
): Effect.Effect<Scope.Scope>
|
||||
} = Effect.fnUntraced(function*(deps, options) {
|
||||
// biome-ignore lint/style/noNonNullAssertion: context initialization
|
||||
const runtimeRef = React.useRef<Runtime.Runtime<never>>(null!)
|
||||
runtimeRef.current = yield* Effect.runtime()
|
||||
const runtime = yield* Effect.runtime()
|
||||
|
||||
const key = React.useId()
|
||||
const scopeMap = yield* ScopeMap as unknown as Effect.Effect<ScopeMap>
|
||||
// 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)
|
||||
|
||||
const scope = React.useMemo(() => Runtime.runSync(runtimeRef.current)(Effect.andThen(
|
||||
scopeMap.ref,
|
||||
map => Option.match(HashMap.get(map, key), {
|
||||
onSome: entry => Effect.succeed(entry.scope),
|
||||
onNone: () => Effect.tap(
|
||||
Scope.make(options?.finalizerExecutionStrategy ?? ExecutionStrategy.sequential),
|
||||
scope => Ref.update(scopeMap.ref, HashMap.set(key, {
|
||||
scope,
|
||||
closeFiber: Option.none(),
|
||||
}))
|
||||
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)
|
||||
|
||||
// biome-ignore lint/correctness/useExhaustiveDependencies: only reactive on "scope"
|
||||
React.useEffect(() => Runtime.runSync(runtimeRef.current)(scopeMap.ref.pipe(
|
||||
Effect.andThen(HashMap.get(key)),
|
||||
Effect.tap(entry => Option.match(entry.closeFiber, {
|
||||
onSome: fiber => Effect.andThen(
|
||||
Ref.update(scopeMap.ref, HashMap.set(key, { ...entry, closeFiber: Option.none() })),
|
||||
Fiber.interruptFork(fiber),
|
||||
),
|
||||
onNone: () => Effect.void,
|
||||
})),
|
||||
Effect.map(({ scope }) =>
|
||||
() => Runtime.runSync(runtimeRef.current)(Effect.andThen(
|
||||
Effect.forkDaemon(Effect.sleep("100 millis").pipe(
|
||||
Effect.andThen(Scope.close(scope, Exit.void)),
|
||||
Effect.andThen(Ref.update(scopeMap.ref, HashMap.remove(key))),
|
||||
)),
|
||||
fiber => Ref.update(scopeMap.ref, HashMap.set(key, {
|
||||
scope,
|
||||
closeFiber: Option.some(fiber),
|
||||
})),
|
||||
))
|
||||
),
|
||||
)), [scope])
|
||||
), deps)
|
||||
|
||||
return scope
|
||||
})
|
||||
|
||||
@@ -9,7 +9,7 @@ import * as SubscriptionRef from "./SubscriptionRef.js"
|
||||
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")
|
||||
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>
|
||||
|
||||
@@ -3,7 +3,7 @@ import { type Equivalence, Function, Predicate } from "effect"
|
||||
import type * as Component from "./Component.js"
|
||||
|
||||
|
||||
export const TypeId: unique symbol = Symbol.for("effect-fc/Memoized/Memoized")
|
||||
export const TypeId: unique symbol = Symbol.for("effect-fc/Memoized")
|
||||
export type TypeId = typeof TypeId
|
||||
|
||||
export interface Memoized<P> extends Memoized.Options<P> {
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
/** biome-ignore-all lint/complexity/useArrowFunction: necessary for class prototypes */
|
||||
import { Effect, Layer, ManagedRuntime, Predicate, type Runtime } from "effect"
|
||||
import { Effect, type Layer, ManagedRuntime, Predicate, type Runtime } from "effect"
|
||||
import * as React from "react"
|
||||
import * as Component from "./Component.js"
|
||||
|
||||
|
||||
export const TypeId: unique symbol = Symbol.for("effect-fc/ReactRuntime/ReactRuntime")
|
||||
export const TypeId: unique symbol = Symbol.for("effect-fc/ReactRuntime")
|
||||
export type TypeId = typeof TypeId
|
||||
|
||||
export interface ReactRuntime<R, ER> {
|
||||
@@ -22,12 +21,9 @@ export const isReactRuntime = (u: unknown): u is ReactRuntime<unknown, unknown>
|
||||
export const make = <R, ER>(
|
||||
layer: Layer.Layer<R, ER>,
|
||||
memoMap?: Layer.MemoMap,
|
||||
): ReactRuntime<R | Component.ScopeMap, ER> => Object.setPrototypeOf(
|
||||
): ReactRuntime<R, ER> => Object.setPrototypeOf(
|
||||
Object.assign(function() {}, {
|
||||
runtime: ManagedRuntime.make(
|
||||
Layer.merge(layer, Component.ScopeMap.Default),
|
||||
memoMap,
|
||||
),
|
||||
runtime: ManagedRuntime.make(layer, memoMap),
|
||||
// biome-ignore lint/style/noNonNullAssertion: context initialization
|
||||
context: React.createContext<Runtime.Runtime<R>>(null!),
|
||||
}),
|
||||
|
||||
@@ -2,7 +2,7 @@ import { Chunk, Effect, Effectable, Option, Predicate, Readable, Ref, Stream, Su
|
||||
import * as PropertyPath from "./PropertyPath.js"
|
||||
|
||||
|
||||
export const SubscriptionSubRefTypeId: unique symbol = Symbol.for("effect-fc/SubscriptionSubRef/SubscriptionSubRef")
|
||||
export const SubscriptionSubRefTypeId: unique symbol = Symbol.for("effect-fc/SubscriptionSubRef")
|
||||
export type SubscriptionSubRefTypeId = typeof SubscriptionSubRefTypeId
|
||||
|
||||
export interface SubscriptionSubRef<in out A, in out B extends SubscriptionRef.SubscriptionRef<any>>
|
||||
|
||||
@@ -54,11 +54,6 @@ class RegisterFormView extends Component.makeUntraced("RegisterFormView")(functi
|
||||
|
||||
const TextFieldFormInputFC = yield* TextFieldFormInput
|
||||
|
||||
yield* Component.useOnMount(() => Effect.gen(function*() {
|
||||
yield* Effect.addFinalizer(() => Console.log("RegisterFormView unmounted"))
|
||||
yield* Console.log("RegisterFormView mounted")
|
||||
}))
|
||||
|
||||
|
||||
return (
|
||||
<Container width="300">
|
||||
@@ -92,7 +87,7 @@ class RegisterFormView extends Component.makeUntraced("RegisterFormView")(functi
|
||||
const RegisterPage = Component.makeUntraced("RegisterPage")(function*() {
|
||||
const RegisterFormViewFC = yield* Effect.provide(
|
||||
RegisterFormView,
|
||||
yield* Component.useContext(RegisterForm.Default),
|
||||
yield* Component.useContext(RegisterForm.Default, { finalizerExecutionMode: "fork" }),
|
||||
)
|
||||
|
||||
return <RegisterFormViewFC />
|
||||
|
||||
@@ -11,7 +11,7 @@ const TodosStateLive = TodosState.Default("todos")
|
||||
const Index = Component.makeUntraced("Index")(function*() {
|
||||
const TodosFC = yield* Effect.provide(
|
||||
Todos,
|
||||
yield* Component.useContext(TodosStateLive),
|
||||
yield* Component.useContext(TodosStateLive, { finalizerExecutionMode: "fork" }),
|
||||
)
|
||||
|
||||
return <TodosFC />
|
||||
|
||||
Reference in New Issue
Block a user