UNPKG

reactfire

Version:
37 lines (36 loc) 1.63 kB
import { collectionData, doc, docData, fromCollectionRef } from 'rxfire/firestore'; import { preloadFirestore, useObservable } from '..'; import { preloadObservable } from '../useObservable'; export function preloadFirestoreDoc(refProvider, firebaseApp) { return preloadFirestore(firebaseApp).then(function (firestore) { var ref = refProvider(firestore()); return preloadObservable(doc(ref), ref.path); }); } export function useFirestoreDoc(ref, options) { return useObservable(doc(ref), 'firestore doc: ' + ref.path, options ? options.startWithValue : undefined); } export function useFirestoreDocData(ref, options) { return useObservable(docData(ref, checkIdField(options)), 'firestore docdata: ' + ref.path, checkStartWithValue(options)); } export function useFirestoreCollection(query, options) { var queryId = getHashFromFirestoreQuery(query); return useObservable(fromCollectionRef(query, checkIdField(options)), queryId, options ? options.startWithValue : undefined); } function getHashFromFirestoreQuery(query) { var hash = query._query.canonicalId(); return "firestore: " + hash; } export function useFirestoreCollectionData(query, options) { var queryId = getHashFromFirestoreQuery(query); return useObservable(collectionData(query, checkIdField(options)), queryId, checkStartWithValue(options)); } function checkOptions(options, field) { return options ? options[field] : undefined; } function checkStartWithValue(options) { return checkOptions(options, 'startWithValue'); } function checkIdField(options) { return checkOptions(options, 'idField'); }