@@ -1,5 +1,3 @@
|
||||
import { Console } from "effect"
|
||||
import { useState } from "react"
|
||||
import "./App.css"
|
||||
import reactLogo from "./assets/react.svg"
|
||||
import { Reffuse } from "./Reffuse"
|
||||
@@ -8,10 +6,7 @@ import viteLogo from "/vite.svg"
|
||||
|
||||
export function App() {
|
||||
|
||||
Reffuse.runSync(Console.log("test"))
|
||||
|
||||
// const [count, setCount] = Reffuse.useRefState(Reffuse.useRef(0))
|
||||
const [count, setCount] = useState(0)
|
||||
const [count, setCount] = Reffuse.useRefState(Reffuse.useRef(0))
|
||||
|
||||
|
||||
return <>
|
||||
|
||||
@@ -5,7 +5,7 @@ import React from "react"
|
||||
export class Reffuse<R, ER> {
|
||||
|
||||
constructor(
|
||||
private readonly runtime: ManagedRuntime.ManagedRuntime<R, ER>
|
||||
runtime: ManagedRuntime.ManagedRuntime<R, ER>
|
||||
) {
|
||||
this.Context = React.createContext(runtime)
|
||||
|
||||
@@ -61,19 +61,24 @@ export class Reffuse<R, ER> {
|
||||
deps?: React.DependencyList,
|
||||
options?: Runtime.RunForkOptions,
|
||||
): void {
|
||||
const runtime = this.useRuntime()
|
||||
|
||||
return React.useEffect(() => {
|
||||
const fiber = this.runFork(self.pipe(Effect.scoped), options)
|
||||
return () => { this.runFork(Fiber.interrupt(fiber)) }
|
||||
const fiber = runtime.runFork(self.pipe(Effect.scoped), options)
|
||||
return () => { runtime.runFork(Fiber.interrupt(fiber)) }
|
||||
}, deps)
|
||||
}
|
||||
|
||||
|
||||
useRef<A>(value: A): SubscriptionRef.SubscriptionRef<A> {
|
||||
return React.useMemo(() => this.runSync(SubscriptionRef.make(value)), [])
|
||||
const runtime = this.useRuntime()
|
||||
return React.useMemo(() => runtime.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 runtime = this.useRuntime()
|
||||
|
||||
const initialState = React.useMemo(() => runtime.runSync(ref), [ref])
|
||||
const [reactStateValue, setReactStateValue] = React.useState(initialState)
|
||||
|
||||
this.useFork(Stream.runForEach(ref.changes, v => Effect.sync(() =>
|
||||
@@ -81,7 +86,7 @@ export class Reffuse<R, ER> {
|
||||
)), [ref])
|
||||
|
||||
const setValue = React.useCallback((setStateAction: React.SetStateAction<A>) =>
|
||||
this.runSync(Ref.update(ref, previousState =>
|
||||
runtime.runSync(Ref.update(ref, previousState =>
|
||||
typeof setStateAction === "function"
|
||||
? (setStateAction as (prevState: A) => A)(previousState)
|
||||
: setStateAction
|
||||
|
||||
Reference in New Issue
Block a user