Guest User

Untitled

a guest
Jun 24th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. const firestore = firebase.firestore()
  2.  
  3.  
  4. const doc$ = (path: string) => {
  5.  
  6. return Observable.create(observer => {
  7. firestore
  8. .doc(path)
  9. .onSnapshot({
  10. next(doc) {
  11. observer.next(doc);
  12. }
  13. });
  14. });
  15. }
  16.  
  17.  
  18. // Custom Operators
  19.  
  20. // Maps snapshot to raw data
  21. const data = (opts?: firebase.firestore.SnapshotOptions) => (
  22. source: Observable<firebase.firestore.DocumentSnapshot>
  23. ) =>
  24. new Observable<firebase.firestore.DocumentData>(observer => {
  25. return source.subscribe({
  26. next(snap) {
  27. observer.next(snap.data());
  28. }
  29. });
  30. });
  31.  
  32.  
  33.  
  34. // Plucks specific properties from the document data, like a client-side projection query
  35. const pick = (props: string[]) => (
  36. source: Observable<firebase.firestore.DocumentSnapshot>
  37. ) =>
  38. new Observable<firebase.firestore.DocumentData>(observer => {
  39. return source.subscribe({
  40. next(snap) {
  41. const data = snap.data();
  42. observer.next(_.pick(data, props)); // Using lodash here
  43. }
  44. });
  45. });
Add Comment
Please, Sign In to add comment