Skip to content

Add useFirestoreCollectionDataOnce#252

Open
banyan wants to merge 1 commit into
FirebaseExtended:mainfrom
banyan:use-firestore-collection-data-once
Open

Add useFirestoreCollectionDataOnce#252
banyan wants to merge 1 commit into
FirebaseExtended:mainfrom
banyan:use-firestore-collection-data-once

Conversation

@banyan

@banyan banyan commented May 3, 2020

Copy link
Copy Markdown
Contributor

A part of #137

I've implemented this with reference to #211 :)

@googlebot

Copy link
Copy Markdown

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please visit https://cla.developers.google.com/ to sign.

Once you've signed (or fixed any issues), please reply here with @googlebot I signed it! and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

ℹ️ Googlers: Go here for more info.

@banyan

banyan commented May 3, 2020

Copy link
Copy Markdown
Contributor Author

@googlebot I signed it!

@googlebot

Copy link
Copy Markdown

CLAs look good, thanks!

ℹ️ Googlers: Go here for more info.

@googlebot googlebot added cla: yes and removed cla: no labels May 3, 2020
const idField = checkIdField(options);
const queryId = `firestore:collectionDataOnce:${getUniqueIdForFirestoreQuery(query)}:idField=${idField}`;

return useObservable(collectionData(query, idField).pipe(first()), queryId, checkStartWithValue(options));

@davideast davideast May 5, 2020

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is tricky. I'm not sure if using first() to terminate an onSnapshot() listener is the right call when we could use .get() which is meant for one time reads.

What's tricky is that we don't support this natively in RxFire and ReactFire does not take a dependency of RxJS. This means I don't think we can directly create an observable from a promise like we would below in RxFire.

function getCollectionData(colRef) {
  return from(citiesCol.get());
}

So maybe we just add that to RxFire and then add that here? What are you thoughts? I'm happy to make the PR to RxFire or if you'd like to let me know as well.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the insight!

we could use .get() which is meant for one time reads.

I totally agree.

ReactFire does not take a dependency of RxJS.

Maybe I don't understand it properly, but it looks like it does have the dependency on rxjs.
So it looks like I could write the following if I want, but maybe it's just not a good thing design-wise for the reactfire and rxfire libraries (I'm not sure).

diff --git a/reactfire/firestore/index.tsx b/reactfire/firestore/index.tsx
index 0974b14..14971b1 100644
--- a/reactfire/firestore/index.tsx
+++ b/reactfire/firestore/index.tsx
@@ -1,9 +1,10 @@
 import { firestore } from 'firebase/app';
-import { collectionData, doc, docData, fromCollectionRef } from 'rxfire/firestore';
+import { collectionData, doc, docData, fromCollectionRef, snapToData } from 'rxfire/firestore';
 import { preloadFirestore, ReactFireOptions, useObservable, checkIdField, checkStartWithValue } from '..';
 import { preloadObservable } from '../useObservable';
-import { first } from 'rxjs/operators';
+import { map, first } from 'rxjs/operators';
 import { useFirebaseApp } from '../firebaseApp';
+import { from } from 'rxjs';
 
 const CACHED_QUERIES = '_reactFireFirestoreQueryCache';
 
@@ -124,6 +125,6 @@ export function useFirestoreCollectionData<T = { [key: string]: unknown }>(query
 export function useFirestoreCollectionDataOnce<T = { [key: string]: unknown }>(query: firestore.Query, options?: ReactFireOptions<T[]>): T[] {
   const idField = checkIdField(options);
   const queryId = `firestore:collectionDataOnce:${getUniqueIdForFirestoreQuery(query)}:idField=${idField}`;
-
-  return useObservable(collectionData(query, idField).pipe(first()), queryId, checkStartWithValue(options));
+  const onetimeCollectionData = from(query.get()).pipe(map(snapshot => snapshot.docs.map(snap => snapToData(snap, idField) as T)));
+  return useObservable(onetimeCollectionData, queryId, checkStartWithValue(options));
 }

If you'd like me to put some logic on the rxfire side, I'd be happy to do so! Please let me know. Thanks!

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@davideast and @banyan any update on this?
It would be a great addition to reactfire.

@davideast davideast left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @banyan!

Thank you so much for the PR with the docs and the tests! I have one small question about the implementation, so let me know your thoughts.

@steurt

steurt commented Jun 12, 2020

Copy link
Copy Markdown

Would be great to have this useFirestoreCollectionDataOnce. Any progress @banyan ?

Base automatically changed from master to main January 20, 2021 19:11
@n-sviridenko

Copy link
Copy Markdown

Hey guys. Any progress here? This seems to be pretty handy to have.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants