Last active
October 4, 2021 04:55
-
-
Save codediodeio/852c7dbdac472a40ec90deb32e615c04 to your computer and use it in GitHub Desktop.
rxfire Ideas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const firestore = firebase.firestore() | |
const doc$ = (path: string) => { | |
return Observable.create(observer => { | |
firestore | |
.doc(path) | |
.onSnapshot({ | |
next(doc) { | |
observer.next(doc); | |
} | |
}); | |
}); | |
} | |
// Custom Operators | |
// Maps snapshot to raw data | |
const data = (opts?: firebase.firestore.SnapshotOptions) => ( | |
source: Observable<firebase.firestore.DocumentSnapshot> | |
) => | |
new Observable<firebase.firestore.DocumentData>(observer => { | |
return source.subscribe({ | |
next(snap) { | |
observer.next(snap.data()); | |
} | |
}); | |
}); | |
// Plucks specific properties from the document data, like a client-side projection query | |
const pick = (props: string[]) => ( | |
source: Observable<firebase.firestore.DocumentSnapshot> | |
) => | |
new Observable<firebase.firestore.DocumentData>(observer => { | |
return source.subscribe({ | |
next(snap) { | |
const data = snap.data(); | |
observer.next(_.pick(data, props)); // Using lodash here | |
} | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { doc$, update$, pick, data } from 'rxfire'; | |
doc$('animals/elephant') | |
// Emits full snapshot | |
doc$('animals/elephant').pipe(data()) | |
// Emits data | |
doc$('animals/elephant') | |
.pipe( | |
pick(['imgURL', 'name']) | |
) | |
// Emits data only with projected properties | |
// { name: 'elephant', imgURL: '...' } | |
//// Writes //// | |
// fromPromise | |
update$(path, data).subscribe() | |
/// Refs /// |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment