0.1.3 #5

Merged
Thilawyn merged 104 commits from next into master 2025-03-11 01:44:38 +01:00
2 changed files with 15 additions and 62 deletions
Showing only changes of commit 65810a6d79 - Show all commits

View File

@@ -15,9 +15,8 @@ const Result = Schema.Tuple(Schema.String)
type Result = typeof Result.Type type Result = typeof Result.Type
function RouteComponent() { function RouteComponent() {
const promise = R.usePromiseScoped(Effect.addFinalizer(() => Console.log("Cleanup")).pipe( const promise = R.usePromise(Effect.addFinalizer(() => Console.log("Cleanup")).pipe(
Effect.andThen(HttpClient.HttpClient), Effect.andThen(HttpClient.get("https://www.uuidtools.com/api/generate/v4")),
Effect.flatMap(client => client.get("https://www.uuidtools.com/api/generate/v4")),
HttpClient.withTracerPropagation(false), HttpClient.withTracerPropagation(false),
Effect.flatMap(res => res.json), Effect.flatMap(res => res.json),
Effect.flatMap(Schema.decodeUnknown(Result)), Effect.flatMap(Schema.decodeUnknown(Result)),

View File

@@ -284,19 +284,6 @@ export class Reffuse<R> {
} }
usePromise<A, E>( usePromise<A, E>(
effect: Effect.Effect<A, E, R>,
deps?: React.DependencyList,
options?: { readonly signal?: AbortSignal } & RenderOptions,
): Promise<A> {
const runPromise = this.useRunPromise()
return React.useMemo(() => runPromise(effect, options), [
...options?.doNotReExecuteOnRuntimeOrContextChange ? [] : [runPromise],
...(deps ?? []),
])
}
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 } & Runtime.RunForkOptions & RenderOptions & ScopeOptions, options?: { readonly signal?: AbortSignal } & Runtime.RunForkOptions & RenderOptions & ScopeOptions,
@@ -310,27 +297,29 @@ export class Reffuse<R> {
const { promise, resolve, reject } = Promise.withResolvers<A>() const { promise, resolve, reject } = Promise.withResolvers<A>()
setValue(promise) setValue(promise)
const scope = runSync(Scope.make(options?.finalizerExecutionStrategy)) const scope = runSync(options?.scope
? Scope.fork(options.scope, options?.finalizerExecutionStrategy ?? ExecutionStrategy.sequential)
: Scope.make(options?.finalizerExecutionStrategy)
)
const fiber = effect.pipe( const cleanup = () => { runFork(Scope.close(scope, Exit.void)) }
if (options?.signal)
options.signal.addEventListener("abort", cleanup)
effect.pipe(
Effect.provideService(Scope.Scope, scope), Effect.provideService(Scope.Scope, scope),
Effect.match({ Effect.match({
onSuccess: resolve, onSuccess: resolve,
onFailure: reject, onFailure: reject,
}), }),
// TODO: use scope from RunForkOptions?
effect => runFork(effect, { ...options, scope }), effect => runFork(effect, { ...options, scope }),
) )
return () => { return () => {
// Fiber.interrupt(fiber).pipe( if (options?.signal)
// Effect.andThen(Scope.close(scope, Exit.void)), options.signal.removeEventListener("abort", cleanup)
// // \/ TODO: should interrupted promises reject?
// // Effect.andThen(Effect.sync(() => { reject() })), cleanup()
// runFork,
// )
runFork(Scope.close(scope, Exit.void))
} }
}, [ }, [
...options?.doNotReExecuteOnRuntimeOrContextChange ? [] : [runSync, runFork], ...options?.doNotReExecuteOnRuntimeOrContextChange ? [] : [runSync, runFork],
@@ -340,41 +329,6 @@ export class Reffuse<R> {
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(