@tanstack/db
Version:
A reactive client store for building super fast apps on sync
1 lines • 4.16 kB
Source Map (JSON)
{"version":3,"file":"live-query-primitives.cjs","sources":["../../src/live-query-primitives.ts"],"sourcesContent":["import type { Collection } from './collection/index.js'\nimport type { CollectionStatus, HealthNode } from './types.js'\n\n// Collection guards and status flags. Core modules import them from here, and\n// the adapter layer (`live-query-adapter.ts`) re-exports them.\n\n/**\n * Structural check for a live-query/`Collection` instance.\n *\n * Uses duck typing rather than `instanceof CollectionImpl` on purpose: adapters\n * and core can resolve to different copies of `@tanstack/db` (dual-package /\n * multi-realm), where `instanceof` gives false negatives. The three methods\n * below uniquely identify a Collection.\n */\nexport function isCollection(\n value: unknown,\n): value is Collection<any, any, any> {\n return (\n typeof value === `object` &&\n value !== null &&\n typeof (value as any).subscribeChanges === `function` &&\n typeof (value as any).startSyncImmediate === `function` &&\n typeof (value as any).id === `string`\n )\n}\n\n/**\n * The Collection an adapter hands users as `collection`. A pooled live query\n * stands in for its live-query Collection and builds it only when touched.\n */\nexport function getPublicCollection<\n T extends Collection<any, any, any> | null | undefined,\n>(collection: T): T {\n return (\n (collection as { publicCollection?: T } | null | undefined)\n ?.publicCollection ?? collection\n )\n}\n\n/** Whether a collection yields a single result (`findOne`) rather than an array. */\nexport function isSingleResultCollection(\n collection: Collection<any, any, any>,\n): boolean {\n return (\n (collection.config as { singleResult?: boolean } | undefined)\n ?.singleResult === true\n )\n}\n\n/** `error` and `crashed` end a sync run before it can serve its result. */\nexport function isFailedStatus(status: CollectionStatus): boolean {\n return status === `error` || status === `crashed`\n}\n\n/**\n * The derived flags and health every adapter exposes for a query, besides\n * `status`, which each adapter reports as its own field.\n */\nexport interface LiveQueryStatusFlags {\n isLoading: boolean\n isReady: boolean\n isIdle: boolean\n /** The first load failed: there is nothing to show. `status` is `error`. */\n isLoadingError: boolean\n /** Local storage or an invariant broke. `status` is `crashed`. */\n isCrashed: boolean\n isCleanedUp: boolean\n /**\n * The data is shown, and something keeps it from staying current: a sync\n * problem or failed request somewhere in `health`. Set only while `status` is\n * `ready`; a disabled query is never in a sync error.\n */\n isSyncError: boolean\n /**\n * The health tree for the query and every Collection it depends on, or\n * `undefined` for a disabled query.\n */\n health: HealthNode | undefined\n}\n\n/**\n * Derive the status flags from a collection status and its health tree. This is\n * the one place the `isSyncError` rule is written. Adapters represent a disabled\n * query separately (with `isReady: true`); this covers the real\n * `CollectionStatus` values.\n */\nexport function getLiveQueryStatusFlags(\n status: CollectionStatus,\n health?: HealthNode,\n): LiveQueryStatusFlags {\n return {\n isLoading: status === `loading`,\n isReady: status === `ready`,\n isIdle: status === `idle`,\n isLoadingError: status === `error`,\n isCrashed: status === `crashed`,\n isCleanedUp: status === `cleaned-up`,\n isSyncError: status === `ready` && health?.hasErrors === true,\n health,\n }\n}\n"],"names":[],"mappings":";;AAcO,SAAS,aACd,OACoC;AACpC,SACE,OAAO,UAAU,YACjB,UAAU,QACV,OAAQ,MAAc,qBAAqB,cAC3C,OAAQ,MAAc,uBAAuB,cAC7C,OAAQ,MAAc,OAAO;AAEjC;AAMO,SAAS,oBAEd,YAAkB;AAClB,SACG,YACG,oBAAoB;AAE5B;AAGO,SAAS,yBACd,YACS;AACT,SACG,WAAW,QACR,iBAAiB;AAEzB;AAGO,SAAS,eAAe,QAAmC;AAChE,SAAO,WAAW,WAAW,WAAW;AAC1C;AAkCO,SAAS,wBACd,QACA,QACsB;AACtB,SAAO;AAAA,IACL,WAAW,WAAW;AAAA,IACtB,SAAS,WAAW;AAAA,IACpB,QAAQ,WAAW;AAAA,IACnB,gBAAgB,WAAW;AAAA,IAC3B,WAAW,WAAW;AAAA,IACtB,aAAa,WAAW;AAAA,IACxB,aAAa,WAAW,WAAW,QAAQ,cAAc;AAAA,IACzD;AAAA,EAAA;AAEJ;;;;;;"}