Open History to see the last commit
1import type { Collection } from './collection/index.js'
2import type { CollectionStatus, HealthNode } from './types.js'
3
4// Collection guards and status flags. Core modules import them from here, and
5// the adapter layer (`live-query-adapter.ts`) re-exports them.
6
7/**
8 * Structural check for a live-query/`Collection` instance.
9 *
10 * Uses duck typing rather than `instanceof CollectionImpl` on purpose: adapters
11 * and core can resolve to different copies of `@tanstack/db` (dual-package /
12 * multi-realm), where `instanceof` gives false negatives. The three methods
13 * below uniquely identify a Collection.
14 */
15export function isCollection(
16 value: unknown,
17): value is Collection<any, any, any> {
18 return (
19 typeof value === `object` &&
20 value !== null &&
21 typeof (value as any).subscribeChanges === `function` &&
22 typeof (value as any).startSyncImmediate === `function` &&
23 typeof (value as any).id === `string`
24 )
25}
26
27/**
28 * The Collection an adapter hands users as `collection`. A pooled live query
29 * stands in for its live-query Collection and builds it only when touched.
30 */
31export function getPublicCollection<
32 T extends Collection<any, any, any> | null | undefined,
33>(collection: T): T {
34 return (
35 (collection as { publicCollection?: T } | null | undefined)
36 ?.publicCollection ?? collection
37 )
38}
39
40/** Whether a collection yields a single result (`findOne`) rather than an array. */
41export function isSingleResultCollection(
42 collection: Collection<any, any, any>,
43): boolean {
44 return (
45 (collection.config as { singleResult?: boolean } | undefined)
46 ?.singleResult === true
47 )
48}
49
50/** `error` and `crashed` end a sync run before it can serve its result. */
51export function isFailedStatus(status: CollectionStatus): boolean {
52 return status === `error` || status === `crashed`
53}
54
55/**
56 * The derived flags and health every adapter exposes for a query, besides
57 * `status`, which each adapter reports as its own field.
58 */
59export interface LiveQueryStatusFlags {
60 isLoading: boolean
61 isReady: boolean
62 isIdle: boolean
63 /** The first load failed: there is nothing to show. `status` is `error`. */
64 isLoadingError: boolean
65 /** Local storage or an invariant broke. `status` is `crashed`. */
66 isCrashed: boolean
67 isCleanedUp: boolean
68 /**
69 * The data is shown, and something keeps it from staying current: a sync
70 * problem or failed request somewhere in `health`. Set only while `status` is
71 * `ready`; a disabled query is never in a sync error.
72 */
73 isSyncError: boolean
74 /**
75 * The health tree for the query and every Collection it depends on, or
76 * `undefined` for a disabled query.
77 */
78 health: HealthNode | undefined
79}
80
81/**
82 * Derive the status flags from a collection status and its health tree. This is
83 * the one place the `isSyncError` rule is written. Adapters represent a disabled
84 * query separately (with `isReady: true`); this covers the real
85 * `CollectionStatus` values.
86 */
87export function getLiveQueryStatusFlags(
88 status: CollectionStatus,
89 health?: HealthNode,
90): LiveQueryStatusFlags {
91 return {
92 isLoading: status === `loading`,
93 isReady: status === `ready`,
94 isIdle: status === `idle`,
95 isLoadingError: status === `error`,
96 isCrashed: status === `crashed`,
97 isCleanedUp: status === `cleaned-up`,
98 isSyncError: status === `ready` && health?.hasErrors === true,
99 health,
100 }
101}