|
1 | | -import { type DependencyList, useCallback } from "react" |
2 | | -import { catchError, identity, retry } from "rxjs" |
| 1 | +import { useCallback } from "react" |
| 2 | +import { of, retry, tap, throwError } from "rxjs" |
3 | 3 | import { makeObservable } from "../utils/makeObservable" |
4 | 4 | import { useLiveRef } from "../utils/react/useLiveRef" |
5 | | -import type { SubscribeSource } from "./types" |
6 | | -import { useSubscribe } from "./useSubscribe" |
| 5 | +import type { SubscribeSource } from "./useSubscribe/types" |
| 6 | +import { useSubscribe } from "./useSubscribe/useSubscribe" |
7 | 7 |
|
8 | 8 | interface Option { |
9 | | - retry?: boolean |
| 9 | + retryOnError?: boolean |
10 | 10 | onError?: (error: unknown) => void |
11 | 11 | } |
12 | 12 |
|
13 | | -export function useSubscribeEffect<T>(source: SubscribeSource<T>): void |
14 | 13 | export function useSubscribeEffect<T>( |
15 | 14 | source: SubscribeSource<T>, |
16 | | - options: Option, |
17 | | -): void |
18 | | - |
19 | | -export function useSubscribeEffect<T>( |
20 | | - source: SubscribeSource<T>, |
21 | | - deps: DependencyList, |
22 | | -): void |
23 | | - |
24 | | -export function useSubscribeEffect<T>( |
25 | | - source: SubscribeSource<T>, |
26 | | - options: Option, |
27 | | - deps: DependencyList, |
28 | | -): void |
29 | | - |
30 | | -export function useSubscribeEffect<T>( |
31 | | - source: SubscribeSource<T>, |
32 | | - unsafeOptions?: Option | DependencyList, |
33 | | - deps: DependencyList = [], |
| 15 | + options?: Option, |
34 | 16 | ) { |
35 | | - const options = |
36 | | - unsafeOptions != null && !Array.isArray(unsafeOptions) |
37 | | - ? (unsafeOptions as Option) |
38 | | - : ({} satisfies Option) |
39 | | - const retryOption = options.retry ?? true |
40 | | - const onErrorRef = useLiveRef( |
41 | | - options.onError ?? |
42 | | - ((error: unknown) => { |
43 | | - console.error(error) |
44 | | - }), |
45 | | - ) |
| 17 | + const optionsRef = useLiveRef(options) |
46 | 18 |
|
47 | | - // biome-ignore lint/correctness/useExhaustiveDependencies: TODO |
48 | | - const sourceAsObservable = useCallback(() => makeObservable(source)(), deps) |
| 19 | + const enhancerMakeObservable = useCallback(() => { |
| 20 | + const source$ = makeObservable(source)() |
49 | 21 |
|
50 | | - const enhancerMakeObservable = useCallback( |
51 | | - () => |
52 | | - sourceAsObservable().pipe( |
53 | | - catchError((error) => { |
54 | | - onErrorRef.current(error) |
55 | | - |
56 | | - throw error |
57 | | - }), |
58 | | - retryOption ? retry() : identity, |
59 | | - ), |
60 | | - [sourceAsObservable, retryOption, onErrorRef], |
61 | | - ) |
| 22 | + return source$.pipe( |
| 23 | + tap({ |
| 24 | + error: (error) => { |
| 25 | + optionsRef.current?.onError?.(error) |
| 26 | + }, |
| 27 | + }), |
| 28 | + retry({ |
| 29 | + delay: (error) => |
| 30 | + optionsRef.current?.retryOnError ? of(0) : throwError(() => error), |
| 31 | + }), |
| 32 | + ) |
| 33 | + }, [source, optionsRef]) |
62 | 34 |
|
63 | | - useSubscribe(enhancerMakeObservable, deps) |
| 35 | + useSubscribe(enhancerMakeObservable) |
64 | 36 | } |
0 commit comments