I am interested in implementing a nested listener structure similar to the following:
snapshotListeners() {
firestore()
.collectionGroup()
.where()
.onSnapshot({
error: //Handle error
next: firstSnapshot => {
firstSnapshot.docsChanges().forEach(change => {
//Data retrieved and used in the below query to get more data
firestore()
.collection()
.where() //using the data retrived from the above query here.
.onSnapshot({
error: //Handle error
next: secondSnapshot => {
secondSnapshot.docsChanges().forEach(change =>{
//More Data from second query
})
}
})
})
}
})
}
This code snippet was taken from Nesting firestore onSnapshot listeners in react-native?
One question I have is how to manage the created listeners when the outer listener triggers. How can I properly unsubscribe the previous inner listener when a new one is set up? Can simply overwriting an old listener variable unsubscribe it, like this:
var unsubscribe = old_listener
unsubscribe = new_listener
// would this unsubscribe old listener?
If that method doesn't work, I would appreciate advice on the best approach to keep track of these listeners. Frank suggests using a list, but as a beginner in Javascript, I'm unsure how to implement this. I understand the concept of appending each new listener to a list, but how do I then call them as a function to unsubscribe?