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