Skip to content

feat(firestore): Add support for SnapshotListenOptions #1813

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

Closed
wants to merge 3 commits into from
Closed

feat(firestore): Add support for SnapshotListenOptions #1813

wants to merge 3 commits into from

Conversation

davideast
Copy link
Collaborator

Addressing #1747.

Add support for metadata changes via the SnapshotListenOptions

In some cases you need to listen to metadata updates to know if you want to surface an event. If you only want to display server data or know when something offline has been modified.

constructor(afs: AngularFirestore) {
  const items$ = afs.collection('items').snapshotChanges({ includeMetadataChanges: true });
  // or you can specify events
  const addedItems$ = afs.collection('items').snapshotChanges(['added'], { includeMetadataChanges: true });
}

@TyagiAnkit40
Copy link

TyagiAnkit40 commented Aug 13, 2018

Will it be better if we can directly specify {source : 'server'} instead of reading metadata? That way, the subscribe to valuechanges / snapshot will only fire once. Today, subscribe fires two times, one for cached data and another for server data. Even today, I can look at 'fromCache' property and identify whether it's cached data or loaded from server. Not sure how including metadata will be different.

@davideast
Copy link
Collaborator Author

@TyagiAnkit40 You can do something like { source: 'server' } by filtering the changes. The problem with { source : 'server' } is that it's abstracts too much over the Firestore SDK. If something changes upstream we'll likely have to break the API.

Try something like this:

afs.collection('items').snapshotChanges({ includeMetadataChanges: true }).pipe(
  filter(actions => actions.filter(a => a.payload.meta.fromCache === false)),
  map(actions => actions.map(a => a.payload.data())
).subscribe(console.log));

}

/**
* Create a stream of synchronized changes. This method keeps the local array in sorted
* query order.
* @param events
*/
snapshotChanges(events?: DocumentChangeType[]): Observable<DocumentChangeAction<T>[]> {
snapshotChanges(eventsOrOptions?: DocumentChangeType[] | firestore.SnapshotListenOptions, options?: firestore.SnapshotListenOptions): Observable<DocumentChangeAction<T>[]> {
let events: DocumentChangeType[] = [];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use validateEventsOrOptions here?

let events: DocumentChangeType[] = [];
let listenerOptions: firestore.SnapshotListenOptions | undefined = options;
if(Array.isArray(eventsOrOptions)) {
events = eventsOrOptions;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it make sense to pass into validateEventsArray here?

.pipe(
map(payload => ({ payload, type: 'value' }))
);
export function fromDocRef<T>(ref: DocumentReference): Observable<DocumentSnapshot<T>>{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to drop the payload/type in this commit? If so let's make sure to address the docs.

@davideast
Copy link
Collaborator Author

After a lengthy discussion with @jamesdaniels we decided it was best to include these changes into the new #1819 proposal and keep these methods stable. We don't provide access to the QuerySnapshot.metadata object and we'd have to provide another abstraction level. Instead we will allow for this specific functionality with #1819.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants