@reffuse/extension-query 0.1.5 #16
@@ -9,19 +9,17 @@ export const LazyRefExtension = ReffuseExtension.make(() => ({
|
|||||||
this: ReffuseHelpers.ReffuseHelpers<R>,
|
this: ReffuseHelpers.ReffuseHelpers<R>,
|
||||||
ref: LazyRef.LazyRef<A, E, R>,
|
ref: LazyRef.LazyRef<A, E, R>,
|
||||||
): [A, React.Dispatch<React.SetStateAction<A>>] {
|
): [A, React.Dispatch<React.SetStateAction<A>>] {
|
||||||
const runSync = this.useRunSync()
|
const initialState = this.useMemo(() => ref, [], { doNotReExecuteOnRuntimeOrContextChange: true })
|
||||||
|
|
||||||
const initialState = React.useMemo(() => runSync(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(() =>
|
||||||
setReactStateValue(v)
|
setReactStateValue(v)
|
||||||
)), [ref])
|
)), [ref])
|
||||||
|
|
||||||
const setValue = React.useCallback((setStateAction: React.SetStateAction<A>) =>
|
const setValue = this.useCallbackSync((setStateAction: React.SetStateAction<A>) =>
|
||||||
runSync(LazyRef.update(ref, prevState =>
|
LazyRef.update(ref, prevState =>
|
||||||
SetStateAction.value(setStateAction, prevState)
|
SetStateAction.value(setStateAction, prevState)
|
||||||
)),
|
),
|
||||||
[ref])
|
[ref])
|
||||||
|
|
||||||
return [reactStateValue, setValue]
|
return [reactStateValue, setValue]
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import { ReffuseExtension, type ReffuseHelpers } from "reffuse"
|
|||||||
|
|
||||||
|
|
||||||
export interface UseQueryProps<A, E, R> {
|
export interface UseQueryProps<A, E, R> {
|
||||||
effect(): Effect.Effect<A, E, R | Scope.Scope>
|
effect: () => Effect.Effect<A, E, R | Scope.Scope>
|
||||||
readonly deps?: React.DependencyList
|
readonly deps?: React.DependencyList
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -388,19 +388,17 @@ export abstract class ReffuseHelpers<R> {
|
|||||||
this: ReffuseHelpers<R>,
|
this: ReffuseHelpers<R>,
|
||||||
ref: SubscriptionRef.SubscriptionRef<A>,
|
ref: SubscriptionRef.SubscriptionRef<A>,
|
||||||
): [A, React.Dispatch<React.SetStateAction<A>>] {
|
): [A, React.Dispatch<React.SetStateAction<A>>] {
|
||||||
const runSync = this.useRunSync()
|
const initialState = this.useMemo(() => ref, [], { doNotReExecuteOnRuntimeOrContextChange: true })
|
||||||
|
|
||||||
const initialState = React.useMemo(() => runSync(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(() =>
|
||||||
setReactStateValue(v)
|
setReactStateValue(v)
|
||||||
)), [ref])
|
)), [ref])
|
||||||
|
|
||||||
const setValue = React.useCallback((setStateAction: React.SetStateAction<A>) =>
|
const setValue = this.useCallbackSync((setStateAction: React.SetStateAction<A>) =>
|
||||||
runSync(Ref.update(ref, prevState =>
|
Ref.update(ref, prevState =>
|
||||||
SetStateAction.value(setStateAction, prevState)
|
SetStateAction.value(setStateAction, prevState)
|
||||||
)),
|
),
|
||||||
[ref])
|
[ref])
|
||||||
|
|
||||||
return [reactStateValue, setValue]
|
return [reactStateValue, setValue]
|
||||||
|
|||||||
Reference in New Issue
Block a user