0.1.2 #4
@@ -10,7 +10,7 @@ export const Route = createFileRoute("/lazyref")({
|
|||||||
})
|
})
|
||||||
|
|
||||||
function RouteComponent() {
|
function RouteComponent() {
|
||||||
const promise = R.usePromise(LazyRef.of(0))
|
const promise = R.usePromise(() => LazyRef.of(0))
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Suspense fallback={<Text>Loading...</Text>}>
|
<Suspense fallback={<Text>Loading...</Text>}>
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ const Result = Schema.Tuple(Schema.String)
|
|||||||
type Result = typeof Result.Type
|
type Result = typeof Result.Type
|
||||||
|
|
||||||
function RouteComponent() {
|
function RouteComponent() {
|
||||||
const promise = R.usePromise(Effect.addFinalizer(() => Console.log("Cleanup")).pipe(
|
const promise = R.usePromise(() => Effect.addFinalizer(() => Console.log("Cleanup")).pipe(
|
||||||
Effect.andThen(HttpClient.get("https://www.uuidtools.com/api/generate/v4")),
|
Effect.andThen(HttpClient.get("https://www.uuidtools.com/api/generate/v4")),
|
||||||
HttpClient.withTracerPropagation(false),
|
HttpClient.withTracerPropagation(false),
|
||||||
Effect.flatMap(res => res.json),
|
Effect.flatMap(res => res.json),
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ function RouteComponent() {
|
|||||||
// ), [])
|
// ), [])
|
||||||
// console.log(value)
|
// console.log(value)
|
||||||
|
|
||||||
R.useFork(Effect.addFinalizer(() => Console.log("cleanup")).pipe(
|
R.useFork(() => Effect.addFinalizer(() => Console.log("cleanup")).pipe(
|
||||||
Effect.andThen(Console.log("ouient")),
|
Effect.andThen(Console.log("ouient")),
|
||||||
Effect.delay("1 second"),
|
Effect.delay("1 second"),
|
||||||
))
|
))
|
||||||
|
|||||||
@@ -15,9 +15,9 @@ export const Route = createFileRoute("/time")({
|
|||||||
|
|
||||||
function Time() {
|
function Time() {
|
||||||
|
|
||||||
const timeRef = R.useMemo(DateTime.now.pipe(Effect.flatMap(SubscriptionRef.make)))
|
const timeRef = R.useMemo(() => DateTime.now.pipe(Effect.flatMap(SubscriptionRef.make)))
|
||||||
|
|
||||||
R.useFork(Effect.addFinalizer(() => Console.log("Cleanup")).pipe(
|
R.useFork(() => Effect.addFinalizer(() => Console.log("Cleanup")).pipe(
|
||||||
Effect.andThen(Stream.runForEach(timeEverySecond, v => Ref.set(timeRef, v)))
|
Effect.andThen(Stream.runForEach(timeEverySecond, v => Ref.set(timeRef, v)))
|
||||||
), [timeRef])
|
), [timeRef])
|
||||||
|
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ export function VNewTodo() {
|
|||||||
|
|
||||||
const runSync = R.useRunSync()
|
const runSync = R.useRunSync()
|
||||||
|
|
||||||
const todoRef = R.useMemo(createEmptyTodo.pipe(Effect.flatMap(SubscriptionRef.make)))
|
const todoRef = R.useMemo(() => createEmptyTodo.pipe(Effect.flatMap(SubscriptionRef.make)))
|
||||||
const [todo, setTodo] = R.useRefState(todoRef)
|
const [todo, setTodo] = R.useRefState(todoRef)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -9,13 +9,13 @@ import { VTodo } from "./VTodo"
|
|||||||
export function VTodos() {
|
export function VTodos() {
|
||||||
|
|
||||||
// Sync changes to the todos with the local storage
|
// Sync changes to the todos with the local storage
|
||||||
R.useFork(TodosState.TodosState.pipe(
|
R.useFork(() => TodosState.TodosState.pipe(
|
||||||
Effect.flatMap(state =>
|
Effect.flatMap(state =>
|
||||||
Stream.runForEach(state.todos.changes, () => state.saveToLocalStorage)
|
Stream.runForEach(state.todos.changes, () => state.saveToLocalStorage)
|
||||||
)
|
)
|
||||||
))
|
))
|
||||||
|
|
||||||
const todosRef = R.useMemo(TodosState.TodosState.pipe(Effect.map(state => state.todos)))
|
const todosRef = R.useMemo(() => TodosState.TodosState.pipe(Effect.map(state => state.todos)))
|
||||||
const [todos] = R.useRefState(todosRef)
|
const [todos] = R.useRefState(todosRef)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ export const LazyRefExtension = ReffuseExtension.make(() => ({
|
|||||||
const initialState = React.useMemo(() => runSync(ref), [])
|
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])
|
||||||
|
|
||||||
|
|||||||
@@ -24,52 +24,50 @@ export abstract class ReffuseHelpers<R> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
useRunSync<R>(this: ReffuseHelpers<R>) {
|
useRunSync<R>(this: ReffuseHelpers<R>): <A, E>(effect: Effect.Effect<A, E, R>) => A {
|
||||||
const runtime = ReffuseRuntime.useRuntime()
|
const runtime = ReffuseRuntime.useRuntime()
|
||||||
const context = this.useContext()
|
const context = this.useContext()
|
||||||
|
|
||||||
return React.useCallback(<A, E>(
|
return React.useCallback(effect => effect.pipe(
|
||||||
effect: Effect.Effect<A, E, R>
|
|
||||||
): A => effect.pipe(
|
|
||||||
Effect.provide(context),
|
Effect.provide(context),
|
||||||
Runtime.runSync(runtime),
|
Runtime.runSync(runtime),
|
||||||
), [runtime, context])
|
), [runtime, context])
|
||||||
}
|
}
|
||||||
|
|
||||||
useRunPromise<R>(this: ReffuseHelpers<R>) {
|
useRunPromise<R>(this: ReffuseHelpers<R>): <A, E>(
|
||||||
|
effect: Effect.Effect<A, E, R>,
|
||||||
|
options?: { readonly signal?: AbortSignal },
|
||||||
|
) => Promise<A> {
|
||||||
const runtime = ReffuseRuntime.useRuntime()
|
const runtime = ReffuseRuntime.useRuntime()
|
||||||
const context = this.useContext()
|
const context = this.useContext()
|
||||||
|
|
||||||
return React.useCallback(<A, E>(
|
return React.useCallback((effect, options) => effect.pipe(
|
||||||
effect: Effect.Effect<A, E, R>,
|
|
||||||
options?: { readonly signal?: AbortSignal },
|
|
||||||
): Promise<A> => effect.pipe(
|
|
||||||
Effect.provide(context),
|
Effect.provide(context),
|
||||||
effect => Runtime.runPromise(runtime)(effect, options),
|
effect => Runtime.runPromise(runtime)(effect, options),
|
||||||
), [runtime, context])
|
), [runtime, context])
|
||||||
}
|
}
|
||||||
|
|
||||||
useRunFork<R>(this: ReffuseHelpers<R>) {
|
useRunFork<R>(this: ReffuseHelpers<R>): <A, E>(
|
||||||
|
effect: Effect.Effect<A, E, R>,
|
||||||
|
options?: Runtime.RunForkOptions,
|
||||||
|
) => Fiber.RuntimeFiber<A, E> {
|
||||||
const runtime = ReffuseRuntime.useRuntime()
|
const runtime = ReffuseRuntime.useRuntime()
|
||||||
const context = this.useContext()
|
const context = this.useContext()
|
||||||
|
|
||||||
return React.useCallback(<A, E>(
|
return React.useCallback((effect, options) => effect.pipe(
|
||||||
effect: Effect.Effect<A, E, R>,
|
|
||||||
options?: Runtime.RunForkOptions,
|
|
||||||
): Fiber.RuntimeFiber<A, E> => effect.pipe(
|
|
||||||
Effect.provide(context),
|
Effect.provide(context),
|
||||||
effect => Runtime.runFork(runtime)(effect, options),
|
effect => Runtime.runFork(runtime)(effect, options),
|
||||||
), [runtime, context])
|
), [runtime, context])
|
||||||
}
|
}
|
||||||
|
|
||||||
useRunCallback<R>(this: ReffuseHelpers<R>) {
|
useRunCallback<R>(this: ReffuseHelpers<R>): <A, E>(
|
||||||
|
effect: Effect.Effect<A, E, R>,
|
||||||
|
options?: Runtime.RunCallbackOptions<A, E>,
|
||||||
|
) => Runtime.Cancel<A, E> {
|
||||||
const runtime = ReffuseRuntime.useRuntime()
|
const runtime = ReffuseRuntime.useRuntime()
|
||||||
const context = this.useContext()
|
const context = this.useContext()
|
||||||
|
|
||||||
return React.useCallback(<A, E>(
|
return React.useCallback((effect, options) => effect.pipe(
|
||||||
effect: Effect.Effect<A, E, R>,
|
|
||||||
options?: Runtime.RunCallbackOptions<A, E>,
|
|
||||||
): Runtime.Cancel<A, E> => effect.pipe(
|
|
||||||
Effect.provide(context),
|
Effect.provide(context),
|
||||||
effect => Runtime.runCallback(runtime)(effect, options),
|
effect => Runtime.runCallback(runtime)(effect, options),
|
||||||
), [runtime, context])
|
), [runtime, context])
|
||||||
@@ -87,13 +85,13 @@ export abstract class ReffuseHelpers<R> {
|
|||||||
*/
|
*/
|
||||||
useMemo<A, E, R>(
|
useMemo<A, E, R>(
|
||||||
this: ReffuseHelpers<R>,
|
this: ReffuseHelpers<R>,
|
||||||
effect: Effect.Effect<A, E, R>,
|
effect: () => Effect.Effect<A, E, R>,
|
||||||
deps?: React.DependencyList,
|
deps?: React.DependencyList,
|
||||||
options?: RenderOptions,
|
options?: RenderOptions,
|
||||||
): A {
|
): A {
|
||||||
const runSync = this.useRunSync()
|
const runSync = this.useRunSync()
|
||||||
|
|
||||||
return React.useMemo(() => runSync(effect), [
|
return React.useMemo(() => runSync(effect()), [
|
||||||
...options?.doNotReExecuteOnRuntimeOrContextChange ? [] : [runSync],
|
...options?.doNotReExecuteOnRuntimeOrContextChange ? [] : [runSync],
|
||||||
...(deps ?? []),
|
...(deps ?? []),
|
||||||
])
|
])
|
||||||
@@ -101,7 +99,7 @@ export abstract class ReffuseHelpers<R> {
|
|||||||
|
|
||||||
useMemoScoped<A, E, R>(
|
useMemoScoped<A, E, R>(
|
||||||
this: ReffuseHelpers<R>,
|
this: ReffuseHelpers<R>,
|
||||||
effect: Effect.Effect<A, E, R | Scope.Scope>,
|
effect: () => Effect.Effect<A, E, R | Scope.Scope>,
|
||||||
deps?: React.DependencyList,
|
deps?: React.DependencyList,
|
||||||
options?: RenderOptions & ScopeOptions,
|
options?: RenderOptions & ScopeOptions,
|
||||||
): A {
|
): A {
|
||||||
@@ -109,7 +107,7 @@ export abstract class ReffuseHelpers<R> {
|
|||||||
|
|
||||||
// Calculate an initial version of the value so that it can be accessed during the first render
|
// Calculate an initial version of the value so that it can be accessed during the first render
|
||||||
const [initialScope, initialValue] = React.useMemo(() => Scope.make(options?.finalizerExecutionStrategy).pipe(
|
const [initialScope, initialValue] = React.useMemo(() => Scope.make(options?.finalizerExecutionStrategy).pipe(
|
||||||
Effect.flatMap(scope => effect.pipe(
|
Effect.flatMap(scope => effect().pipe(
|
||||||
Effect.provideService(Scope.Scope, scope),
|
Effect.provideService(Scope.Scope, scope),
|
||||||
Effect.map(value => [scope, value] as const),
|
Effect.map(value => [scope, value] as const),
|
||||||
)),
|
)),
|
||||||
@@ -130,7 +128,7 @@ export abstract class ReffuseHelpers<R> {
|
|||||||
|
|
||||||
const [scope, value] = closeInitialScopeIfNeeded.pipe(
|
const [scope, value] = closeInitialScopeIfNeeded.pipe(
|
||||||
Effect.andThen(Scope.make(options?.finalizerExecutionStrategy).pipe(
|
Effect.andThen(Scope.make(options?.finalizerExecutionStrategy).pipe(
|
||||||
Effect.flatMap(scope => effect.pipe(
|
Effect.flatMap(scope => effect().pipe(
|
||||||
Effect.provideService(Scope.Scope, scope),
|
Effect.provideService(Scope.Scope, scope),
|
||||||
Effect.map(value => [scope, value] as const),
|
Effect.map(value => [scope, value] as const),
|
||||||
))
|
))
|
||||||
@@ -177,15 +175,15 @@ export abstract class ReffuseHelpers<R> {
|
|||||||
*/
|
*/
|
||||||
useEffect<A, E, R>(
|
useEffect<A, E, R>(
|
||||||
this: ReffuseHelpers<R>,
|
this: ReffuseHelpers<R>,
|
||||||
effect: Effect.Effect<A, E, R | Scope.Scope>,
|
effect: () => Effect.Effect<A, E, R | Scope.Scope>,
|
||||||
deps?: React.DependencyList,
|
deps?: React.DependencyList,
|
||||||
options?: RenderOptions & ScopeOptions,
|
options?: RenderOptions & ScopeOptions,
|
||||||
): void {
|
): void {
|
||||||
const runSync = this.useRunSync()
|
const runSync = this.useRunSync()
|
||||||
|
|
||||||
return React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
const scope = Scope.make(options?.finalizerExecutionStrategy).pipe(
|
const scope = Scope.make(options?.finalizerExecutionStrategy).pipe(
|
||||||
Effect.tap(scope => Effect.provideService(effect, Scope.Scope, scope)),
|
Effect.tap(scope => Effect.provideService(effect(), Scope.Scope, scope)),
|
||||||
runSync,
|
runSync,
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -225,7 +223,7 @@ export abstract class ReffuseHelpers<R> {
|
|||||||
*/
|
*/
|
||||||
useLayoutEffect<A, E, R>(
|
useLayoutEffect<A, E, R>(
|
||||||
this: ReffuseHelpers<R>,
|
this: ReffuseHelpers<R>,
|
||||||
effect: Effect.Effect<A, E, R | Scope.Scope>,
|
effect: () => Effect.Effect<A, E, R | Scope.Scope>,
|
||||||
deps?: React.DependencyList,
|
deps?: React.DependencyList,
|
||||||
options?: RenderOptions & ScopeOptions,
|
options?: RenderOptions & ScopeOptions,
|
||||||
): void {
|
): void {
|
||||||
@@ -233,7 +231,7 @@ export abstract class ReffuseHelpers<R> {
|
|||||||
|
|
||||||
return React.useLayoutEffect(() => {
|
return React.useLayoutEffect(() => {
|
||||||
const scope = Scope.make(options?.finalizerExecutionStrategy).pipe(
|
const scope = Scope.make(options?.finalizerExecutionStrategy).pipe(
|
||||||
Effect.tap(scope => Effect.provideService(effect, Scope.Scope, scope)),
|
Effect.tap(scope => Effect.provideService(effect(), Scope.Scope, scope)),
|
||||||
runSync,
|
runSync,
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -273,7 +271,7 @@ export abstract class ReffuseHelpers<R> {
|
|||||||
*/
|
*/
|
||||||
useFork<A, E, R>(
|
useFork<A, E, R>(
|
||||||
this: ReffuseHelpers<R>,
|
this: ReffuseHelpers<R>,
|
||||||
effect: Effect.Effect<A, E, R | Scope.Scope>,
|
effect: () => Effect.Effect<A, E, R | Scope.Scope>,
|
||||||
deps?: React.DependencyList,
|
deps?: React.DependencyList,
|
||||||
options?: Runtime.RunForkOptions & RenderOptions & ScopeOptions,
|
options?: Runtime.RunForkOptions & RenderOptions & ScopeOptions,
|
||||||
): void {
|
): void {
|
||||||
@@ -285,7 +283,7 @@ export abstract class ReffuseHelpers<R> {
|
|||||||
? Scope.fork(options.scope, options?.finalizerExecutionStrategy ?? ExecutionStrategy.sequential)
|
? Scope.fork(options.scope, options?.finalizerExecutionStrategy ?? ExecutionStrategy.sequential)
|
||||||
: Scope.make(options?.finalizerExecutionStrategy)
|
: Scope.make(options?.finalizerExecutionStrategy)
|
||||||
)
|
)
|
||||||
runFork(Effect.provideService(effect, Scope.Scope, scope), { ...options, scope })
|
runFork(Effect.provideService(effect(), Scope.Scope, scope), { ...options, scope })
|
||||||
|
|
||||||
return () => { runFork(Scope.close(scope, Exit.void)) }
|
return () => { runFork(Scope.close(scope, Exit.void)) }
|
||||||
}, [
|
}, [
|
||||||
@@ -296,7 +294,7 @@ export abstract class ReffuseHelpers<R> {
|
|||||||
|
|
||||||
usePromise<A, E, R>(
|
usePromise<A, E, R>(
|
||||||
this: ReffuseHelpers<R>,
|
this: ReffuseHelpers<R>,
|
||||||
effect: Effect.Effect<A, E, R | Scope.Scope>,
|
effect: () => Effect.Effect<A, E, R | Scope.Scope>,
|
||||||
deps?: React.DependencyList,
|
deps?: React.DependencyList,
|
||||||
options?: { readonly signal?: AbortSignal } & Runtime.RunForkOptions & RenderOptions & ScopeOptions,
|
options?: { readonly signal?: AbortSignal } & Runtime.RunForkOptions & RenderOptions & ScopeOptions,
|
||||||
): Promise<A> {
|
): Promise<A> {
|
||||||
@@ -318,7 +316,7 @@ export abstract class ReffuseHelpers<R> {
|
|||||||
if (options?.signal)
|
if (options?.signal)
|
||||||
options.signal.addEventListener("abort", cleanup)
|
options.signal.addEventListener("abort", cleanup)
|
||||||
|
|
||||||
effect.pipe(
|
effect().pipe(
|
||||||
Effect.provideService(Scope.Scope, scope),
|
Effect.provideService(Scope.Scope, scope),
|
||||||
Effect.match({
|
Effect.match({
|
||||||
onSuccess: resolve,
|
onSuccess: resolve,
|
||||||
@@ -347,7 +345,7 @@ export abstract class ReffuseHelpers<R> {
|
|||||||
value: A,
|
value: A,
|
||||||
): SubscriptionRef.SubscriptionRef<A> {
|
): SubscriptionRef.SubscriptionRef<A> {
|
||||||
return this.useMemo(
|
return this.useMemo(
|
||||||
SubscriptionRef.make(value),
|
() => SubscriptionRef.make(value),
|
||||||
[],
|
[],
|
||||||
{ doNotReExecuteOnRuntimeOrContextChange: true }, // Do not recreate the ref when the context changes
|
{ doNotReExecuteOnRuntimeOrContextChange: true }, // Do not recreate the ref when the context changes
|
||||||
)
|
)
|
||||||
@@ -369,7 +367,7 @@ export abstract class ReffuseHelpers<R> {
|
|||||||
const initialState = React.useMemo(() => runSync(ref), [])
|
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])
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user