Skip to content

Commit 4b03934

Browse files
committed
feat(hooks): add interruptible behavior options
1 parent a8feaa6 commit 4b03934

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

src/hooks/useResultCallback.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,30 @@ import type { RuntimeContext } from "effect-react/internal/runtimeContext"
99
import * as Result from "effect-react/Result"
1010
import { useCallback, useContext, useEffect, useRef, useState } from "react"
1111

12+
interface UseResultCallbackOptions {
13+
/**
14+
* Determines on-change behavior
15+
* If this is true, effects and streams from callbacks will be run through
16+
* on a best-effort basis instead of interrupting.
17+
* Before using this option, check if you do not actually want to use `Effect.uninterruptible` instead.
18+
*/
19+
uninterruptible?: true
20+
}
21+
1222
export type UseResultCallback<R> = <Args extends Array<any>, R0 extends R, E, A>(
13-
callback: (...args: Args) => Effect.Effect<R0, E, A>
23+
callback: (...args: Args) => Effect.Effect<R0, E, A>,
24+
options?: UseResultCallbackOptions
1425
) => readonly [ResultBag<E, A>, (...args: Args) => void]
1526

1627
export const makeUseResultCallback: <R>(
1728
runtimeContext: RuntimeContext<R>
1829
) => UseResultCallback<R> = <R>(
1930
runtimeContext: RuntimeContext<R>
2031
) =>
21-
<Args extends Array<any>, R0 extends R, E, A>(f: (...args: Args) => Stream.Stream<R0, E, A>) => {
32+
<Args extends Array<any>, R0 extends R, E, A>(
33+
f: (...args: Args) => Stream.Stream<R0, E, A>,
34+
options?: UseResultCallbackOptions
35+
) => {
2236
const runtime = useContext(runtimeContext)
2337
const fiberRef = useRef<Fiber.RuntimeFiber<E, void>>()
2438
const queueRef = useRef<Queue.Queue<[(...args: Args) => Stream.Stream<R0, E, A>, Args]>>()
@@ -35,7 +49,7 @@ export const makeUseResultCallback: <R>(
3549
setResult((prev) => updateNext(Result.waiting(prev), trackRef))
3650
})
3751
),
38-
Stream.flatMap(([f, args]) => f(...args)),
52+
Stream.flatMap(([f, args]) => f(...args), { switch: !options?.uninterruptible }),
3953
Stream.tap((value) =>
4054
Effect.sync(() => {
4155
setResult(updateNext(Result.success(value), trackRef))

0 commit comments

Comments
 (0)