( atom: () => Atom.Atom<AsyncResult.AsyncResult<A, E>> )
| 285 | export const withDataFallback = <A, E>( |
| 286 | // Structural read so both Ref and Readonly<Ref> from useAtomValue are accepted. |
| 287 | rawResult: { readonly value: AsyncResult.AsyncResult<A, E> }, |
| 288 | options?: unknown |
| 289 | ): ComputedRef<AsyncResult.AsyncResult<A, E>> => { |
| 290 | const opts = options as |
| 291 | | { readonly initialData?: A | (() => A); readonly placeholderData?: A | ((prev: A | undefined) => A) } |
| 292 | | undefined |
| 293 | const initialData = opts?.initialData |
| 294 | const placeholderData = opts?.placeholderData |
| 295 | if (initialData === undefined && placeholderData === undefined) { |
| 296 | // Identity when no fallback options. Callers that need a real ComputedRef |
| 297 | // (e.g. useAtomValue's Readonly<Ref>) wrap before calling this helper. |
| 298 | return rawResult as ComputedRef<AsyncResult.AsyncResult<A, E>> |
| 299 | } |
| 300 | |
| 301 | let lastData: A | undefined |
| 302 | return computed(() => { |
| 303 | const r = rawResult.value |
| 304 | const concrete = AsyncResult.value(r) |
| 305 | if (Option.isSome(concrete)) { |
| 306 | lastData = concrete.value |
| 307 | } |
| 308 | // Fallback is pending-only. Failure (even without previousSuccess) and Success pass through. |
| 309 | if (!AsyncResult.isInitial(r)) { |
| 310 | return r |
| 311 | } |
| 312 | if (initialData !== undefined) { |
| 313 | const v = typeof initialData === "function" ? (initialData as () => A)() : initialData |
| 314 | return AsyncResult.success(v, { waiting: r.waiting }) |
| 315 | } |
searching dependent graphs…