@ttt-productions/query-core 0.14.0 → 0.14.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/dist/cache-helpers.js.map +1 -1
  2. package/dist/defaults.js.map +1 -1
  3. package/dist/domain-events/cache-invalidation.d.ts +21 -0
  4. package/dist/domain-events/cache-invalidation.d.ts.map +1 -1
  5. package/dist/domain-events/cache-invalidation.js +58 -19
  6. package/dist/domain-events/cache-invalidation.js.map +1 -1
  7. package/dist/domain-events/create-domain-event-invalidator.js.map +1 -1
  8. package/dist/domain-events/index.js.map +1 -1
  9. package/dist/firestore/infinite-helpers.js.map +1 -1
  10. package/dist/firestore/types.js.map +1 -1
  11. package/dist/index.js.map +1 -1
  12. package/dist/keys.js.map +1 -1
  13. package/dist/query-client.js.map +1 -1
  14. package/dist/react/firestore/absence-scheduler.d.ts +39 -0
  15. package/dist/react/firestore/absence-scheduler.d.ts.map +1 -0
  16. package/dist/react/firestore/absence-scheduler.js +182 -0
  17. package/dist/react/firestore/absence-scheduler.js.map +1 -0
  18. package/dist/react/firestore/batch-doc-loader.d.ts +45 -0
  19. package/dist/react/firestore/batch-doc-loader.d.ts.map +1 -0
  20. package/dist/react/firestore/batch-doc-loader.js +179 -0
  21. package/dist/react/firestore/batch-doc-loader.js.map +1 -0
  22. package/dist/react/firestore/context.js.map +1 -1
  23. package/dist/react/firestore/doc-subscription-registry.d.ts +9 -0
  24. package/dist/react/firestore/doc-subscription-registry.d.ts.map +1 -1
  25. package/dist/react/firestore/doc-subscription-registry.js +0 -0
  26. package/dist/react/firestore/doc-subscription-registry.js.map +1 -1
  27. package/dist/react/firestore/resubscribe.js.map +1 -1
  28. package/dist/react/firestore/use-firestore-collection.js.map +1 -1
  29. package/dist/react/firestore/use-firestore-count.js.map +1 -1
  30. package/dist/react/firestore/use-firestore-doc.js.map +1 -1
  31. package/dist/react/firestore/use-firestore-infinite.js.map +1 -1
  32. package/dist/react/firestore/use-firestore-live-infinite.js.map +1 -1
  33. package/dist/react/firestore/use-firestore-mutations.js.map +1 -1
  34. package/dist/react/firestore/use-firestore-paginated.js.map +1 -1
  35. package/dist/react/firestore/useBatchFirestoreDocs.d.ts +63 -21
  36. package/dist/react/firestore/useBatchFirestoreDocs.d.ts.map +1 -1
  37. package/dist/react/firestore/useBatchFirestoreDocs.js +117 -109
  38. package/dist/react/firestore/useBatchFirestoreDocs.js.map +1 -1
  39. package/dist/react/index.d.ts +2 -1
  40. package/dist/react/index.d.ts.map +1 -1
  41. package/dist/react/index.js +1 -0
  42. package/dist/react/index.js.map +1 -1
  43. package/dist/react/provider.js.map +1 -1
  44. package/dist/react/search/use-firestore-search.js.map +1 -1
  45. package/dist/search/types.js.map +1 -1
  46. package/package.json +1 -1
@@ -1,4 +1,6 @@
1
- import { type Firestore } from 'firebase/firestore';
1
+ import type { Firestore } from 'firebase/firestore';
2
+ /** Transport used to resolve an id in one-shot mode. */
3
+ export type BatchFirestoreDocsTransport = 'batch' | 'get';
2
4
  export type BatchFirestoreDocsOptions = {
3
5
  /**
4
6
  * Firestore instance to use for queries
@@ -14,12 +16,13 @@ export type BatchFirestoreDocsOptions = {
14
16
  ids: string[];
15
17
  /**
16
18
  * Query key prefix for individual document cache entries
17
- * Each document will be cached as [queryKeyPrefix, docId]
19
+ * Each document is cached as [queryKeyPrefix, docId]
18
20
  * @example 'publicUser' -> ['publicUser', 'user123']
19
21
  */
20
22
  queryKeyPrefix: string;
21
23
  /**
22
- * Time in milliseconds to consider data fresh (default: 30 minutes)
24
+ * Time in milliseconds a PRESENT document stays fresh (default: 30 minutes).
25
+ * An absent document uses `absentStaleTime` instead.
23
26
  */
24
27
  staleTime?: number;
25
28
  /**
@@ -32,46 +35,85 @@ export type BatchFirestoreDocsOptions = {
32
35
  enabled?: boolean;
33
36
  /**
34
37
  * Real-time mode (default: false). When true, each id is resolved via a shared,
35
- * reference-counted `onSnapshot` listener instead of a one-shot `getDocs` batch:
38
+ * reference-counted `onSnapshot` listener instead of a one-shot read:
36
39
  * - a missing doc caches `null` (negative caching) and resolves the instant it appears,
37
40
  * - identity edits propagate live, and every browser tab stays consistent,
38
- * - `staleTime` is irrelevant (listeners keep the cache fresh).
39
- * Initial read cost matches the batch path; listeners then stay open while mounted.
40
- * Use for small, frequently-rendered, change-sensitive identity docs (e.g. publicUsers).
41
+ * - `staleTime` is irrelevant (listeners keep the cache fresh) and `refetch()` is a no-op.
42
+ * Initial read cost matches the one-shot path; listeners then stay open while mounted.
43
+ * Use for small, frequently-rendered, change-sensitive docs owned by the signed-in user.
41
44
  */
42
45
  subscribe?: boolean;
46
+ /**
47
+ * One-shot transport (default: `'batch'`).
48
+ *
49
+ * - `'batch'` — ids enqueued in the same microtask are coalesced into
50
+ * `where(documentId(), 'in', …)` queries of at most 30 ids each. Fewest round-trips.
51
+ * - `'get'` — one `getDoc` per id, run concurrently. Required for a collection whose
52
+ * Firestore rules gate reads on `resource.data`: an unconstrained id-list query is
53
+ * denied WHOLESALE there, while per-document gets are evaluated per document, so one
54
+ * unreadable id errors alone instead of failing every id with it. On such a collection
55
+ * a get against a nonexistent doc is also denied, so "absent" and "hidden" are the same
56
+ * client-visible signal — the id surfaces as that query's error, never as a null.
57
+ */
58
+ transport?: BatchFirestoreDocsTransport;
59
+ /**
60
+ * Time in milliseconds an ABSENT (`null`) result stays fresh (default: 30 seconds).
61
+ * Deliberately much shorter than `staleTime`: a missing doc is usually a doc that has
62
+ * not been mirrored/created YET, and holding "missing" as fresh for the full staleTime
63
+ * is what leaves a row blank long after the doc appears.
64
+ */
65
+ absentStaleTime?: number;
66
+ /**
67
+ * Backoff ladder (ms) for re-reading an id that resolved absent, one re-read per rung
68
+ * per absence episode (default: {@link ABSENT_RETRY_DELAYS_MS}). Pass `[]` to ask for no
69
+ * re-reads.
70
+ *
71
+ * The timer is per cache key, so many consumers of the same absent id still cost one
72
+ * re-read per rung — and consumers that disagree combine by UNION, never by mount order:
73
+ * the key polls if ANY mounted consumer wants polling, on the LONGEST ladder among them.
74
+ * `[]` therefore only silences re-reads that no other mounted consumer asked for. The
75
+ * effective ladder is recomputed on every mount and unmount; an episode already in
76
+ * progress continues on the new ladder's remaining rungs, or stops if it is now empty.
77
+ */
78
+ absentRetryDelaysMs?: readonly number[];
43
79
  };
44
80
  export type BatchFirestoreDocsResult<T> = {
45
81
  /**
46
- * Map of docId -> document data
47
- * Only includes documents that exist in Firestore
82
+ * Map of docId -> document data.
83
+ * Only includes documents that exist AND are currently readable: an id whose query is in
84
+ * an error state is excluded, so a previously cached doc is never presented as readable
85
+ * after access to it fails.
48
86
  */
49
87
  data: Record<string, T>;
50
88
  /**
51
- * True if any batch queries are still loading
89
+ * True while any requested id has not resolved yet
52
90
  */
53
91
  isLoading: boolean;
54
92
  /**
55
- * True if any batch queries have errored
93
+ * True if any requested id is in an error state
56
94
  */
57
95
  isError: boolean;
58
96
  /**
59
- * First error from batch queries, if any
97
+ * First error across the requested ids, if any
60
98
  */
61
99
  error: Error | null;
62
100
  /**
63
- * Refetch all documents (even cached ones)
101
+ * Refetch every requested id, cached ones included. A no-op in subscribe mode.
64
102
  */
65
103
  refetch: () => Promise<void>;
66
104
  };
67
105
  /**
68
- * Efficiently batch fetch Firestore documents with individual caching
106
+ * Resolve many document ids into individually observed, individually cached React Query
107
+ * entries at `[queryKeyPrefix, id]`.
108
+ *
109
+ * There is exactly ONE data cache: the per-id entries. Each id owns a real query with a
110
+ * real `queryFn`, so exact-key invalidation, `refetchQueries({ type: 'active' })`, and
111
+ * `refetchOnMount` all work on a single id the way they work on any other query — and the
112
+ * returned `data` is derived reactively from those query results, never read imperatively.
69
113
  *
70
- * Features:
71
- * - Each document is cached individually for maximum reuse
72
- * - Only fetches documents that are missing or stale
73
- * - Respects Firestore's 30-item limit for 'in' queries
74
- * - Returns combined result as Record<docId, data>
114
+ * A nonexistent document resolves to `null` (negative caching) and is excluded from `data`;
115
+ * an absent id is then re-read on a bounded ladder (see `absentRetryDelaysMs`) so a doc
116
+ * that appears shortly afterwards shows up without an invalidation.
75
117
  *
76
118
  * @example
77
119
  * ```typescript
@@ -80,10 +122,10 @@ export type BatchFirestoreDocsResult<T> = {
80
122
  * collectionPath: 'publicUsers',
81
123
  * ids: ['user1', 'user2', 'user3'],
82
124
  * queryKeyPrefix: 'publicUser',
83
- * staleTime: 30 * 60 * 1000, // 30 minutes
125
+ * staleTime: 30 * 60 * 1000,
84
126
  * });
85
127
  * // users = { user1: {...}, user2: {...}, user3: {...} }
86
128
  * ```
87
129
  */
88
- export declare function useBatchFirestoreDocs<T extends Record<string, any>>({ db, collectionPath, ids, queryKeyPrefix, staleTime, gcTime, enabled, subscribe, }: BatchFirestoreDocsOptions): BatchFirestoreDocsResult<T>;
130
+ export declare function useBatchFirestoreDocs<T extends Record<string, any>>({ db, collectionPath, ids, queryKeyPrefix, staleTime, gcTime, enabled, subscribe, transport, absentStaleTime, absentRetryDelaysMs, }: BatchFirestoreDocsOptions): BatchFirestoreDocsResult<T>;
89
131
  //# sourceMappingURL=useBatchFirestoreDocs.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"useBatchFirestoreDocs.d.ts","sourceRoot":"","sources":["../../../src/react/firestore/useBatchFirestoreDocs.ts"],"names":[],"mappings":"AAIA,OAAO,EAAiD,KAAK,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAKnG,MAAM,MAAM,yBAAyB,GAAG;IACtC;;OAEG;IACH,EAAE,EAAE,SAAS,CAAC;IAEd;;OAEG;IACH,cAAc,EAAE,MAAM,CAAC;IAEvB;;OAEG;IACH,GAAG,EAAE,MAAM,EAAE,CAAC;IAEd;;;;OAIG;IACH,cAAc,EAAE,MAAM,CAAC;IAEvB;;OAEG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IAEnB;;OAEG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;IAEhB;;OAEG;IACH,OAAO,CAAC,EAAE,OAAO,CAAC;IAElB;;;;;;;;OAQG;IACH,SAAS,CAAC,E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AC;IAElB;;;;;;;;OAQG;IACH,SAAS,CAAC,EAAE,OAAO,CAAC;IAEpB;;;;;;;;;;;OAWG;IACH,SAAS,CAAC,EAAE,2BAA2B,CAAC;IAExC;;;;;OAKG;IACH,eAAe,CAAC,EAAE,MAAM,CAAC;IAEzB;;;;;;;;;;;OAWG;IACH,mBAAmB,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;CACzC,CAAC;AAEF,MAAM,MAAM,wBAAwB,CAAC,CAAC,IAAI;IACxC;;;;;OAKG;IACH,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC;IAExB;;OAEG;IACH,SAAS,EAAE,OAAO,CAAC;IAEnB;;OAEG;IACH,OAAO,EAAE,OAAO,CAAC;IAEjB;;OAEG;IACH,KAAK,EAAE,KAAK,GAAG,IAAI,CAAC;IAEpB;;OAEG;IACH,OAAO,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;CAC9B,CAAC;AAWF;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,qBAAqB,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,EACnE,EAAE,EACF,cAAc,EACd,GAAG,EACH,cAAc,EACd,SAA0B,EAC1B,MAAuB,EACvB,OAAc,EACd,SAAiB,EACjB,SAAmB,EACnB,eAA2B,EAC3B,mBAA4C,GAC7C,EAAE,yBAAyB,GAAG,wBAAwB,CAAC,CAAC,CAAC,CA4IzD"}
@@ -1,17 +1,31 @@
1
1
  'use client';
2
- import { useEffect, useMemo, useState } from 'react';
2
+ import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
3
3
  import { useQueries, useQueryClient } from '@tanstack/react-query';
4
- import { collection, getDocs, query, where, documentId } from 'firebase/firestore';
5
- import { subscribeDoc } from './doc-subscription-registry.js';
6
- const FIRESTORE_IN_LIMIT = 30;
4
+ import { isListenerOwned, subscribeDoc } from './doc-subscription-registry.js';
5
+ import { getDocLoader } from './batch-doc-loader.js';
6
+ import { ABSENT_RETRY_DELAYS_MS, trackAbsentDoc } from './absence-scheduler.js';
7
+ function shallowRecordEqual(a, b) {
8
+ const aKeys = Object.keys(a);
9
+ if (aKeys.length !== Object.keys(b).length)
10
+ return false;
11
+ for (const key of aKeys) {
12
+ if (!Object.prototype.hasOwnProperty.call(b, key) || a[key] !== b[key])
13
+ return false;
14
+ }
15
+ return true;
16
+ }
7
17
  /**
8
- * Efficiently batch fetch Firestore documents with individual caching
18
+ * Resolve many document ids into individually observed, individually cached React Query
19
+ * entries at `[queryKeyPrefix, id]`.
20
+ *
21
+ * There is exactly ONE data cache: the per-id entries. Each id owns a real query with a
22
+ * real `queryFn`, so exact-key invalidation, `refetchQueries({ type: 'active' })`, and
23
+ * `refetchOnMount` all work on a single id the way they work on any other query — and the
24
+ * returned `data` is derived reactively from those query results, never read imperatively.
9
25
  *
10
- * Features:
11
- * - Each document is cached individually for maximum reuse
12
- * - Only fetches documents that are missing or stale
13
- * - Respects Firestore's 30-item limit for 'in' queries
14
- * - Returns combined result as Record<docId, data>
26
+ * A nonexistent document resolves to `null` (negative caching) and is excluded from `data`;
27
+ * an absent id is then re-read on a bounded ladder (see `absentRetryDelaysMs`) so a doc
28
+ * that appears shortly afterwards shows up without an invalidation.
15
29
  *
16
30
  * @example
17
31
  * ```typescript
@@ -20,12 +34,12 @@ const FIRESTORE_IN_LIMIT = 30;
20
34
  * collectionPath: 'publicUsers',
21
35
  * ids: ['user1', 'user2', 'user3'],
22
36
  * queryKeyPrefix: 'publicUser',
23
- * staleTime: 30 * 60 * 1000, // 30 minutes
37
+ * staleTime: 30 * 60 * 1000,
24
38
  * });
25
39
  * // users = { user1: {...}, user2: {...}, user3: {...} }
26
40
  * ```
27
41
  */
28
- export function useBatchFirestoreDocs({ db, collectionPath, ids, queryKeyPrefix, staleTime = 30 * 60 * 1000, gcTime = 60 * 60 * 1000, enabled = true, subscribe = false, }) {
42
+ export function useBatchFirestoreDocs({ db, collectionPath, ids, queryKeyPrefix, staleTime = 30 * 60 * 1000, gcTime = 60 * 60 * 1000, enabled = true, subscribe = false, transport = 'batch', absentStaleTime = 30 * 1000, absentRetryDelaysMs = ABSENT_RETRY_DELAYS_MS, }) {
29
43
  const queryClient = useQueryClient();
30
44
  // Stabilize ids reference to prevent cascading re-renders
31
45
  const idsKey = JSON.stringify(ids);
@@ -35,9 +49,40 @@ export function useBatchFirestoreDocs({ db, collectionPath, ids, queryKeyPrefix,
35
49
  return Array.from(new Set(parsed.filter(Boolean)));
36
50
  }, [idsKey]);
37
51
  // ── Real-time subscribe mode ──────────────────────────────────────────────
38
- // Re-render trigger; bumped whenever a subscribed doc's snapshot lands.
39
- const [snapshotVersion, setSnapshotVersion] = useState(0);
52
+ // Listener errors cannot surface through the per-id queries (they are disabled while
53
+ // subscribed), so they are tracked here and merged into the returned result.
40
54
  const [subscribeError, setSubscribeError] = useState(null);
55
+ // Snapshot re-render trigger. The per-id query observers do notify on the listener's
56
+ // cache write, but only on React Query's batched microtask; bumping state in the
57
+ // snapshot callback keeps a landed snapshot visible in the SAME tick it arrives.
58
+ const [, setSnapshotVersion] = useState(0);
59
+ const results = useQueries({
60
+ queries: uniqueIds.map((id) => ({
61
+ queryKey: [queryKeyPrefix, id],
62
+ queryFn: async ({ signal }) => {
63
+ const loader = getDocLoader(queryClient, db, collectionPath);
64
+ const loaded = transport === 'get' ? await loader.loadOne(id, signal) : await loader.loadBatched(id, signal);
65
+ // If a shared listener took ownership of this key while the read was in flight, the
66
+ // listener's value is the newer truth — `onSnapshot` will not re-emit until the doc
67
+ // changes, so returning the read result here would leave a stale value stuck.
68
+ if (isListenerOwned(queryClient, queryKeyPrefix, id)) {
69
+ const owned = queryClient.getQueryData([queryKeyPrefix, id]);
70
+ if (owned !== undefined)
71
+ return owned;
72
+ }
73
+ return loaded;
74
+ },
75
+ enabled: enabled && !subscribe,
76
+ // The consuming app's global default is `refetchOnMount: false`; these lookups opt
77
+ // back in so a remount re-reads a STALE id (refetch-on-mount never fires on a fresh
78
+ // one, so an all-cache-hit mount still costs nothing).
79
+ refetchOnMount: !subscribe,
80
+ staleTime: subscribe
81
+ ? Infinity
82
+ : (query) => query.state.data === null ? absentStaleTime : staleTime,
83
+ gcTime,
84
+ })),
85
+ });
41
86
  useEffect(() => {
42
87
  if (!subscribe || !enabled || uniqueIds.length === 0)
43
88
  return;
@@ -48,110 +93,73 @@ export function useBatchFirestoreDocs({ db, collectionPath, ids, queryKeyPrefix,
48
93
  collectionPath,
49
94
  queryKeyPrefix,
50
95
  id,
51
- onUpdate: () => setSnapshotVersion((v) => v + 1),
96
+ onUpdate: () => setSnapshotVersion((version) => version + 1),
52
97
  onError: (error) => setSubscribeError(error),
53
98
  }));
54
99
  return () => {
55
100
  for (const unsubscribe of unsubscribers)
56
101
  unsubscribe();
57
102
  };
58
- // eslint-disable-next-line react-hooks/exhaustive-deps -- idsKey stands in for uniqueIds
59
- }, [idsKey, subscribe, enabled, collectionPath, queryKeyPrefix, queryClient, db]);
60
- // Separate IDs into cached/fresh vs needs-fetch (one-shot path only)
61
- const { fetchIds } = useMemo(() => {
62
- if (!enabled || subscribe) {
63
- return { fetchIds: [] };
64
- }
65
- const needsFetch = [];
66
- for (const id of uniqueIds) {
67
- const queryKey = [queryKeyPrefix, id];
68
- const state = queryClient.getQueryState(queryKey);
69
- // Check if data exists and is still fresh. `null` counts as data — it marks a
70
- // known-absent doc (negative caching) and must not be re-fetched while fresh.
71
- const hasResolvedData = state !== undefined && state.data !== undefined;
72
- if (!(hasResolvedData && state.dataUpdatedAt && Date.now() - state.dataUpdatedAt < staleTime)) {
73
- needsFetch.push(id);
74
- }
75
- }
76
- return { fetchIds: needsFetch };
77
- }, [uniqueIds, queryKeyPrefix, queryClient, staleTime, enabled, subscribe]);
78
- // Batch the fetchIds into groups of 30
79
- const batches = useMemo(() => {
80
- const result = [];
81
- for (let i = 0; i < fetchIds.length; i += FIRESTORE_IN_LIMIT) {
82
- result.push(fetchIds.slice(i, i + FIRESTORE_IN_LIMIT));
83
- }
84
- return result;
85
- }, [fetchIds]);
86
- // Fetch each batch and populate individual cache entries
87
- const batchQueries = useQueries({
88
- queries: batches.map((batch, batchIndex) => ({
89
- queryKey: [queryKeyPrefix, 'batch', batchIndex, ...[...batch].sort()],
90
- queryFn: async () => {
91
- if (batch.length === 0)
92
- return [];
93
- const q = query(collection(db, collectionPath), where(documentId(), 'in', batch));
94
- const snapshot = await getDocs(q);
95
- const docs = [];
96
- const returnedIds = new Set();
97
- // Process each document and update individual cache entries
98
- snapshot.forEach((doc) => {
99
- const data = { id: doc.id, ...doc.data() };
100
- docs.push(data);
101
- returnedIds.add(doc.id);
102
- // Populate individual cache entry
103
- queryClient.setQueryData([queryKeyPrefix, doc.id], data, { updatedAt: Date.now() });
104
- });
105
- // Negative-cache requested ids that came back missing so they aren't
106
- // re-fetched on every render while fresh (mirrors the subscribe path).
107
- for (const id of batch) {
108
- if (!returnedIds.has(id)) {
109
- queryClient.setQueryData([queryKeyPrefix, id], null, { updatedAt: Date.now() });
110
- }
111
- }
112
- return docs;
113
- },
114
- staleTime,
115
- gcTime,
116
- enabled: enabled && batch.length > 0,
117
- })),
118
- });
119
- // Combine results (one-shot batch path)
120
- const batchIsLoading = batchQueries.some(q => q.isLoading);
121
- const batchIsError = batchQueries.some(q => q.isError);
122
- const batchError = batchQueries.find(q => q.error)?.error || null;
123
- // Trigger re-derivation when batch fetches complete
124
- const batchSettledCount = batchQueries.filter(q => q.isSuccess || q.isError).length;
125
- const batchUpdateTimestamp = batchQueries.reduce((sum, q) => sum + (q.dataUpdatedAt ?? 0), 0);
126
- const data = useMemo(() => {
127
- const combined = {};
128
- // Always read all IDs from individual cache entries. Both paths populate these via
129
- // setQueryData (batch queryFn, or the subscribe listener); we never read batch results
130
- // directly, so IDs can't fall through the cached/fetched split. A `null` entry means a
131
- // doc is known-absent (negative caching, both paths) and is correctly excluded here.
132
- for (const id of uniqueIds) {
133
- const cached = queryClient.getQueryData([queryKeyPrefix, id]);
134
- if (cached) {
135
- combined[id] = cached;
136
- }
103
+ }, [uniqueIds, subscribe, enabled, collectionPath, queryKeyPrefix, queryClient, db]);
104
+ // Bounded re-read ladder for ids that resolved absent. Registration is per cache key, so
105
+ // overlapping consumers share one episode budget. The ladder is rebuilt from its own
106
+ // serialized form so an inline array literal cannot re-register (and reset it) every render.
107
+ const absentDelaysKey = absentRetryDelaysMs.join(',');
108
+ const absentDelays = useMemo(() => absentDelaysKey.split(',').filter(Boolean).map(Number), [absentDelaysKey]);
109
+ useEffect(() => {
110
+ if (uniqueIds.length === 0)
111
+ return;
112
+ const releases = uniqueIds.map((id) => trackAbsentDoc({ queryClient, queryKey: [queryKeyPrefix, id], delays: absentDelays }));
113
+ return () => {
114
+ for (const release of releases)
115
+ release();
116
+ };
117
+ }, [uniqueIds, queryKeyPrefix, queryClient, absentDelays]);
118
+ const nextData = {};
119
+ let anyError = null;
120
+ let oneShotLoading = false;
121
+ let anyUnresolved = false;
122
+ for (let index = 0; index < uniqueIds.length; index += 1) {
123
+ const result = results[index];
124
+ if (!result)
125
+ continue;
126
+ if (result.isError) {
127
+ anyError ??= result.error ?? null;
128
+ continue;
137
129
  }
138
- return combined;
139
- // batchSettledCount/batchUpdateTimestamp/snapshotVersion are intentional re-derivation triggers:
140
- // the cache is read imperatively via getQueryData (non-reactive), so these counters force a
141
- // recompute when a batch fetch settles or a subscribed doc updates.
142
- // eslint-disable-next-line react-hooks/exhaustive-deps
143
- }, [uniqueIds, queryKeyPrefix, queryClient, batchSettledCount, batchUpdateTimestamp, snapshotVersion]);
144
- // In subscribe mode the batch queries are inert; "loading" means a subscribed id has not
145
- // received its first snapshot yet (no cache entry — `null` counts as resolved-missing).
146
- const subscribeIsLoading = subscribe && enabled && uniqueIds.some((id) => queryClient.getQueryData([queryKeyPrefix, id]) === undefined);
147
- const refetch = async () => {
148
- await Promise.all(batchQueries.map(q => q.refetch()));
149
- };
130
+ if (result.isLoading)
131
+ oneShotLoading = true;
132
+ const value = result.data;
133
+ if (value === undefined)
134
+ anyUnresolved = true;
135
+ else if (value)
136
+ nextData[uniqueIds[index]] = value;
137
+ }
138
+ // The map is rebuilt every render; keep the last COMMITTED object while it is unchanged so
139
+ // consumers can safely depend on `data`'s identity. Only the effect writes the ref, so a
140
+ // concurrent render React discards can never leave it pointing at an uncommitted map.
141
+ const committedDataRef = useRef(nextData);
142
+ const data = shallowRecordEqual(committedDataRef.current, nextData)
143
+ ? committedDataRef.current
144
+ : nextData;
145
+ useEffect(() => {
146
+ committedDataRef.current = data;
147
+ }, [data]);
148
+ const refetch = useCallback(async () => {
149
+ // Subscribe mode owns its freshness through the shared listeners — refetching would
150
+ // issue reads against disabled queries and race the listener's value.
151
+ if (subscribe || !enabled)
152
+ return;
153
+ await Promise.all(uniqueIds.map((id) => queryClient.refetchQueries({ queryKey: [queryKeyPrefix, id], exact: true })));
154
+ }, [uniqueIds, subscribe, enabled, queryClient, queryKeyPrefix]);
155
+ // In subscribe mode "loading" means a subscribed id has no cache entry yet (`null`
156
+ // counts as resolved-missing).
157
+ const isLoading = subscribe ? enabled && anyUnresolved : oneShotLoading;
150
158
  return {
151
159
  data,
152
- isLoading: batchIsLoading || subscribeIsLoading,
153
- isError: batchIsError || subscribeError !== null,
154
- error: batchError ?? subscribeError,
160
+ isLoading,
161
+ isError: anyError !== null || subscribeError !== null,
162
+ error: anyError ?? subscribeError,
155
163
  refetch,
156
164
  };
157
165
  }
@@ -1 +1 @@
1
- {"version":3,"file":"useBatchFirestoreDocs.js","sourceRoot":"","sources":["../../../src/react/firestore/useBatchFirestoreDocs.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AACrD,OAAO,EAAE,UAAU,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AACnE,OAAO,EAAE,UAAU,EAAE,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,UAAU,EAAkB,M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pF,sEAAsE;QACtE,IAAI,SAAS,IAAI,CAAC,OAAO;YAAE,OAAO;QAClC,MAAM,OAAO,CAAC,GAAG,CACf,SAAS,CAAC,GAAG,CAAC,CAAC,EAAE,EAAE,EAAE,CACnB,WAAW,CAAC,cAAc,CAAC,EAAE,QAAQ,EAAE,CAAC,cAAc,EAAE,EAAE,CAAC,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAC5E,CACF,CAAC;IACJ,CAAC,EAAE,CAAC,SAAS,EAAE,SAAS,EAAE,OAAO,EAAE,WAAW,EAAE,cAAc,CAAC,CAAC,CAAC;IAEjE,mFAAmF;IACnF,+BAA+B;IAC/B,MAAM,SAAS,GAAG,SAAS,CAAC,CAAC,CAAC,OAAO,IAAI,aAAa,CAAC,CAAC,CAAC,cAAc,CAAC;IAExE,OAAO;QACL,IAAI;QACJ,SAAS;QACT,OAAO,EAAE,QAAQ,KAAK,IAAI,IAAI,cAAc,KAAK,IAAI;QACrD,KAAK,EAAE,QAAQ,IAAI,cAAc;QACjC,OAAO;KACR,CAAC;AACJ,CAAC","sourcesContent":["'use client';\n\nimport { useCallback, useEffect, useMemo, useRef, useState } from 'react';\nimport { useQueries, useQueryClient } from '@tanstack/react-query';\nimport type { Firestore } from 'firebase/firestore';\nimport { isListenerOwned, subscribeDoc } from './doc-subscription-registry.js';\nimport { getDocLoader } from './batch-doc-loader.js';\nimport { ABSENT_RETRY_DELAYS_MS, trackAbsentDoc } from './absence-scheduler.js';\n\n/** Transport used to resolve an id in one-shot mode. */\nexport type BatchFirestoreDocsTransport = 'batch' | 'get';\n\nexport type BatchFirestoreDocsOptions = {\n /**\n * Firestore instance to use for queries\n */\n db: Firestore;\n\n /**\n * Full collection path (e.g., 'publicUsers', 'entities/public')\n */\n collectionPath: string;\n\n /**\n * Array of document IDs to fetch\n */\n ids: string[];\n\n /**\n * Query key prefix for individual document cache entries\n * Each document is cached as [queryKeyPrefix, docId]\n * @example 'publicUser' -> ['publicUser', 'user123']\n */\n queryKeyPrefix: string;\n\n /**\n * Time in milliseconds a PRESENT document stays fresh (default: 30 minutes).\n * An absent document uses `absentStaleTime` instead.\n */\n staleTime?: number;\n\n /**\n * Time in milliseconds to keep unused data in cache (default: 1 hour)\n */\n gcTime?: number;\n\n /**\n * Whether queries should be enabled (default: true)\n */\n enabled?: boolean;\n\n /**\n * Real-time mode (default: false). When true, each id is resolved via a shared,\n * reference-counted `onSnapshot` listener instead of a one-shot read:\n * - a missing doc caches `null` (negative caching) and resolves the instant it appears,\n * - identity edits propagate live, and every browser tab stays consistent,\n * - `staleTime` is irrelevant (listeners keep the cache fresh) and `refetch()` is a no-op.\n * Initial read cost matches the one-shot path; listeners then stay open while mounted.\n * Use for small, frequently-rendered, change-sensitive docs owned by the signed-in user.\n */\n subscribe?: boolean;\n\n /**\n * One-shot transport (default: `'batch'`).\n *\n * - `'batch'` — ids enqueued in the same microtask are coalesced into\n * `where(documentId(), 'in', …)` queries of at most 30 ids each. Fewest round-trips.\n * - `'get'` — one `getDoc` per id, run concurrently. Required for a collection whose\n * Firestore rules gate reads on `resource.data`: an unconstrained id-list query is\n * denied WHOLESALE there, while per-document gets are evaluated per document, so one\n * unreadable id errors alone instead of failing every id with it. On such a collection\n * a get against a nonexistent doc is also denied, so \"absent\" and \"hidden\" are the same\n * client-visible signal — the id surfaces as that query's error, never as a null.\n */\n transport?: BatchFirestoreDocsTransport;\n\n /**\n * Time in milliseconds an ABSENT (`null`) result stays fresh (default: 30 seconds).\n * Deliberately much shorter than `staleTime`: a missing doc is usually a doc that has\n * not been mirrored/created YET, and holding \"missing\" as fresh for the full staleTime\n * is what leaves a row blank long after the doc appears.\n */\n absentStaleTime?: number;\n\n /**\n * Backoff ladder (ms) for re-reading an id that resolved absent, one re-read per rung\n * per absence episode (default: {@link ABSENT_RETRY_DELAYS_MS}). Pass `[]` to ask for no\n * re-reads.\n *\n * The timer is per cache key, so many consumers of the same absent id still cost one\n * re-read per rung — and consumers that disagree combine by UNION, never by mount order:\n * the key polls if ANY mounted consumer wants polling, on the LONGEST ladder among them.\n * `[]` therefore only silences re-reads that no other mounted consumer asked for. The\n * effective ladder is recomputed on every mount and unmount; an episode already in\n * progress continues on the new ladder's remaining rungs, or stops if it is now empty.\n */\n absentRetryDelaysMs?: readonly number[];\n};\n\nexport type BatchFirestoreDocsResult<T> = {\n /**\n * Map of docId -> document data.\n * Only includes documents that exist AND are currently readable: an id whose query is in\n * an error state is excluded, so a previously cached doc is never presented as readable\n * after access to it fails.\n */\n data: Record<string, T>;\n\n /**\n * True while any requested id has not resolved yet\n */\n isLoading: boolean;\n\n /**\n * True if any requested id is in an error state\n */\n isError: boolean;\n\n /**\n * First error across the requested ids, if any\n */\n error: Error | null;\n\n /**\n * Refetch every requested id, cached ones included. A no-op in subscribe mode.\n */\n refetch: () => Promise<void>;\n};\n\nfunction shallowRecordEqual<T>(a: Record<string, T>, b: Record<string, T>): boolean {\n const aKeys = Object.keys(a);\n if (aKeys.length !== Object.keys(b).length) return false;\n for (const key of aKeys) {\n if (!Object.prototype.hasOwnProperty.call(b, key) || a[key] !== b[key]) return false;\n }\n return true;\n}\n\n/**\n * Resolve many document ids into individually observed, individually cached React Query\n * entries at `[queryKeyPrefix, id]`.\n *\n * There is exactly ONE data cache: the per-id entries. Each id owns a real query with a\n * real `queryFn`, so exact-key invalidation, `refetchQueries({ type: 'active' })`, and\n * `refetchOnMount` all work on a single id the way they work on any other query — and the\n * returned `data` is derived reactively from those query results, never read imperatively.\n *\n * A nonexistent document resolves to `null` (negative caching) and is excluded from `data`;\n * an absent id is then re-read on a bounded ladder (see `absentRetryDelaysMs`) so a doc\n * that appears shortly afterwards shows up without an invalidation.\n *\n * @example\n * ```typescript\n * const { data: users, isLoading } = useBatchFirestoreDocs<PublicUser>({\n * db,\n * collectionPath: 'publicUsers',\n * ids: ['user1', 'user2', 'user3'],\n * queryKeyPrefix: 'publicUser',\n * staleTime: 30 * 60 * 1000,\n * });\n * // users = { user1: {...}, user2: {...}, user3: {...} }\n * ```\n */\nexport function useBatchFirestoreDocs<T extends Record<string, any>>({\n db,\n collectionPath,\n ids,\n queryKeyPrefix,\n staleTime = 30 * 60 * 1000,\n gcTime = 60 * 60 * 1000,\n enabled = true,\n subscribe = false,\n transport = 'batch',\n absentStaleTime = 30 * 1000,\n absentRetryDelaysMs = ABSENT_RETRY_DELAYS_MS,\n}: BatchFirestoreDocsOptions): BatchFirestoreDocsResult<T> {\n const queryClient = useQueryClient();\n\n // Stabilize ids reference to prevent cascading re-renders\n const idsKey = JSON.stringify(ids);\n\n // Deduplicate and filter out empty IDs\n const uniqueIds = useMemo(() => {\n const parsed: string[] = JSON.parse(idsKey);\n return Array.from(new Set(parsed.filter(Boolean)));\n }, [idsKey]);\n\n // ── Real-time subscribe mode ──────────────────────────────────────────────\n // Listener errors cannot surface through the per-id queries (they are disabled while\n // subscribed), so they are tracked here and merged into the returned result.\n const [subscribeError, setSubscribeError] = useState<Error | null>(null);\n // Snapshot re-render trigger. The per-id query observers do notify on the listener's\n // cache write, but only on React Query's batched microtask; bumping state in the\n // snapshot callback keeps a landed snapshot visible in the SAME tick it arrives.\n const [, setSnapshotVersion] = useState(0);\n\n const results = useQueries({\n queries: uniqueIds.map((id) => ({\n queryKey: [queryKeyPrefix, id],\n queryFn: async ({ signal }: { signal: AbortSignal }): Promise<T | null> => {\n const loader = getDocLoader(queryClient, db, collectionPath);\n const loaded =\n transport === 'get' ? await loader.loadOne(id, signal) : await loader.loadBatched(id, signal);\n // If a shared listener took ownership of this key while the read was in flight, the\n // listener's value is the newer truth — `onSnapshot` will not re-emit until the doc\n // changes, so returning the read result here would leave a stale value stuck.\n if (isListenerOwned(queryClient, queryKeyPrefix, id)) {\n const owned = queryClient.getQueryData([queryKeyPrefix, id]);\n if (owned !== undefined) return owned as T | null;\n }\n return loaded as T | null;\n },\n enabled: enabled && !subscribe,\n // The consuming app's global default is `refetchOnMount: false`; these lookups opt\n // back in so a remount re-reads a STALE id (refetch-on-mount never fires on a fresh\n // one, so an all-cache-hit mount still costs nothing).\n refetchOnMount: !subscribe,\n staleTime: subscribe\n ? Infinity\n : (query: { state: { data: unknown } }) =>\n query.state.data === null ? absentStaleTime : staleTime,\n gcTime,\n })),\n });\n\n useEffect(() => {\n if (!subscribe || !enabled || uniqueIds.length === 0) return;\n setSubscribeError(null);\n const unsubscribers = uniqueIds.map((id) =>\n subscribeDoc({\n queryClient,\n db,\n collectionPath,\n queryKeyPrefix,\n id,\n onUpdate: () => setSnapshotVersion((version) => version + 1),\n onError: (error) => setSubscribeError(error),\n }),\n );\n return () => {\n for (const unsubscribe of unsubscribers) unsubscribe();\n };\n }, [uniqueIds, subscribe, enabled, collectionPath, queryKeyPrefix, queryClient, db]);\n\n // Bounded re-read ladder for ids that resolved absent. Registration is per cache key, so\n // overlapping consumers share one episode budget. The ladder is rebuilt from its own\n // serialized form so an inline array literal cannot re-register (and reset it) every render.\n const absentDelaysKey = absentRetryDelaysMs.join(',');\n const absentDelays = useMemo(\n () => absentDelaysKey.split(',').filter(Boolean).map(Number),\n [absentDelaysKey],\n );\n\n useEffect(() => {\n if (uniqueIds.length === 0) return;\n const releases = uniqueIds.map((id) =>\n trackAbsentDoc({ queryClient, queryKey: [queryKeyPrefix, id], delays: absentDelays }),\n );\n return () => {\n for (const release of releases) release();\n };\n }, [uniqueIds, queryKeyPrefix, queryClient, absentDelays]);\n\n const nextData: Record<string, T> = {};\n let anyError: Error | null = null;\n let oneShotLoading = false;\n let anyUnresolved = false;\n\n for (let index = 0; index < uniqueIds.length; index += 1) {\n const result = results[index];\n if (!result) continue;\n if (result.isError) {\n anyError ??= (result.error as Error | null) ?? null;\n continue;\n }\n if (result.isLoading) oneShotLoading = true;\n const value = result.data as T | null | undefined;\n if (value === undefined) anyUnresolved = true;\n else if (value) nextData[uniqueIds[index]] = value;\n }\n\n // The map is rebuilt every render; keep the last COMMITTED object while it is unchanged so\n // consumers can safely depend on `data`'s identity. Only the effect writes the ref, so a\n // concurrent render React discards can never leave it pointing at an uncommitted map.\n const committedDataRef = useRef<Record<string, T>>(nextData);\n const data = shallowRecordEqual(committedDataRef.current, nextData)\n ? committedDataRef.current\n : nextData;\n\n useEffect(() => {\n committedDataRef.current = data;\n }, [data]);\n\n const refetch = useCallback(async () => {\n // Subscribe mode owns its freshness through the shared listeners — refetching would\n // issue reads against disabled queries and race the listener's value.\n if (subscribe || !enabled) return;\n await Promise.all(\n uniqueIds.map((id) =>\n queryClient.refetchQueries({ queryKey: [queryKeyPrefix, id], exact: true }),\n ),\n );\n }, [uniqueIds, subscribe, enabled, queryClient, queryKeyPrefix]);\n\n // In subscribe mode \"loading\" means a subscribed id has no cache entry yet (`null`\n // counts as resolved-missing).\n const isLoading = subscribe ? enabled && anyUnresolved : oneShotLoading;\n\n return {\n data,\n isLoading,\n isError: anyError !== null || subscribeError !== null,\n error: anyError ?? subscribeError,\n refetch,\n };\n}\n"]}
@@ -15,6 +15,7 @@ export { useFirestoreLiveInfinite } from './firestore/use-firestore-live-infinit
15
15
  export type { FirestoreLiveInfiniteResult } from './firestore/use-firestore-live-infinite.js';
16
16
  export { useFirestoreSet, useFirestoreUpdate, useFirestoreDelete, useFirestoreBatch, } from './firestore/use-firestore-mutations.js';
17
17
  export { useBatchFirestoreDocs, } from './firestore/useBatchFirestoreDocs.js';
18
- export type { BatchFirestoreDocsOptions, BatchFirestoreDocsResult, } from './firestore/useBatchFirestoreDocs.js';
18
+ export type { BatchFirestoreDocsOptions, BatchFirestoreDocsResult, BatchFirestoreDocsTransport, } from './firestore/useBatchFirestoreDocs.js';
19
+ export { ABSENT_RETRY_DELAYS_MS } from './firestore/absence-scheduler.js';
19
20
  export { useFirestoreSearch } from './search/use-firestore-search.js';
20
21
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/react/index.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AACvD,YAAY,EAAE,wBA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AAC;AAC9C,YAAY,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAC;AAGxD,OAAO,EAAE,iBAAiB,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAC;AAC3E,YAAY,EAAE,sBAAsB,EAAE,MAAM,wBAAwB,CAAC;AAGrE,OAAO,EAAE,eAAe,EAAE,MAAM,kCAAkC,CAAC;AACnE,OAAO,EAAE,sBAAsB,EAAE,MAAM,yCAAyC,CAAC;AACjF,YAAY,EAAE,oBAAoB,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AACnF,OAAO,EAAE,oBAAoB,EAAE,MAAM,uCAAuC,CAAC;AAC7E,OAAO,EAAE,qBAAqB,EAAE,MAAM,wCAAwC,CAAC;AAC/E,YAAY,EAAE,2BAA2B,EAAE,MAAM,wCAAwC,CAAC;AAC1F,OAAO,EAAE,iBAAiB,EAAE,MAAM,oCAAoC,CAAC;AACvE,OAAO,EAAE,wBAAwB,EAAE,MAAM,4CAA4C,CAAC;AACtF,YAAY,EAAE,2BAA2B,EAAE,MAAM,4CAA4C,CAAC;AAG9F,OAAO,EACL,eAAe,EACf,kBAAkB,EAClB,kBAAkB,EAClB,iBAAiB,GAClB,MAAM,wCAAwC,CAAC;AAGhD,OAAO,EACL,qBAAqB,GACtB,MAAM,sCAAsC,CAAC;AAC9C,YAAY,EACV,yBAAyB,EACzB,wBAAwB,EACxB,2BAA2B,GAC5B,MAAM,sCAAsC,CAAC;AAC9C,OAAO,EAAE,sBAAsB,EAAE,MAAM,kCAAkC,CAAC;AAG1E,OAAO,EAAE,kBAAkB,EAAE,MAAM,kCAAkC,CAAC"}
@@ -16,6 +16,7 @@ export { useFirestoreLiveInfinite } from './firestore/use-firestore-live-infinit
16
16
  export { useFirestoreSet, useFirestoreUpdate, useFirestoreDelete, useFirestoreBatch, } from './firestore/use-firestore-mutations.js';
17
17
  // Batch Firestore Docs
18
18
  export { useBatchFirestoreDocs, } from './firestore/useBatchFirestoreDocs.js';
19
+ export { ABSENT_RETRY_DELAYS_MS } from './firestore/absence-scheduler.js';
19
20
  // Search Hook
20
21
  export { useFirestoreSearch } from './search/use-firestore-search.js';
21
22
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/react/index.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,qEAAqE;AACrE,OAAO,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AAGvD,WAAW;AACX,OAAO,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAG9C,qBAAqB;AACrB,OAAO,EAAE,iBAAiB,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAC;AAG3E,wBAAwB;AACxB,OAAO,EAAE,eAAe,EAAE,MAAM,kCAAkC,CAAC;AACnE,OAAO,EAAE,sBAAsB,EAAE,MAAM,yCAAyC,CAAC;AAEjF,OAAO,EAAE,oBAAoB,EAAE,MAAM,uCAAuC,CAAC;AAC7E,OAAO,EAAE,qBAAqB,EAAE,MAAM,wCAAwC,CAAC;AAE/E,OAAO,EAAE,iBAAiB,EAAE,MAAM,oCAAoC,CAAC;AACvE,OAAO,EAAE,wBAAwB,EAAE,MAAM,4CAA4C,CAAC;AAGtF,2BAA2B;AAC3B,OAAO,EACL,eAAe,EACf,kBAAkB,EAClB,kBAAkB,EAClB,iBAAiB,GAClB,MAAM,wCAAwC,CAAC;AAEhD,uBAAuB;AACvB,OAAO,EACL,qBAAqB,GACtB,MAAM,sCAAsC,CAAC;AAM9C,cAAc;AACd,OAAO,EAAE,kBAAkB,EAAE,MAAM,kCAAkC,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/react/index.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,qEAAqE;AACrE,OAAO,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AAGvD,WAAW;AACX,OAAO,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAG9C,qBAAqB;AACrB,OAAO,EAAE,iBAAiB,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAC;AAG3E,wBAAwB;AACxB,OAAO,EAAE,eAAe,EAAE,MAAM,kCAAkC,CAAC;AACnE,OAAO,EAAE,sBAAsB,EAAE,MAAM,yCAAyC,CAAC;AAEjF,OAAO,EAAE,oBAAoB,EAAE,MAAM,uCAAuC,CAAC;AAC7E,OAAO,EAAE,qBAAqB,EAAE,MAAM,wCAAwC,CAAC;AAE/E,OAAO,EAAE,iBAAiB,EAAE,MAAM,oCAAoC,CAAC;AACvE,OAAO,EAAE,wBAAwB,EAAE,MAAM,4CAA4C,CAAC;AAGtF,2BAA2B;AAC3B,OAAO,EACL,eAAe,EACf,kBAAkB,EAClB,kBAAkB,EAClB,iBAAiB,GAClB,MAAM,wCAAwC,CAAC;AAEhD,uBAAuB;AACvB,OAAO,EACL,qBAAqB,GACtB,MAAM,sCAAsC,CAAC;AAM9C,OAAO,EAAE,sBAAsB,EAAE,MAAM,kCAAkC,CAAC;AAE1E,cAAc;AACd,OAAO,EAAE,kBAAkB,EAAE,MAAM,kCAAkC,CAAC","sourcesContent":["\"use client\";\n\n// Query Client factory (client-only; requires @tanstack/react-query)\nexport { createQueryClient } from '../query-client.js';\nexport type { CreateQueryClientOptions } from '../query-client.js';\n\n// Provider\nexport { QueryProvider } from './provider.js';\nexport type { QueryProviderProps } from './provider.js';\n\n// Firestore Provider\nexport { FirestoreProvider, useFirestoreDb } from './firestore/context.js';\nexport type { FirestoreProviderProps } from './firestore/context.js';\n\n// Firestore Query Hooks\nexport { useFirestoreDoc } from './firestore/use-firestore-doc.js';\nexport { useFirestoreCollection } from './firestore/use-firestore-collection.js';\nexport type { FirestoreSourceState, WithSourceState } from '../firestore/types.js';\nexport { useFirestoreInfinite } from './firestore/use-firestore-infinite.js';\nexport { useFirestorePaginated } from './firestore/use-firestore-paginated.js';\nexport type { UseFirestorePaginatedResult } from './firestore/use-firestore-paginated.js';\nexport { useFirestoreCount } from './firestore/use-firestore-count.js';\nexport { useFirestoreLiveInfinite } from './firestore/use-firestore-live-infinite.js';\nexport type { FirestoreLiveInfiniteResult } from './firestore/use-firestore-live-infinite.js';\n\n// Firestore Mutation Hooks\nexport {\n useFirestoreSet,\n useFirestoreUpdate,\n useFirestoreDelete,\n useFirestoreBatch,\n} from './firestore/use-firestore-mutations.js';\n\n// Batch Firestore Docs\nexport {\n useBatchFirestoreDocs,\n} from './firestore/useBatchFirestoreDocs.js';\nexport type {\n BatchFirestoreDocsOptions,\n BatchFirestoreDocsResult,\n BatchFirestoreDocsTransport,\n} from './firestore/useBatchFirestoreDocs.js';\nexport { ABSENT_RETRY_DELAYS_MS } from './firestore/absence-scheduler.js';\n\n// Search Hook\nexport { useFirestoreSearch } from './search/use-firestore-search.js';\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"provider.js","sourceRoot":"","sources":["../../src/react/provider.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAC;;AAEb,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAC5D,OAAO,EAAE,iBAAiB,EAAiC,MAAM,oBAAoB,CAAC;AAOtF;;;GAGG;AACH,MAAM,UAAU,aAAa,CAAC,EAAE,QAAQ,EAAE,aAAa,EAAsB;IAC3E,MAAM,CAAC,MAAM,CAAC,GAAG,KAAK,CAAC,QAAQ,CAAC,GAAG,EAAE,CAAC,iBAAiB,CAAC,aAAa,CAAC,CAAC,CAAC;IACxE,OAAO,KAAC,mBAAmB,IAAC,MAAM,EAAE,MAAM,YAAG,QAAQ,GAAuB,CAAC;AAC/E,CAAC"}
1
+ {"version":3,"file":"provider.js","sourceRoot":"","sources":["../../src/react/provider.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAC;;AAEb,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAC5D,OAAO,EAAE,iBAAiB,EAAiC,MAAM,oBAAoB,CAAC;AAOtF;;;GAGG;AACH,MAAM,UAAU,aAAa,CAAC,EAAE,QAAQ,EAAE,aAAa,EAAsB;IAC3E,MAAM,CAAC,MAAM,CAAC,GAAG,KAAK,CAAC,QAAQ,CAAC,GAAG,EAAE,CAAC,iBAAiB,CAAC,aAAa,CAAC,CAAC,CAAC;IACxE,OAAO,KAAC,mBAAmB,IAAC,MAAM,EAAE,MAAM,YAAG,QAAQ,GAAuB,CAAC;AAC/E,CAAC","sourcesContent":["'use client';\n\nimport React from 'react';\nimport { QueryClientProvider } from '@tanstack/react-query';\nimport { createQueryClient, type CreateQueryClientOptions } from '../query-client.js';\n\nexport type QueryProviderProps = React.PropsWithChildren<{\n /** Optional overrides applied when the client is first created. */\n clientOptions?: CreateQueryClientOptions;\n}>;\n\n/**\n * Client component wrapper for Next.js / React apps.\n * Creates a single QueryClient per browser session.\n */\nexport function QueryProvider({ children, clientOptions }: QueryProviderProps) {\n const [client] = React.useState(() => createQueryClient(clientOptions));\n return <QueryClientProvider client={client}>{children}</QueryClientProvider>;\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"use-firestore-search.js","sourceRoot":"","sources":["../../../src/react/search/use-firestore-search.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAC5C,OAAO,EAAE,QAAQ,EAAuB,MAAM,uBAAuB,CAAC;AACtE,OAAO,EACL,UAAU,EACV,OAAO,EACP,KAAK,EACL,KAAK,EACL,OAAO,EACP,KAAK,IAAI,cAAc,GAGxB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,cAAc,EAAE,MAAM,yBAAyB,CAAC;AACzD,OAAO,EAAE,IAAI,EAAE,MAAM,eAAe,CAAC;AAIrC,MAAM,aAAa,GAAG,CAAC,CAAC;AACxB,MAAM,mBAAmB,GAAG,GAAG,CAAC;AAChC,MAAM,kBAAkB,GAAG,EAAE,G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","sourcesContent":["'use client';\n\nimport { useState, useEffect } from 'react';\nimport { useQuery, type UseQueryResult } from '@tanstack/react-query';\nimport {\n collection,\n getDocs,\n query,\n where,\n orderBy,\n limit as firestoreLimit,\n type DocumentData,\n type QueryConstraint,\n} from 'firebase/firestore';\nimport { useFirestoreDb } from '../firestore/context.js';\nimport { keys } from '../../keys.js';\nimport type { FirestoreSearchOptions } from '../../search/types.js';\nimport type { WithId } from '../../firestore/types.js';\n\nconst DEFAULT_LIMIT = 5;\nconst DEFAULT_DEBOUNCE_MS = 300;\nconst DEFAULT_STALE_TIME = 60 * 1000; // 1 minute\n\n/**\n * Generic Firestore search hook with debouncing and case-insensitive prefix matching.\n * Automatically normalizes search text to lowercase and uses Firestore range queries.\n *\n * Features:\n * - Debounced input (default 300ms)\n * - Case-insensitive search\n * - Minimum 3 character requirement\n * - Prefix matching with '\\uf8ff' suffix\n * - Configurable result limit\n *\n * @example\n * ```typescript\n * const { data, isLoading } = useFirestoreSearch<UserProfile>({\n * collectionPath: 'userProfiles',\n * searchField: 'username',\n * queryText: searchValue,\n * });\n *\n * const { data } = useFirestoreSearch<Team>({\n * collectionPath: 'teams',\n * searchField: 'name_lowercase',\n * queryText: searchValue,\n * limit: 10,\n * });\n * ```\n */\nexport function useFirestoreSearch<T extends DocumentData = DocumentData>({\n collectionPath,\n searchField,\n queryText,\n limit = DEFAULT_LIMIT,\n enabled = true,\n select,\n debounceMs = DEFAULT_DEBOUNCE_MS,\n staleTime = DEFAULT_STALE_TIME,\n equalityFilters = [],\n}: FirestoreSearchOptions<T>): UseQueryResult<WithId<T>[], Error> {\n const db = useFirestoreDb();\n const [debouncedQuery, setDebouncedQuery] = useState(queryText);\n\n // Debounce the search query\n useEffect(() => {\n const timer = setTimeout(() => {\n setDebouncedQuery(queryText);\n }, debounceMs);\n\n return () => clearTimeout(timer);\n }, [queryText, debounceMs]);\n\n // Normalize query text\n const normalizedQuery = debouncedQuery.toLowerCase().trim();\n const shouldSearch = normalizedQuery.length >= 3 && enabled;\n\n return useQuery({\n queryKey: keys.custom('search', collectionPath, searchField, normalizedQuery, JSON.stringify(equalityFilters)),\n queryFn: async (): Promise<WithId<T>[]> => {\n if (!shouldSearch) {\n return [];\n }\n\n const collectionRef = collection(db, collectionPath);\n\n // Build range query for prefix matching\n // Using '\\uf8ff' as upper bound creates a range that matches all strings starting with the query\n const constraints: QueryConstraint[] = [\n ...equalityFilters.map((f) => where(f.field, '==', f.value)),\n where(searchField, '>=', normalizedQuery),\n where(searchField, '<=', normalizedQuery + '\\uf8ff'),\n orderBy(searchField, 'asc'),\n firestoreLimit(limit),\n ];\n\n const q = query(collectionRef, ...constraints);\n const snapshot = await getDocs(q);\n\n return snapshot.docs.map((docSnap) => {\n const rawData = docSnap.data();\n const dataWithId = { id: docSnap.id, ...rawData };\n const data = select ? select(dataWithId) : dataWithId;\n return data as WithId<T>;\n });\n },\n enabled: shouldSearch,\n staleTime,\n });\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/search/types.ts"],"names":[],"mappings":""}
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/search/types.ts"],"names":[],"mappings":"","sourcesContent":["import type { DocumentData } from 'firebase/firestore';\n\n/**\n * Equality filter applied before the prefix range in a Firestore search.\n * Each becomes a `where(field, '==', value)` constraint, ordered ahead of the\n * search field. The composite index must list these equality fields (ASC)\n * before the search field (ASC).\n */\nexport interface SearchEqualityFilter {\n field: string;\n value: unknown;\n}\n\n/**\n * Configuration for a Firestore search query\n */\nexport interface FirestoreSearchConfig {\n /** Firestore collection path to search */\n collectionPath: string;\n /** Field name to search on (must be indexed and lowercase) */\n searchField: string;\n /** Maximum number of results to return */\n limit: number;\n /** Optional equality filters applied before the prefix range. */\n equalityFilters?: SearchEqualityFilter[];\n}\n\n/**\n * Options for useFirestoreSearch hook\n */\nexport interface FirestoreSearchOptions<T extends DocumentData = DocumentData> {\n /** Firestore collection path to search */\n collectionPath: string;\n /** Field name to search on (must be indexed and lowercase) */\n searchField: string;\n /** Search query text */\n queryText: string;\n /** Maximum number of results (default: 5) */\n limit?: number;\n /** Enable/disable the search (default: true) */\n enabled?: boolean;\n /** Transform function to convert raw Firestore data to typed result */\n select?: (data: DocumentData & { id: string }) => T;\n /** Debounce delay in milliseconds (default: 300) */\n debounceMs?: number;\n /** React Query stale time (default: 60000ms) */\n staleTime?: number;\n /** Optional equality filters applied before the prefix range. */\n equalityFilters?: SearchEqualityFilter[];\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ttt-productions/query-core",
3
- "version": "0.14.0",
3
+ "version": "0.14.2",
4
4
  "description": "Shared TanStack Query (React Query) client defaults, query key helpers, and Firestore integration hooks for TTT Productions apps",
5
5
  "repository": {
6
6
  "type": "git",