harden code

Massimo Melina committed Nov 1, 2025 at 19:41 UTC 07a726ab20fb2a6f99ee5ad2ebfc6a5723c8c517
1 file changed +6 -6
shared/api.ts
+6 -6
@@ -95,16 +95,16 @@ export function useApi<T=any>(cmd: string | Falsy, params?: object, options: Api
95 const reloadPromise = useRef<any>()
96 useEffect(() => {
97 setError(undefined)
98 - const aborted = () => getLoading()?.aborted()
98 + const isAborted = () => getLoading()?.aborted()
99 const wholePromise = wait(0) // postpone a bit so that if it is aborted immediately, it is never really fired (happens mostly in dev mode)
100 .then(() => {
101 - const ret = !cmd || aborted() ? undefined : apiCall<T>(cmd, params, options)
101 + const ret = !cmd || isAborted() ? undefined : apiCall<T>(cmd, params, options)
102 setLoading(ret)
103 return ret
104 })
105 - .then(res => aborted() || setData(res as any) || setError(undefined),
105 + .then(res => isAborted() || setData(res as any) || setError(undefined),
106 err => {
107 - if (aborted()) return
107 + if (isAborted()) return
108 setError(err)
109 setData(undefined)
110 })
@@ -113,9 +113,9 @@ export function useApi<T=any>(cmd: string | Falsy, params?: object, options: Api
113 return () => { wholePromise.finally(() => getLoading()?.abort()) }
114 }, [cmd, JSON.stringify(params), forcer]) //eslint-disable-line -- json-ize to detect deep changes
115 const reload = useCallback(() => {
116 - if (getLoading()) return
117 - setForcer(v => v + 1)
116 + if (reloadPromise.current) return
117 reloadPromise.current = pendingPromise()
118 + setForcer(v => v + 1)
119 }, [setForcer])
120 const ee = useMemo(() => new BetterEventEmitter, [])
121 const sub = useCallback((cb: Callback) => ee.on('data', cb), [ee])