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

Commit 95a9a87

Browse files
Firestore anytime soon? #507 (onSnapshot)
1 parent c817dc6 commit 95a9a87

File tree

6 files changed

+63
-19
lines changed

6 files changed

+63
-19
lines changed

demo-ng/app/item/items.component.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<Button text="Set (auto id)" (tap)="firestoreSetByAutoID()" class="button"></Button>
1313
<Button text="Update" (tap)="firestoreUpdate()" class="button"></Button>
1414
<Button text="Get" (tap)="firestoreGet()" class="button"></Button>
15+
<Button text="Listen to SF" (tap)="firestoreListen()" class="button"></Button>
1516
<Button text="Where" (tap)="firestoreWhere()" class="button"></Button>
1617
<Button text="Where, Order, Limit" (tap)="firestoreWhereOrderLimit()" class="button"></Button>
1718
<Button text="Delete" (tap)="firestoreDelete()" class="button"></Button>

demo-ng/app/item/items.component.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,18 @@ export class ItemsComponent implements OnInit {
135135
});
136136
}
137137

138+
public firestoreListen(): void {
139+
const docRef: firestore.DocumentReference = firebase.firestore().collection("cities").doc("SF");
140+
141+
docRef.onSnapshot((doc: firestore.DocumentSnapshot) => {
142+
if (doc.exists) {
143+
console.log("Document data:", JSON.stringify(doc.data()));
144+
} else {
145+
console.log("No such document!");
146+
}
147+
});
148+
}
149+
138150
public firestoreWhere(): void {
139151
const query: firestore.Query = firebase.firestore().collection("cities")
140152
.where("state", "==", "CA")

docs/FIRESTORE.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,21 @@ sanFranciscoDocument.get().then(doc => {
6767
});
6868
```
6969

70+
### `collection.doc().onSnapshot()`
71+
To listen to changes in a certain document, you can register a callback function that gets invoked every time data is changed:
72+
73+
```typescript
74+
const sanFranciscoDocument = firebase.firestore().collection("cities").doc("SF");
75+
76+
sanFranciscoDocument.onSnapshot(doc => {
77+
if (doc.exists) {
78+
console.log("Document data:", JSON.stringify(doc.data()));
79+
} else {
80+
console.log("No such document!");
81+
}
82+
});
83+
```
84+
7085
### `collection.add()`
7186
If you want to add a document with an auto-generated ID, use `add` on a *collection*:
7287

@@ -173,6 +188,4 @@ query
173188
```
174189

175190
## Future work
176-
I'll soon add [`onSnapshot`](https://firebase.google.com/docs/firestore/query-data/listen) to support realtime updates.
177-
178191
Need something else that's not supported yet? Please open an Issue or PR 😚

src/firebase.android.ts

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2216,6 +2216,21 @@ firebase.firestore.collection = (collectionPath: string): firestore.CollectionRe
22162216
}
22172217
};
22182218

2219+
firebase.firestore.onSnapshot = (docRef: com.google.firebase.firestore.DocumentReference, callback: (doc: DocumentSnapshot) => void): void => {
2220+
docRef.addSnapshotListener(new com.google.firebase.firestore.EventListener({
2221+
onEvent: ((snapshot: com.google.firebase.firestore.DocumentSnapshot, exception) => {
2222+
if (exception !== null) {
2223+
return;
2224+
}
2225+
callback(new DocumentSnapshot(
2226+
snapshot ? snapshot.getId() : null,
2227+
snapshot.exists(),
2228+
snapshot ? () => firebase.toJsObject(snapshot.getData()) : null));
2229+
})
2230+
})
2231+
);
2232+
};
2233+
22192234
firebase.firestore.doc = (collectionPath: string, documentPath?: string): firestore.DocumentReference => {
22202235
try {
22212236

@@ -2234,7 +2249,8 @@ firebase.firestore.doc = (collectionPath: string, documentPath?: string): firest
22342249
set: (data: any, options?: firestore.SetOptions) => firebase.firestore.set(collectionPath, docRef.getId(), data, options),
22352250
get: () => firebase.firestore.getDocument(collectionPath, docRef.getId()),
22362251
update: (data: any) => firebase.firestore.update(collectionPath, docRef.getId(), data),
2237-
delete: () => firebase.firestore.delete(collectionPath, docRef.getId())
2252+
delete: () => firebase.firestore.delete(collectionPath, docRef.getId()),
2253+
onSnapshot: (callback: (doc: DocumentSnapshot) => void) => firebase.firestore.onSnapshot(docRef, callback)
22382254
};
22392255

22402256
} catch (ex) {
@@ -2255,14 +2271,15 @@ firebase.firestore.add = (collectionPath: string, document: any): Promise<firest
22552271
const db = com.google.firebase.firestore.FirebaseFirestore.getInstance();
22562272

22572273
const onSuccessListener = new com.google.android.gms.tasks.OnSuccessListener({
2258-
onSuccess: (documentReference: com.google.firebase.firestore.DocumentReference) => {
2274+
onSuccess: (docRef: com.google.firebase.firestore.DocumentReference) => {
22592275
resolve({
2260-
id: documentReference.getId(),
2276+
id: docRef.getId(),
22612277
collection: cp => firebase.firestore.collection(cp),
2262-
set: (data: any, options?: firestore.SetOptions) => firebase.firestore.set(collectionPath, documentReference.getId(), data, options),
2263-
get: () => firebase.firestore.getDocument(collectionPath, documentReference.getId()),
2264-
update: (data: any) => firebase.firestore.update(collectionPath, documentReference.getId(), data),
2265-
delete: () => firebase.firestore.delete(collectionPath, documentReference.getId())
2278+
set: (data: any, options?: firestore.SetOptions) => firebase.firestore.set(collectionPath, docRef.getId(), data, options),
2279+
get: () => firebase.firestore.getDocument(collectionPath, docRef.getId()),
2280+
update: (data: any) => firebase.firestore.update(collectionPath, docRef.getId(), data),
2281+
delete: () => firebase.firestore.delete(collectionPath, docRef.getId()),
2282+
onSnapshot: (callback: (doc: DocumentSnapshot) => void) => firebase.firestore.onSnapshot(docRef, callback)
22662283
});
22672284
}
22682285
});

src/firebase.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -807,7 +807,7 @@ export namespace firestore {
807807
get: () => Promise<DocumentSnapshot>;
808808
update: (document: any) => Promise<void>;
809809
delete: () => Promise<void>;
810-
// onSnapshot(callback: (doc: DocumentSnapshot) => void): void;
810+
onSnapshot(callback: (doc: DocumentSnapshot) => void): void;
811811
}
812812

813813
export interface Query {

src/firebase.ios.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2243,6 +2243,12 @@ firebase.firestore.collection = (collectionPath: string): firestore.CollectionRe
22432243
}
22442244
};
22452245

2246+
firebase.firestore.onSnapshot = (docRef: FIRDocumentReference, callback: (doc: DocumentSnapshot) => void): void => {
2247+
docRef.addSnapshotListener((snapshot: FIRDocumentSnapshot, error: NSError) => {
2248+
callback(new DocumentSnapshot(snapshot ? snapshot.documentID : null, !!snapshot, snapshot ? () => firebase.toJsObject(snapshot.data()) : null));
2249+
})
2250+
};
2251+
22462252
firebase.firestore.doc = (collectionPath: string, documentPath?: string): firestore.DocumentReference => {
22472253
try {
22482254
if (typeof(FIRFirestore) === "undefined") {
@@ -2259,8 +2265,8 @@ firebase.firestore.doc = (collectionPath: string, documentPath?: string): firest
22592265
set: (data: any, options?: firestore.SetOptions) => firebase.firestore.set(collectionPath, fIRDocumentReference.documentID, data, options),
22602266
get: () => firebase.firestore.getDocument(collectionPath, fIRDocumentReference.documentID),
22612267
update: (data: any) => firebase.firestore.update(collectionPath, fIRDocumentReference.documentID, data),
2262-
delete: () => firebase.firestore.delete(collectionPath, fIRDocumentReference.documentID)
2263-
// onSnapshot: (callback: (doc: DocumentSnapshot) => void) => firebase.firestore.onSnapshot(fIRDocumentReference, callback)
2268+
delete: () => firebase.firestore.delete(collectionPath, fIRDocumentReference.documentID),
2269+
onSnapshot: (callback: (doc: DocumentSnapshot) => void) => firebase.firestore.onSnapshot(fIRDocumentReference, callback)
22642270
};
22652271

22662272
} catch (ex) {
@@ -2269,12 +2275,6 @@ firebase.firestore.doc = (collectionPath: string, documentPath?: string): firest
22692275
}
22702276
};
22712277

2272-
// firebase.firestore.onSnapshot = (docRef: FIRDocumentReference, callback: (doc: DocumentSnapshot) => void): void => {
2273-
// docRef.addSnapshotListener((snapshot: FIRDocumentSnapshot, error: NSError) => {
2274-
// callback(new DocumentSnapshot(snapshot ? snapshot.documentID : null, !!snapshot, snapshot ? firebase.toJsObject(snapshot.data()) : null));
2275-
// })
2276-
// };
2277-
22782278
firebase.firestore.add = (collectionPath: string, document: any): Promise<firestore.DocumentReference> => {
22792279
return new Promise((resolve, reject) => {
22802280
try {
@@ -2296,7 +2296,8 @@ firebase.firestore.add = (collectionPath: string, document: any): Promise<firest
22962296
set: (data: any, options?: firestore.SetOptions) => firebase.firestore.set(collectionPath, fIRDocumentReference.documentID, data, options),
22972297
get: () => firebase.firestore.getDocument(collectionPath, fIRDocumentReference.documentID),
22982298
update: (data: any) => firebase.firestore.update(collectionPath, fIRDocumentReference.documentID, data),
2299-
delete: () => firebase.firestore.delete(collectionPath, fIRDocumentReference.documentID)
2299+
delete: () => firebase.firestore.delete(collectionPath, fIRDocumentReference.documentID),
2300+
onSnapshot: (callback: (doc: DocumentSnapshot) => void) => firebase.firestore.onSnapshot(fIRDocumentReference, callback)
23002301
});
23012302
}
23022303
});

0 commit comments

Comments
 (0)