Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(firestore): add ignoreCachedChanges ref option (close #1324) #1325

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/firestore/bind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ import { onSnapshot } from 'firebase/firestore'
* Options when binding a Firestore document or collection.
*/
export interface FirestoreRefOptions extends _DataSourceOptions {
/**
* Ignores snapshot changes that are coming from the Firestore client cache, defering ref updates until fresh data
* is pulled from the server. Useful for pagination scenarios, when using multiple listeners on the same collection
* with distinct queries and updating query refs.
*/
ignoreCachedChanges?: boolean

/**
* The maximum depth to bind nested refs. A nested ref that isn't bound will stay as the ref path while a bound ref
* will contain the same data as if the ref was bound directly.
Expand Down Expand Up @@ -60,11 +67,17 @@ export interface _FirestoreRefOptionsWithDefaults extends FirestoreRefOptions {
* @defaultValue `false`
*/
reset: ResetOption

/**
* @defaultValue `true`
*/
wait: boolean

/**
* @defaultValue `false`
*/
ignoreCachedChanges: boolean

/**
* @defaultValue `2`
*/
Expand All @@ -88,6 +101,7 @@ export interface _FirestoreRefOptionsWithDefaults extends FirestoreRefOptions {
const DEFAULT_OPTIONS: _FirestoreRefOptionsWithDefaults = {
reset: false,
wait: true,
ignoreCachedChanges: false,
maxRefDepth: 2,
converter: firestoreDefaultConverter,
snapshotOptions: { serverTimestamps: 'estimate' },
Expand Down Expand Up @@ -380,8 +394,10 @@ export function bindCollection<T = unknown>(
// (https://firebase.google.com/docs/firestore/query-data/listen#view_changes_between_snapshots)

const docChanges = snapshot.docChanges(snapshotListenOptions)
const ignoreCached: boolean =
options.ignoreCachedChanges && snapshot.metadata.fromCache

if (!isResolved && docChanges.length) {
if (!isResolved && docChanges.length && !ignoreCached) {
// isResolved is only meant to make sure we do the check only once
isResolved = true
let count = 0
Expand Down