0.1.8 #11
1
bun.lock
1
bun.lock
@@ -45,6 +45,7 @@
|
|||||||
"name": "@reffuse/extension-lazyref",
|
"name": "@reffuse/extension-lazyref",
|
||||||
"version": "0.1.0",
|
"version": "0.1.0",
|
||||||
"peerDependencies": {
|
"peerDependencies": {
|
||||||
|
"@typed/lazy-ref": "^0.3.3",
|
||||||
"@types/react": "^19.0.0",
|
"@types/react": "^19.0.0",
|
||||||
"effect": "^3.13.0",
|
"effect": "^3.13.0",
|
||||||
"react": "^19.0.0",
|
"react": "^19.0.0",
|
||||||
|
|||||||
@@ -29,6 +29,7 @@
|
|||||||
"clean:node": "rm -rf node_modules"
|
"clean:node": "rm -rf node_modules"
|
||||||
},
|
},
|
||||||
"peerDependencies": {
|
"peerDependencies": {
|
||||||
|
"@typed/lazy-ref": "^0.3.3",
|
||||||
"@types/react": "^19.0.0",
|
"@types/react": "^19.0.0",
|
||||||
"effect": "^3.13.0",
|
"effect": "^3.13.0",
|
||||||
"react": "^19.0.0",
|
"react": "^19.0.0",
|
||||||
|
|||||||
@@ -0,0 +1,29 @@
|
|||||||
|
import * as LazyRef from "@typed/lazy-ref"
|
||||||
|
import { Effect, Stream } from "effect"
|
||||||
|
import * as React from "react"
|
||||||
|
import { ReffuseExtension, ReffuseHelpers, SetStateAction } from "reffuse"
|
||||||
|
|
||||||
|
|
||||||
|
export const withLazyRef = ReffuseExtension.make(() => ({
|
||||||
|
useLazyRefState<A, E, R>(
|
||||||
|
this: ReffuseHelpers.ReffuseHelpers<R>,
|
||||||
|
ref: LazyRef.LazyRef<A, E, R>,
|
||||||
|
): [A, React.Dispatch<React.SetStateAction<A>>] {
|
||||||
|
const runSync = this.useRunSync()
|
||||||
|
|
||||||
|
const initialState = React.useMemo(() => runSync(ref), [])
|
||||||
|
const [reactStateValue, setReactStateValue] = React.useState(initialState)
|
||||||
|
|
||||||
|
this.useFork(Stream.runForEach(ref.changes, v => Effect.sync(() =>
|
||||||
|
setReactStateValue(v)
|
||||||
|
)), [ref])
|
||||||
|
|
||||||
|
const setValue = React.useCallback((setStateAction: React.SetStateAction<A>) =>
|
||||||
|
runSync(LazyRef.update(ref, prevState =>
|
||||||
|
SetStateAction.value(setStateAction, prevState)
|
||||||
|
)),
|
||||||
|
[ref])
|
||||||
|
|
||||||
|
return [reactStateValue, setValue]
|
||||||
|
},
|
||||||
|
}))
|
||||||
|
|||||||
Reference in New Issue
Block a user