0.1.0 #1

Merged
Thilawyn merged 87 commits from next into master 2025-01-18 00:54:42 +01:00
Showing only changes of commit 53232729c3 - Show all commits

View File

@@ -1,5 +1,6 @@
import { Effect, Exit, ManagedRuntime } from "effect"
import { createContext, useContext, type Context, type ReactNode } from "react"
import { Effect, Exit, Fiber, ManagedRuntime, Stream, SubscriptionRef } from "effect"
import type { RunForkOptions } from "effect/Runtime"
import React, { useMemo } from "react"
export class Reffuse<R, ER> {
@@ -7,12 +8,12 @@ export class Reffuse<R, ER> {
constructor(
private readonly runtime: ManagedRuntime.ManagedRuntime<R, ER>
) {
this.Context = createContext(runtime)
this.Context = React.createContext(runtime)
}
readonly Context: Context<ManagedRuntime.ManagedRuntime<R, ER>>
readonly Context: React.Context<ManagedRuntime.ManagedRuntime<R, ER>>
Provider(props: { readonly children: ReactNode }) {
Provider(props: { readonly children: React.ReactNode }) {
return this.Context.Provider({
...props,
value: this.runtime,
@@ -21,7 +22,7 @@ export class Reffuse<R, ER> {
useRuntime(): ManagedRuntime.ManagedRuntime<R, ER> {
return useContext(this.Context)
return React.useContext(this.Context)
}
@@ -47,4 +48,34 @@ export class Reffuse<R, ER> {
return this.useRuntime().runPromiseExit(effect, options)
}
runFork<A, E>(
self: Effect.Effect<A, E, R>,
options?: RunForkOptions,
): Fiber.RuntimeFiber<A, ER | E> {
return this.useRuntime().runFork(self, options)
}
useFork<A, E>(
self: Effect.Effect<A, E, R>,
deps?: React.DependencyList,
options?: RunForkOptions,
): void {
return React.useEffect(() => {
const fiber = this.runFork(self, options)
return () => { this.runFork(Fiber.interrupt(fiber)) }
}, deps)
}
useRefState<A>(ref: SubscriptionRef.SubscriptionRef<A>) {
const initial = useMemo(() => this.runSync(ref), [ref])
const [value, setValueInternal] = React.useState(initial)
this.useFork(Stream.runForEach(ref.changes, v => Effect.sync(() =>
setValueInternal(v)
)))
return []
}
}