0.1.3 #5
@@ -301,38 +301,80 @@ export class Reffuse<R> {
|
|||||||
usePromiseScoped<A, E>(
|
usePromiseScoped<A, E>(
|
||||||
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 } & RenderOptions & ScopeOptions,
|
options?: { readonly signal?: AbortSignal } & Runtime.RunForkOptions & RenderOptions & ScopeOptions,
|
||||||
): Promise<A> {
|
): Promise<A> {
|
||||||
const runSync = this.useRunSync()
|
const runSync = this.useRunSync()
|
||||||
const runPromise = this.useRunPromise()
|
const runFork = this.useRunFork()
|
||||||
|
|
||||||
const [value, setValue] = React.useState(new Promise<A>(() => {}))
|
const [value, setValue] = React.useState(Promise.withResolvers<A>().promise)
|
||||||
|
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
const controller = new AbortController()
|
const { promise, resolve, reject } = Promise.withResolvers<A>()
|
||||||
const signal = AbortSignal.any([
|
setValue(promise)
|
||||||
controller.signal,
|
|
||||||
...options?.signal ? [options.signal] : [],
|
|
||||||
])
|
|
||||||
|
|
||||||
const scope = runSync(Scope.make(options?.finalizerExecutionStrategy))
|
const scope = runSync(Scope.make(options?.finalizerExecutionStrategy))
|
||||||
setValue(runPromise(Effect.provideService(effect, Scope.Scope, scope), {
|
|
||||||
...options,
|
const fiber = effect.pipe(
|
||||||
signal,
|
Effect.provideService(Scope.Scope, scope),
|
||||||
}))
|
Effect.match({
|
||||||
|
onSuccess: resolve,
|
||||||
|
onFailure: reject,
|
||||||
|
}),
|
||||||
|
|
||||||
|
// TODO: use scope from RunForkOptions?
|
||||||
|
effect => runFork(effect, options),
|
||||||
|
)
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
controller.abort()
|
Fiber.interrupt(fiber).pipe(
|
||||||
runSync(Scope.close(scope, Exit.void))
|
Effect.andThen(Scope.close(scope, Exit.void)),
|
||||||
|
Effect.andThen(Effect.sync(() => { reject() })), // TODO: Relevant?
|
||||||
|
runFork,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}, [
|
}, [
|
||||||
...options?.doNotReExecuteOnRuntimeOrContextChange ? [] : [runSync, runPromise],
|
...options?.doNotReExecuteOnRuntimeOrContextChange ? [] : [runSync, runFork],
|
||||||
...(deps ?? []),
|
...(deps ?? []),
|
||||||
])
|
])
|
||||||
|
|
||||||
return value
|
return value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// usePromiseScoped<A, E>(
|
||||||
|
// effect: Effect.Effect<A, E, R | Scope.Scope>,
|
||||||
|
// deps?: React.DependencyList,
|
||||||
|
// options?: { readonly signal?: AbortSignal } & RenderOptions & ScopeOptions,
|
||||||
|
// ): Promise<A> {
|
||||||
|
// const runSync = this.useRunSync()
|
||||||
|
// const runPromise = this.useRunPromise()
|
||||||
|
|
||||||
|
// const [value, setValue] = React.useState(new Promise<A>(() => {}))
|
||||||
|
|
||||||
|
// React.useEffect(() => {
|
||||||
|
// const controller = new AbortController()
|
||||||
|
// const signal = AbortSignal.any([
|
||||||
|
// controller.signal,
|
||||||
|
// ...options?.signal ? [options.signal] : [],
|
||||||
|
// ])
|
||||||
|
|
||||||
|
// const scope = runSync(Scope.make(options?.finalizerExecutionStrategy))
|
||||||
|
// setValue(runPromise(Effect.provideService(effect, Scope.Scope, scope), {
|
||||||
|
// ...options,
|
||||||
|
// signal,
|
||||||
|
// }))
|
||||||
|
|
||||||
|
// return () => {
|
||||||
|
// controller.abort()
|
||||||
|
// runSync(Scope.close(scope, Exit.void))
|
||||||
|
// }
|
||||||
|
// }, [
|
||||||
|
// ...options?.doNotReExecuteOnRuntimeOrContextChange ? [] : [runSync, runPromise],
|
||||||
|
// ...(deps ?? []),
|
||||||
|
// ])
|
||||||
|
|
||||||
|
// return value
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
useRef<A>(value: A): SubscriptionRef.SubscriptionRef<A> {
|
useRef<A>(value: A): SubscriptionRef.SubscriptionRef<A> {
|
||||||
return this.useMemo(
|
return this.useMemo(
|
||||||
|
|||||||
Reference in New Issue
Block a user