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

Commit c83f2aa

Browse files
fix snapshot creation logic and data callback return type #662
1 parent 5685b63 commit c83f2aa

File tree

3 files changed

+14
-19
lines changed

3 files changed

+14
-19
lines changed

src/firebase-common.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,9 @@ export const firebase: any = {
189189
};
190190

191191
export class DocumentSnapshot implements firestore.DocumentSnapshot {
192-
constructor(public id: string, public exists: boolean, public data: () => firestore.DocumentData) {
192+
public data: () => firestore.DocumentData;
193+
constructor(public id: string, public exists: boolean, documentData: firestore.DocumentData) {
194+
this.data = () => exists ? documentData : undefined;
193195
}
194196
}
195197

src/firebase.android.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2319,13 +2319,9 @@ firebase.firestore.collection = (collectionPath: string): firestore.CollectionRe
23192319
firebase.firestore.onDocumentSnapshot = (docRef: com.google.firebase.firestore.DocumentReference, callback: (doc: DocumentSnapshot) => void): () => void => {
23202320
const listener = docRef.addSnapshotListener(new com.google.firebase.firestore.EventListener({
23212321
onEvent: ((snapshot: com.google.firebase.firestore.DocumentSnapshot, exception) => {
2322-
if (exception !== null) {
2323-
return;
2322+
if (exception === null) {
2323+
callback(new DocumentSnapshot(snapshot ? snapshot.getId() : null, snapshot.exists(), firebase.toJsObject(snapshot.getData())));
23242324
}
2325-
callback(new DocumentSnapshot(
2326-
snapshot ? snapshot.getId() : null,
2327-
snapshot.exists(),
2328-
() => snapshot.exists() ? firebase.toJsObject(snapshot.getData()) : undefined));
23292325
})
23302326
})
23312327
);
@@ -2343,7 +2339,7 @@ firebase.firestore.onCollectionSnapshot = (colRef: com.google.firebase.firestore
23432339
const docSnapshots: Array<firestore.DocumentSnapshot> = [];
23442340
for (let i = 0; i < snapshot.size(); i++) {
23452341
const documentSnapshot: com.google.firebase.firestore.DocumentSnapshot = snapshot.getDocuments().get(i);
2346-
docSnapshots.push(new DocumentSnapshot(documentSnapshot.getId(), true, () => firebase.toJsObject(documentSnapshot.getData())));
2342+
docSnapshots.push(new DocumentSnapshot(documentSnapshot.getId(), true, firebase.toJsObject(documentSnapshot.getData())));
23472343
}
23482344
const snap = new QuerySnapshot();
23492345
snap.docSnapshots = docSnapshots;
@@ -2550,7 +2546,7 @@ firebase.firestore.getCollection = (collectionPath: string): Promise<firestore.Q
25502546
const docSnapshots: Array<firestore.DocumentSnapshot> = [];
25512547
for (let i = 0; i < result.size(); i++) {
25522548
const documentSnapshot: com.google.firebase.firestore.DocumentSnapshot = result.getDocuments().get(i);
2553-
docSnapshots.push(new DocumentSnapshot(documentSnapshot.getId(), true, () => firebase.toJsObject(documentSnapshot.getData())));
2549+
docSnapshots.push(new DocumentSnapshot(documentSnapshot.getId(), true, firebase.toJsObject(documentSnapshot.getData())));
25542550
}
25552551
const snap = new QuerySnapshot();
25562552
snap.docSnapshots = docSnapshots;
@@ -2600,7 +2596,7 @@ firebase.firestore.getDocument = (collectionPath: string, documentPath: string):
26002596
} else {
26012597
const result: com.google.firebase.firestore.DocumentSnapshot = task.getResult();
26022598
const exists = result.exists();
2603-
resolve(new DocumentSnapshot(exists ? result.getId() : null, exists, () => exists ? firebase.toJsObject(result.getData()) : null));
2599+
resolve(new DocumentSnapshot(exists ? result.getId() : null, exists, firebase.toJsObject(result.getData())));
26042600
}
26052601
}
26062602
});
@@ -2637,7 +2633,7 @@ firebase.firestore._getQuery = (collectionPath: string, query: com.google.fireba
26372633
const docSnapshots: Array<firestore.DocumentSnapshot> = [];
26382634
for (let i = 0; i < result.size(); i++) {
26392635
const documentSnapshot: com.google.firebase.firestore.DocumentSnapshot = result.getDocuments().get(i);
2640-
docSnapshots.push(new DocumentSnapshot(documentSnapshot.getId(), true, () => firebase.toJsObject(documentSnapshot.getData())));
2636+
docSnapshots.push(new DocumentSnapshot(documentSnapshot.getId(), true, firebase.toJsObject(documentSnapshot.getData())));
26412637
}
26422638
const snap = new QuerySnapshot();
26432639
snap.docSnapshots = docSnapshots;

src/firebase.ios.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2268,10 +2268,7 @@ firebase.firestore.collection = (collectionPath: string): firestore.CollectionRe
22682268

22692269
firebase.firestore.onDocumentSnapshot = (docRef: FIRDocumentReference, callback: (doc: DocumentSnapshot) => void): () => void => {
22702270
const listener = docRef.addSnapshotListener((snapshot: FIRDocumentSnapshot, error: NSError) => {
2271-
callback(new DocumentSnapshot(
2272-
snapshot ? snapshot.documentID : null,
2273-
snapshot.exists,
2274-
() => snapshot.exists ? firebase.toJsObject(snapshot.data()) : undefined));
2271+
callback(new DocumentSnapshot(snapshot.documentID, snapshot.exists, firebase.toJsObject(snapshot.data())));
22752272
});
22762273

22772274
// There's a bug resulting this function to be undefined..
@@ -2291,7 +2288,7 @@ firebase.firestore.onCollectionSnapshot = (colRef: FIRCollectionReference, callb
22912288
const docSnapshots: Array<firestore.DocumentSnapshot> = [];
22922289
for (let i = 0, l = snapshot.documents.count; i < l; i++) {
22932290
const document: FIRDocumentSnapshot = snapshot.documents.objectAtIndex(i);
2294-
docSnapshots.push(new DocumentSnapshot(document.documentID, true, () => firebase.toJsObject(document.data())));
2291+
docSnapshots.push(new DocumentSnapshot(document.documentID, true, firebase.toJsObject(document.data())));
22952292
}
22962293

22972294
const snap = new QuerySnapshot();
@@ -2484,7 +2481,7 @@ firebase.firestore.getCollection = (collectionPath: string): Promise<firestore.Q
24842481
const docSnapshots: Array<firestore.DocumentSnapshot> = [];
24852482
for (let i = 0, l = snapshot.documents.count; i < l; i++) {
24862483
const document: FIRDocumentSnapshot = snapshot.documents.objectAtIndex(i);
2487-
docSnapshots.push(new DocumentSnapshot(document.documentID, true, () => firebase.toJsObject(document.data())));
2484+
docSnapshots.push(new DocumentSnapshot(document.documentID, true, firebase.toJsObject(document.data())));
24882485
}
24892486
const snap = new QuerySnapshot();
24902487
snap.docSnapshots = docSnapshots;
@@ -2519,7 +2516,7 @@ firebase.firestore.getDocument = (collectionPath: string, documentPath: string):
25192516
reject(error.localizedDescription);
25202517
} else {
25212518
const exists = snapshot.exists;
2522-
resolve(new DocumentSnapshot(exists ? snapshot.documentID : null, exists, () => exists ? firebase.toJsObject(snapshot.data()) : null));
2519+
resolve(new DocumentSnapshot(exists ? snapshot.documentID : null, exists, firebase.toJsObject(snapshot.data())));
25232520
}
25242521
});
25252522

@@ -2541,7 +2538,7 @@ firebase.firestore._getQuery = (collectionPath: string, query: FIRQuery): firest
25412538
const docSnapshots: Array<firestore.DocumentSnapshot> = [];
25422539
for (let i = 0, l = snapshot.documents.count; i < l; i++) {
25432540
const document: FIRDocumentSnapshot = snapshot.documents.objectAtIndex(i);
2544-
docSnapshots.push(new DocumentSnapshot(document.documentID, true, () => firebase.toJsObject(document.data())));
2541+
docSnapshots.push(new DocumentSnapshot(document.documentID, true, firebase.toJsObject(document.data())));
25452542
}
25462543
const snap = new QuerySnapshot();
25472544
snap.docSnapshots = docSnapshots;

0 commit comments

Comments
 (0)