Skip to content

Instantly share code, notes, and snippets.

@angulartist
Last active March 27, 2019 18:41
Show Gist options
  • Save angulartist/63a5c2f58b3fa8c74977bc450ae05fd8 to your computer and use it in GitHub Desktop.
Save angulartist/63a5c2f58b3fa8c74977bc450ae05fd8 to your computer and use it in GitHub Desktop.
Reactive Firebase / BASICS - RxFire, Getting documents
/* Getting a single firestore document */
import { docData } from 'rxfire/firestore'
...
someFunction() {
const todoRef = db.doc('todos/myTodo')
const todo$ = docData(todoRef, 'id')
todo$.subscribe(todo => /* doSomething() */)
}
/* Getting a firestore collection */
import { collectionData } from 'rxfire/firestore'
...
someFunction() {
const todosRef = db.collection('todos')
const todos$ = collectionData(todoRef, 'id')
todos$.subscribe(todos => /* doSomething() */)
}
/**
* Preventing memory leaks
* (make a component instance available for garbage collection)
*/
import { takeUntil } from 'rxjs/operators'
...
destroy$: Subject<boolean> = new Subject<boolean>()
...
componentDidUnload() { // or whatever your unload hook is.
this.destroy$.next(true)
this.destroy$.unsubscribe()
}
...
someFunction() {
const todosRef = db.collection('todos')
const todos$ = collectionData(todoRef, 'id')
todos$
// unsub when destroy$ obs emits
.pipe(takeUntil(this.destroy$))
.subscribe(todos => /* doSomething() */)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment