Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.

Commit 63a00c1

Browse files
committed
[firestore] - Update document and query onSnapshot to accept onError callbacks
- Also fixes a crash which will occur if no callbacks were provided
1 parent 5c3a6a6 commit 63a00c1

File tree

3 files changed

+102
-46
lines changed

3 files changed

+102
-46
lines changed

src/firebase.android.ts

Lines changed: 50 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2275,7 +2275,7 @@ firebase.firestore.collection = (collectionPath: string): firestore.CollectionRe
22752275
where: (fieldPath: string, opStr: firestore.WhereFilterOp, value: any) => firebase.firestore.where(collectionPath, fieldPath, opStr, value),
22762276
orderBy: (fieldPath: string, directionStr: firestore.OrderByDirection): firestore.Query => firebase.firestore.orderBy(collectionPath, fieldPath, directionStr, collectionRef),
22772277
limit: (limit: number): firestore.Query => firebase.firestore.limit(collectionPath, limit, collectionRef),
2278-
onSnapshot: (optionsOrCallback: firestore.SnapshotListenOptions | ((snapshot: QuerySnapshot) => void), callback?: (snapshot: QuerySnapshot) => void) => firebase.firestore.onCollectionSnapshot(collectionRef, optionsOrCallback, callback),
2278+
onSnapshot: (optionsOrCallback: firestore.SnapshotListenOptions | ((snapshot: QuerySnapshot) => void), callbackOrOnError?: (snapshotOrError: QuerySnapshot | Error) => void, onError?: (error: Error) => void) => firebase.firestore.onCollectionSnapshot(collectionRef, optionsOrCallback, callbackOrOnError, onError),
22792279
startAfter: (snapshot: DocumentSnapshot): firestore.Query => firebase.firestore.startAfter(collectionPath, snapshot, collectionRef),
22802280
startAt: (snapshot: DocumentSnapshot): firestore.Query => firebase.firestore.startAt(collectionPath, snapshot, collectionRef),
22812281
endAt: (snapshot: DocumentSnapshot): firestore.Query => firebase.firestore.endAt(collectionPath, snapshot, collectionRef),
@@ -2288,46 +2288,66 @@ firebase.firestore.collection = (collectionPath: string): firestore.CollectionRe
22882288
}
22892289
};
22902290

2291-
firebase.firestore.onDocumentSnapshot = (docRef: com.google.firebase.firestore.DocumentReference, optionsOrCallback: firestore.SnapshotListenOptions | ((snapshot: DocumentSnapshot) => void), callback: (doc: DocumentSnapshot) => void): () => void => {
2291+
firebase.firestore.onDocumentSnapshot = (docRef: com.google.firebase.firestore.DocumentReference, optionsOrCallback: firestore.SnapshotListenOptions | ((snapshot: DocumentSnapshot) => void), callbackOrOnError: (docOrError: DocumentSnapshot | Error) => void, onError: (error: Error) => void): () => void => {
22922292
let options = com.google.firebase.firestore.MetadataChanges.EXCLUDE;
2293+
let onNextCallback: (snapshot: DocumentSnapshot) => void;
2294+
let onErrorCallback: (error: Error) => void;
2295+
22932296
if ((typeof optionsOrCallback) === "function") {
2294-
callback = <(snapshot: DocumentSnapshot) => void>optionsOrCallback;
2295-
} else if ((<firestore.SnapshotListenOptions>optionsOrCallback).includeMetadataChanges) {
2297+
onNextCallback = <(snapshot: DocumentSnapshot) => void>optionsOrCallback;
2298+
onErrorCallback = callbackOrOnError;
2299+
} else {
2300+
onNextCallback = callbackOrOnError;
2301+
onErrorCallback = onError;
2302+
}
2303+
if ((<firestore.SnapshotListenOptions>optionsOrCallback).includeMetadataChanges) {
22962304
options = com.google.firebase.firestore.MetadataChanges.INCLUDE;
22972305
}
22982306

22992307
const listener = docRef.addSnapshotListener(options, new com.google.firebase.firestore.EventListener({
2300-
onEvent: ((snapshot: com.google.firebase.firestore.DocumentSnapshot, exception: com.google.firebase.firestore.FirebaseFirestoreException) => {
2301-
if (exception === null) {
2302-
callback(new DocumentSnapshot(snapshot));
2303-
}
2304-
})
2305-
})
2308+
onEvent: ((snapshot: com.google.firebase.firestore.DocumentSnapshot, exception: com.google.firebase.firestore.FirebaseFirestoreException) => {
2309+
if (exception !== null) {
2310+
const error = "onDocumentSnapshot error code: " + exception.getCode();
2311+
onErrorCallback && onErrorCallback(new Error(error));
2312+
return;
2313+
}
2314+
onNextCallback && onNextCallback(new DocumentSnapshot(snapshot));
2315+
})
2316+
})
23062317
);
23072318

23082319
return () => listener.remove();
23092320
};
23102321

2311-
firebase.firestore.onCollectionSnapshot = (colRef: com.google.firebase.firestore.CollectionReference, optionsOrCallback: firestore.SnapshotListenOptions | ((snapshot: QuerySnapshot) => void), callback: (snapshot: QuerySnapshot) => void): () => void => {
2322+
firebase.firestore.onCollectionSnapshot = (colRef: com.google.firebase.firestore.CollectionReference, optionsOrCallback: firestore.SnapshotListenOptions | ((snapshot: QuerySnapshot) => void), callbackOrOnError: (snapshotOrError: QuerySnapshot | Error) => void, onError?: (error: Error) => void): () => void => {
23122323
let options = com.google.firebase.firestore.MetadataChanges.EXCLUDE;
2324+
let onNextCallback: (snapshot: QuerySnapshot) => void;
2325+
let onErrorCallback: (error: Error) => void;
2326+
2327+
// If we passed in an onNext for the first parameter, the next parameter would be onError if provided
2328+
// If options was the first parameter then the next parameter would be onNext if provided
23132329
if ((typeof optionsOrCallback) === "function") {
2314-
callback = <(snapshot: QuerySnapshot) => void>optionsOrCallback;
2315-
} else if ((<firestore.SnapshotListenOptions>optionsOrCallback).includeMetadataChanges) {
2330+
onNextCallback = <(snapshot: QuerySnapshot) => void>optionsOrCallback;
2331+
onErrorCallback = callbackOrOnError;
2332+
} else {
2333+
onNextCallback = callbackOrOnError; // Can be undefined if callback was not provided
2334+
onErrorCallback = onError; // Can be undefined if onError was not provided
2335+
}
2336+
if ((<firestore.SnapshotListenOptions>optionsOrCallback).includeMetadataChanges) {
23162337
options = com.google.firebase.firestore.MetadataChanges.INCLUDE;
23172338
}
23182339

23192340
const listener = colRef.addSnapshotListener(options, new com.google.firebase.firestore.EventListener({
2320-
onEvent: ((snapshot: com.google.firebase.firestore.QuerySnapshot, exception: com.google.firebase.firestore.FirebaseFirestoreException) => {
2321-
if (exception !== null) {
2322-
console.error('onCollectionSnapshot error code: ' + exception.getCode());
2323-
return;
2324-
}
2325-
2326-
callback(new QuerySnapshot(snapshot));
2327-
})
2328-
})
2341+
onEvent: ((snapshot: com.google.firebase.firestore.QuerySnapshot, exception: com.google.firebase.firestore.FirebaseFirestoreException) => {
2342+
if (exception !== null) {
2343+
const error = "onCollectionSnapshot error code: " + exception.getCode();
2344+
onErrorCallback && onErrorCallback(new Error(error));
2345+
return;
2346+
}
2347+
onNextCallback && onNextCallback(new QuerySnapshot(snapshot));
2348+
})
2349+
})
23292350
);
2330-
23312351
return () => listener.remove();
23322352
};
23332353

@@ -2341,7 +2361,8 @@ firebase.firestore._getDocumentReference = (javaObj: JDocumentReference, collect
23412361
get: () => firebase.firestore.getDocument(collectionPath, javaObj.getId()),
23422362
update: (data: any) => firebase.firestore.update(collectionPath, javaObj.getId(), data),
23432363
delete: () => firebase.firestore.delete(collectionPath, javaObj.getId()),
2344-
onSnapshot: (optionsOrCallback: firestore.SnapshotListenOptions | ((snapshot: DocumentSnapshot) => void), callback: (doc: DocumentSnapshot) => void) => firebase.firestore.onDocumentSnapshot(javaObj, optionsOrCallback, callback),
2364+
onSnapshot: (optionsOrCallback: firestore.SnapshotListenOptions | ((snapshot: DocumentSnapshot) => void), callbackOrOnError?: (docOrError: DocumentSnapshot | Error) => void,
2365+
onError?: (error: Error) => void) => firebase.firestore.onDocumentSnapshot(javaObj, optionsOrCallback, callbackOrOnError, onError),
23452366
android: javaObj
23462367
};
23472368
};
@@ -2402,7 +2423,8 @@ firebase.firestore.add = (collectionPath: string, document: any): Promise<firest
24022423
get: () => firebase.firestore.getDocument(collectionPath, docRef.getId()),
24032424
update: (data: any) => firebase.firestore.update(collectionPath, docRef.getId(), data),
24042425
delete: () => firebase.firestore.delete(collectionPath, docRef.getId()),
2405-
onSnapshot: (optionsOrCallback: firestore.SnapshotListenOptions | ((snapshot: DocumentSnapshot) => void), callback: (doc: DocumentSnapshot) => void) => firebase.firestore.onDocumentSnapshot(docRef, optionsOrCallback, callback)
2426+
onSnapshot: (optionsOrCallback: firestore.SnapshotListenOptions | ((snapshot: DocumentSnapshot) => void), callbackOrOnError?: (docOrError: DocumentSnapshot | Error) => void,
2427+
onError?: (error: Error) => void) => firebase.firestore.onDocumentSnapshot(docRef, optionsOrCallback, callbackOrOnError, onError)
24062428
});
24072429
}
24082430
});
@@ -2618,7 +2640,7 @@ firebase.firestore._getQuery = (collectionPath: string, query: com.google.fireba
26182640
where: (fp: string, os: firestore.WhereFilterOp, v: any): firestore.Query => firebase.firestore.where(collectionPath, fp, os, v, query),
26192641
orderBy: (fp: string, directionStr: firestore.OrderByDirection): firestore.Query => firebase.firestore.orderBy(collectionPath, fp, directionStr, query),
26202642
limit: (limit: number): firestore.Query => firebase.firestore.limit(collectionPath, limit, query),
2621-
onSnapshot: (optionsOrCallback: firestore.SnapshotListenOptions | ((snapshot: QuerySnapshot) => void), callback?: (snapshot: QuerySnapshot) => void) => firebase.firestore.onCollectionSnapshot(query, optionsOrCallback, callback),
2643+
onSnapshot: (optionsOrCallback: firestore.SnapshotListenOptions | ((snapshot: QuerySnapshot) => void), callbackOrOnError?: (snapshotOrError: QuerySnapshot | Error) => void, onError?: (error: Error) => void) => firebase.firestore.onCollectionSnapshot(query, optionsOrCallback, callbackOrOnError, onError),
26222644
startAfter: (snapshot: DocumentSnapshot) => firebase.firestore.startAfter(collectionPath, snapshot, query),
26232645
startAt: (snapshot: DocumentSnapshot) => firebase.firestore.startAt(collectionPath, snapshot, query),
26242646
endAt: (snapshot: DocumentSnapshot) => firebase.firestore.endAt(collectionPath, snapshot, query),
@@ -2702,7 +2724,8 @@ function convertDocRef(docRef: JDocumentReference): firestore.DocumentReference
27022724
get: () => firebase.firestore.getDocument(collectionPath, docRef.getId()),
27032725
update: (data: any) => firebase.firestore.update(collectionPath, docRef.getId(), data),
27042726
delete: () => firebase.firestore.delete(collectionPath, docRef.getId()),
2705-
onSnapshot: (optionsOrCallback: firestore.SnapshotListenOptions | ((doc: DocumentSnapshot) => void), callback: (doc: DocumentSnapshot) => void) => firebase.firestore.onDocumentSnapshot(docRef, optionsOrCallback, callback),
2727+
onSnapshot: (optionsOrCallback: firestore.SnapshotListenOptions | ((snapshot: DocumentSnapshot) => void), callbackOrOnError?: (docOrError: DocumentSnapshot | Error) => void,
2728+
onError?: (error: Error) => void) => firebase.firestore.onDocumentSnapshot(docRef, optionsOrCallback, callbackOrOnError, onError),
27062729
android: docRef
27072730
};
27082731
}

src/firebase.d.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -789,17 +789,25 @@ export namespace firestore {
789789

790790
export interface DocumentReference {
791791
discriminator: "docRef";
792+
792793
id: string;
794+
793795
path: string;
796+
794797
collection: (collectionPath: string) => CollectionReference;
798+
795799
set: (document: any, options?: SetOptions) => Promise<void>;
800+
796801
get: () => Promise<DocumentSnapshot>;
802+
797803
update: (document: any) => Promise<void>;
804+
798805
delete: () => Promise<void>;
799806

800-
onSnapshot(optionsOrCallback: SnapshotListenOptions | ((snapshot: DocumentSnapshot) => void), callback?: (snapshot: DocumentSnapshot) => void): () => void;
807+
onSnapshot(optionsOrCallback: SnapshotListenOptions | ((snapshot: DocumentSnapshot) => void), callbackOrOnError?: (snapshot: DocumentSnapshot | Error) => void, onError?: (error: Error) => void): () => void;
801808

802809
android?: any;
810+
803811
ios?: any;
804812
}
805813

@@ -812,7 +820,7 @@ export namespace firestore {
812820

813821
limit(limit: number): Query;
814822

815-
onSnapshot(optionsOrCallback: SnapshotListenOptions | ((snapshot: QuerySnapshot) => void), callback?: (snapshot: QuerySnapshot) => void): () => void;
823+
onSnapshot(optionsOrCallback: SnapshotListenOptions | ((snapshot: QuerySnapshot) => void), callbackOrOnError?: (snapshotOrError: QuerySnapshot | Error) => void, onError?: (error: Error) => void): () => void;
816824

817825
startAt(snapshot: DocumentSnapshot): Query;
818826

0 commit comments

Comments
 (0)