-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,7 @@ | ||
import { Observable, Subscriber } from 'rxjs'; | ||
import { fromCollectionRef } from '../observable/fromRef'; | ||
import { map, filter, scan } from 'rxjs/operators'; | ||
|
||
import { Injectable } from '@angular/core'; | ||
import { firestore } from 'firebase'; | ||
|
||
import { DocumentChangeType, CollectionReference, Query, DocumentReference, DocumentData, QueryFn, AssociatedReference, DocumentChangeAction, DocumentChange } from '../interfaces'; | ||
import { docChanges, sortedChanges } from './changes'; | ||
|
@@ -16,6 +15,17 @@ export function validateEventsArray(events?: DocumentChangeType[]) { | |
return events; | ||
} | ||
|
||
function validateEventsOrOptions(eventsOrOptions, options) { | ||
let events: DocumentChangeType[] = []; | ||
let listenerOptions: firestore.SnapshotListenOptions | undefined = options; | ||
if(Array.isArray(eventsOrOptions)) { | ||
events = eventsOrOptions; | ||
} else { | ||
listenerOptions = eventsOrOptions; | ||
} | ||
return { events, listenerOptions }; | ||
} | ||
|
||
/** | ||
* AngularFirestoreCollection service | ||
* | ||
|
@@ -61,17 +71,18 @@ export class AngularFirestoreCollection<T=DocumentData> { | |
* your own data structure. | ||
* @param events | ||
*/ | ||
stateChanges(events?: DocumentChangeType[]): Observable<DocumentChangeAction<T>[]> { | ||
stateChanges(eventsOrOptions?: DocumentChangeType[] | firestore.SnapshotListenOptions, options?: firestore.SnapshotListenOptions): Observable<DocumentChangeAction<T>[]> { | ||
const { events, listenerOptions } = validateEventsOrOptions(eventsOrOptions, options); | ||
if(!events || events.length === 0) { | ||
return this.afs.scheduler.keepUnstableUntilFirst( | ||
this.afs.scheduler.runOutsideAngular( | ||
docChanges<T>(this.query) | ||
docChanges<T>(this.query, listenerOptions) | ||
) | ||
); | ||
} | ||
return this.afs.scheduler.keepUnstableUntilFirst( | ||
this.afs.scheduler.runOutsideAngular( | ||
docChanges<T>(this.query) | ||
docChanges<T>(this.query, listenerOptions) | ||
) | ||
) | ||
.pipe( | ||
|
@@ -85,27 +96,36 @@ export class AngularFirestoreCollection<T=DocumentData> { | |
* but it collects each event in an array over time. | ||
* @param events | ||
*/ | ||
auditTrail(events?: DocumentChangeType[]): Observable<DocumentChangeAction<T>[]> { | ||
return this.stateChanges(events).pipe(scan((current, action) => [...current, ...action], [])); | ||
auditTrail(eventsOrOptions?: DocumentChangeType[] | firestore.SnapshotListenOptions, options?: firestore.SnapshotListenOptions): Observable<DocumentChangeAction<T>[]> { | ||
const { events, listenerOptions } = validateEventsOrOptions(eventsOrOptions, options); | ||
return this.stateChanges(events, listenerOptions).pipe(scan((current, action) => [...current, ...action], [])); | ||
} | ||
|
||
/** | ||
* 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[] = []; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
let listenerOptions: firestore.SnapshotListenOptions | undefined = options; | ||
if(Array.isArray(eventsOrOptions)) { | ||
events = eventsOrOptions; | ||
} else { | ||
listenerOptions = eventsOrOptions; | ||
} | ||
|
||
const validatedEvents = validateEventsArray(events); | ||
const sortedChanges$ = sortedChanges<T>(this.query, validatedEvents); | ||
const sortedChanges$ = sortedChanges<T>(this.query, validatedEvents, listenerOptions); | ||
const scheduledSortedChanges$ = this.afs.scheduler.runOutsideAngular(sortedChanges$); | ||
return this.afs.scheduler.keepUnstableUntilFirst(scheduledSortedChanges$); | ||
} | ||
|
||
/** | ||
* Listen to all documents in the collection and its possible query as an Observable. | ||
*/ | ||
valueChanges(): Observable<T[]> { | ||
const fromCollectionRef$ = fromCollectionRef<T>(this.query); | ||
valueChanges(options?: firestore.SnapshotListenOptions): Observable<T[]> { | ||
const fromCollectionRef$ = fromCollectionRef<T>(this.query, options); | ||
const scheduled$ = this.afs.scheduler.runOutsideAngular(fromCollectionRef$); | ||
return this.afs.scheduler.keepUnstableUntilFirst(scheduled$) | ||
.pipe( | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,26 @@ | ||
import { Observable, Subscriber } from 'rxjs'; | ||
import { DocumentReference, Query, Action, Reference, DocumentSnapshot, QuerySnapshot } from '../interfaces'; | ||
import { map, share } from 'rxjs/operators'; | ||
import { firestore } from 'firebase'; | ||
|
||
function _fromRef<T, R>(ref: Reference<T>): Observable<R> { | ||
function _fromRef<T, R>(ref: Query, options?: firestore.SnapshotListenOptions): Observable<R> { | ||
return new Observable(subscriber => { | ||
const unsubscribe = ref.onSnapshot(subscriber); | ||
const unsubscribe = ref.onSnapshot(options || {}, subscriber as any) | ||
return { unsubscribe }; | ||
}); | ||
} | ||
|
||
export function fromRef<R>(ref: DocumentReference | Query) { | ||
return _fromRef<typeof ref, R>(ref).pipe(share()); | ||
export function fromRef<R>(ref: any, options?: firestore.SnapshotListenOptions) { | ||
return _fromRef<typeof ref, R>(ref, options).pipe(share()); | ||
} | ||
|
||
export function fromDocRef<T>(ref: DocumentReference): Observable<Action<DocumentSnapshot<T>>>{ | ||
return fromRef<DocumentSnapshot<T>>(ref) | ||
.pipe( | ||
map(payload => ({ payload, type: 'value' })) | ||
); | ||
export function fromDocRef<T>(ref: DocumentReference): Observable<DocumentSnapshot<T>>{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we want to drop the |
||
return new Observable(subscriber => { | ||
const unsubscribe = ref.onSnapshot(subscriber as any) | ||
return { unsubscribe }; | ||
}); | ||
} | ||
|
||
export function fromCollectionRef<T>(ref: Query): Observable<Action<QuerySnapshot<T>>> { | ||
return fromRef<QuerySnapshot<T>>(ref).pipe(map(payload => ({ payload, type: 'query' }))); | ||
export function fromCollectionRef<T>(ref: Query, options?: firestore.SnapshotListenOptions,): Observable<Action<QuerySnapshot<T>>> { | ||
return fromRef<QuerySnapshot<T>>(ref, options).pipe(map(payload => ({ payload, type: 'query' }))); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?