0.1.0 #1
@@ -1,5 +1,6 @@
|
|||||||
import { Effect, Exit, ManagedRuntime } from "effect"
|
import { Effect, Exit, Fiber, ManagedRuntime, Stream, SubscriptionRef } from "effect"
|
||||||
import { createContext, useContext, type Context, type ReactNode } from "react"
|
import type { RunForkOptions } from "effect/Runtime"
|
||||||
|
import React, { useMemo } from "react"
|
||||||
|
|
||||||
|
|
||||||
export class Reffuse<R, ER> {
|
export class Reffuse<R, ER> {
|
||||||
@@ -7,12 +8,12 @@ export class Reffuse<R, ER> {
|
|||||||
constructor(
|
constructor(
|
||||||
private readonly runtime: ManagedRuntime.ManagedRuntime<R, ER>
|
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({
|
return this.Context.Provider({
|
||||||
...props,
|
...props,
|
||||||
value: this.runtime,
|
value: this.runtime,
|
||||||
@@ -21,7 +22,7 @@ export class Reffuse<R, ER> {
|
|||||||
|
|
||||||
|
|
||||||
useRuntime(): ManagedRuntime.ManagedRuntime<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)
|
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 []
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user