Skip to content

Commit 9406071

Browse files
committed
feat(link): add ignoreCachedChanges ref option (close vuejs#1324)
1 parent 83f4e09 commit 9406071

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

src/firestore/bind.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ import { onSnapshot } from 'firebase/firestore'
2929
* Options when binding a Firestore document or collection.
3030
*/
3131
export interface FirestoreRefOptions extends _DataSourceOptions {
32+
/**
33+
* Ignores snapshot changes that are coming from the Firestore client cache, defering ref updates until fresh data
34+
* is pulled from the server. Useful for pagination scenarios, when using multiple listeners on the same collection
35+
* with distinct queries and updating query refs.
36+
*/
37+
ignoreCachedChanges?: boolean
38+
3239
/**
3340
* The maximum depth to bind nested refs. A nested ref that isn't bound will stay as the ref path while a bound ref
3441
* will contain the same data as if the ref was bound directly.
@@ -60,11 +67,17 @@ export interface _FirestoreRefOptionsWithDefaults extends FirestoreRefOptions {
6067
* @defaultValue `false`
6168
*/
6269
reset: ResetOption
70+
6371
/**
6472
* @defaultValue `true`
6573
*/
6674
wait: boolean
6775

76+
/**
77+
* @defaultValue `false`
78+
*/
79+
ignoreCachedChanges: boolean
80+
6881
/**
6982
* @defaultValue `2`
7083
*/
@@ -88,6 +101,7 @@ export interface _FirestoreRefOptionsWithDefaults extends FirestoreRefOptions {
88101
const DEFAULT_OPTIONS: _FirestoreRefOptionsWithDefaults = {
89102
reset: false,
90103
wait: true,
104+
ignoreCachedChanges: false,
91105
maxRefDepth: 2,
92106
converter: firestoreDefaultConverter,
93107
snapshotOptions: { serverTimestamps: 'estimate' },
@@ -380,8 +394,10 @@ export function bindCollection<T = unknown>(
380394
// (https://firebase.google.com/docs/firestore/query-data/listen#view_changes_between_snapshots)
381395

382396
const docChanges = snapshot.docChanges(snapshotListenOptions)
397+
const ignoreCached: boolean =
398+
options.ignoreCachedChanges && snapshot.metadata.fromCache
383399

384-
if (!isResolved && docChanges.length) {
400+
if (!isResolved && docChanges.length && !ignoreCached) {
385401
// isResolved is only meant to make sure we do the check only once
386402
isResolved = true
387403
let count = 0

0 commit comments

Comments
 (0)