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

Commit a82d088

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

File tree

6 files changed

+47
-10
lines changed

6 files changed

+47
-10
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
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>
15+
<Button text="Listen to changes in SF" (tap)="firestoreListen()" class="button"></Button>
16+
<Button text="Stop listening" (tap)="firestoreStopListening()" class="button"></Button>
1617
<Button text="Where" (tap)="firestoreWhere()" class="button"></Button>
1718
<Button text="Where, Order, Limit" (tap)="firestoreWhereOrderLimit()" class="button"></Button>
1819
<Button text="Delete" (tap)="firestoreDelete()" class="button"></Button>

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ const firebase = require("nativescript-plugin-firebase/app");
99
})
1010
export class ItemsComponent implements OnInit {
1111

12+
private listenerUnsubscribe: () => void;
13+
1214
constructor() {
1315
}
1416

@@ -136,9 +138,14 @@ export class ItemsComponent implements OnInit {
136138
}
137139

138140
public firestoreListen(): void {
141+
if (this.listenerUnsubscribe !== undefined) {
142+
console.log("Already listening ;)");
143+
return;
144+
}
145+
139146
const docRef: firestore.DocumentReference = firebase.firestore().collection("cities").doc("SF");
140147

141-
docRef.onSnapshot((doc: firestore.DocumentSnapshot) => {
148+
this.listenerUnsubscribe = docRef.onSnapshot((doc: firestore.DocumentSnapshot) => {
142149
if (doc.exists) {
143150
console.log("Document data:", JSON.stringify(doc.data()));
144151
} else {
@@ -147,6 +154,16 @@ export class ItemsComponent implements OnInit {
147154
});
148155
}
149156

157+
public firestoreStopListening(): void {
158+
if (this.listenerUnsubscribe === undefined) {
159+
console.log("Please start listening first ;)");
160+
return;
161+
}
162+
163+
this.listenerUnsubscribe();
164+
this.listenerUnsubscribe = undefined;
165+
}
166+
150167
public firestoreWhere(): void {
151168
const query: firestore.Query = firebase.firestore().collection("cities")
152169
.where("state", "==", "CA")

docs/FIRESTORE.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,25 @@ sanFranciscoDocument.get().then(doc => {
6868
```
6969

7070
### `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:
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+
To unsubscribe the listener, just invoke the function that was returned when attaching the listener.
74+
75+
> NOTE: Due to a bug in the iOS Firestore SDK 'unsubscribe' does not currently work (plugin version 5.0.0), so the plugin is silently ignoring further updates after unsubscribing. This should not hurt your usage in any way though.
7276
7377
```typescript
7478
const sanFranciscoDocument = firebase.firestore().collection("cities").doc("SF");
7579

76-
sanFranciscoDocument.onSnapshot(doc => {
80+
const unsubscribe = sanFranciscoDocument.onSnapshot(doc => {
7781
if (doc.exists) {
7882
console.log("Document data:", JSON.stringify(doc.data()));
7983
} else {
8084
console.log("No such document!");
8185
}
8286
});
87+
88+
// then after a while, to detach the listener:
89+
unsubscribe();
8390
```
8491

8592
### `collection.add()`

src/firebase.android.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2216,8 +2216,8 @@ 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({
2219+
firebase.firestore.onSnapshot = (docRef: com.google.firebase.firestore.DocumentReference, callback: (doc: DocumentSnapshot) => void): ()=> void => {
2220+
const listener = docRef.addSnapshotListener(new com.google.firebase.firestore.EventListener({
22212221
onEvent: ((snapshot: com.google.firebase.firestore.DocumentSnapshot, exception) => {
22222222
if (exception !== null) {
22232223
return;
@@ -2229,6 +2229,8 @@ firebase.firestore.onSnapshot = (docRef: com.google.firebase.firestore.DocumentR
22292229
})
22302230
})
22312231
);
2232+
2233+
return () => listener.remove();
22322234
};
22332235

22342236
firebase.firestore.doc = (collectionPath: string, documentPath?: string): firestore.DocumentReference => {

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: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2243,10 +2243,20 @@ 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) => {
2246+
firebase.firestore.onSnapshot = (docRef: FIRDocumentReference, callback: (doc: DocumentSnapshot) => void): () => void => {
2247+
const listener = docRef.addSnapshotListener((snapshot: FIRDocumentSnapshot, error: NSError) => {
22482248
callback(new DocumentSnapshot(snapshot ? snapshot.documentID : null, !!snapshot, snapshot ? () => firebase.toJsObject(snapshot.data()) : null));
2249-
})
2249+
});
2250+
2251+
// There's a bug resulting this function to be undefined..
2252+
if (listener.remove === undefined) {
2253+
return () => {
2254+
// .. so we're just ignoring anything received from the server (until the callback is set again when 'onSnapshot' is invoked).
2255+
callback = () => {};
2256+
};
2257+
} else {
2258+
return () => listener.remove();
2259+
}
22502260
};
22512261

22522262
firebase.firestore.doc = (collectionPath: string, documentPath?: string): firestore.DocumentReference => {

0 commit comments

Comments
 (0)