I’m trying to check the Firestore collections of id by using debounceTime() if IDs exist to avoid a high frequency of reads. But, it seems that the debounceTime() is not working as expected, as it reads almost instantly.
Here is the function calling checking for the id:
checkId(id: string) {
debounceTime(3000);
return getDoc(doc(this.firestore, 'users', id))
}
I definitely think I’m using debounce time wrongly here.
3
Answers
Not sure if this is the most functional to use with Firebase/Firestore technique.
I've tried using @CCbet suggestion, return as Observable, but it still does not denounce.
So, with @martin suggestion, I look into the rxjs.dev/guide/operators documentation and wrap the function in an rxjs Subject and call it.
This seems to work. Let me know if there is a caveat.
debounceTime
is an operator, so you’ll have to use it in a pipe so that it applies on the data stream that you want to.getDoc returns a Promise so you could convert it to an Observable first and the simply apply the
debounceTime
operator.@HassanMoin‘s solution is almost right, but there’s a huge issue on it : calling the function
randomFunction
will return a new instance of the observable, creating a new subscription.Try making a variable for it, for instance :
This way you have a single subscrpition, that automatically gets cancelles when you change the ID.