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 c91b538c97 - Show all commits

View File

@@ -1,6 +1,6 @@
import { Effect, Exit, Fiber, ManagedRuntime, Stream, SubscriptionRef } from "effect"
import { Effect, Exit, Fiber, ManagedRuntime, Ref, Stream, SubscriptionRef } from "effect"
import type { RunForkOptions } from "effect/Runtime"
import React, { useMemo } from "react"
import React from "react"
export class Reffuse<R, ER> {
@@ -9,17 +9,16 @@ export class Reffuse<R, ER> {
private readonly runtime: ManagedRuntime.ManagedRuntime<R, ER>
) {
this.Context = React.createContext(runtime)
}
readonly Context: React.Context<ManagedRuntime.ManagedRuntime<R, ER>>
Provider(props: { readonly children: React.ReactNode }) {
return this.Context.Provider({
this.Provider = (props: { readonly children?: React.ReactNode }) => this.Context.Provider({
...props,
value: this.runtime,
})
}
readonly Context: React.Context<ManagedRuntime.ManagedRuntime<R, ER>>
readonly Provider: React.FC<{ readonly children?: React.ReactNode }>
useRuntime(): ManagedRuntime.ManagedRuntime<R, ER> {
return React.useContext(this.Context)
@@ -62,20 +61,33 @@ export class Reffuse<R, ER> {
options?: RunForkOptions,
): void {
return React.useEffect(() => {
const fiber = this.runFork(self, options)
const fiber = this.runFork(self.pipe(Effect.scoped), 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)
useRef<A>(value: A): SubscriptionRef.SubscriptionRef<A> {
return React.useMemo(() => this.runSync(SubscriptionRef.make(value)), [])
}
useRefState<A>(ref: SubscriptionRef.SubscriptionRef<A>): [A, React.Dispatch<React.SetStateAction<A>>] {
const initialState = React.useMemo(() => this.runSync(ref), [ref])
const [reactStateValue, setReactStateValue] = React.useState(initialState)
this.useFork(Stream.runForEach(ref.changes, v => Effect.sync(() =>
setValueInternal(v)
)))
setReactStateValue(v)
)), [ref])
return []
const setValue = React.useCallback((setStateAction: React.SetStateAction<A>) =>
this.runSync(Ref.update(ref, previousState =>
typeof setStateAction === "function"
? (setStateAction as (prevState: A) => A)(previousState)
: setStateAction
)),
[ref])
return [reactStateValue, setValue]
}
}