From 7ac6439650b1ec57150528a3a0ac836d75cbe0f7 Mon Sep 17 00:00:00 2001 From: Dylan Watson Date: Mon, 20 Dec 2021 16:07:31 +0800 Subject: [PATCH 1/4] feat: Allow an initialValue to be passed as an option on data methods --- firestore/types.ts | 3 +++ firestore/useCollection.ts | 9 +++++---- firestore/useDocument.ts | 9 +++++---- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/firestore/types.ts b/firestore/types.ts index 0f3476d..da7bc0d 100644 --- a/firestore/types.ts +++ b/firestore/types.ts @@ -11,6 +11,9 @@ export type Options = { snapshotListenOptions?: firebase.firestore.SnapshotListenOptions; }; export type DataOptions = Options & IDOptions; +export type InitialValueOptions = { + initialValue?: T; +}; export type OnceOptions = { getOptions?: firebase.firestore.GetOptions; }; diff --git a/firestore/useCollection.ts b/firestore/useCollection.ts index 1d997bd..10ec2b4 100644 --- a/firestore/useCollection.ts +++ b/firestore/useCollection.ts @@ -9,6 +9,7 @@ import { OnceOptions, OnceDataOptions, Options, + InitialValueOptions, } from './types'; import { useIsEqualRef, useLoadingValue } from '../util'; @@ -32,7 +33,7 @@ export const useCollectionData = < RefField extends string = '' >( query?: firebase.firestore.Query | null, - options?: DataOptions + options?: DataOptions & InitialValueOptions ): CollectionDataHook => { return useCollectionDataInternal(true, query, options); }; @@ -43,7 +44,7 @@ export const useCollectionDataOnce = < RefField extends string = '' >( query?: firebase.firestore.Query | null, - options?: OnceDataOptions + options?: OnceDataOptions & InitialValueOptions ): CollectionDataHook => { return useCollectionDataInternal(false, query, options); }; @@ -100,7 +101,7 @@ const useCollectionDataInternal = < >( listen: boolean, query?: firebase.firestore.Query | null, - options?: DataOptions & OnceDataOptions + options?: DataOptions & OnceDataOptions & InitialValueOptions ): CollectionDataHook => { const idField = options ? options.idField : undefined; const refField = options ? options.refField : undefined; @@ -123,7 +124,7 @@ const useCollectionDataInternal = < transform ) ) - : undefined) as Data[], + : options && options.initialValue) as Data[], [snapshots, snapshotOptions, idField, refField, transform] ); diff --git a/firestore/useDocument.ts b/firestore/useDocument.ts index 960a833..9acfd80 100644 --- a/firestore/useDocument.ts +++ b/firestore/useDocument.ts @@ -9,6 +9,7 @@ import { OnceOptions, OnceDataOptions, Options, + InitialValueOptions, } from './types'; import { useIsEqualRef, useLoadingValue } from '../util'; @@ -32,7 +33,7 @@ export const useDocumentData = < RefField extends string = '' >( docRef?: firebase.firestore.DocumentReference | null, - options?: DataOptions + options?: DataOptions & InitialValueOptions ): DocumentDataHook => { return useDocumentDataInternal(true, docRef, options); }; @@ -43,7 +44,7 @@ export const useDocumentDataOnce = < RefField extends string = '' >( docRef?: firebase.firestore.DocumentReference | null, - options?: OnceDataOptions + options?: OnceDataOptions & InitialValueOptions ): DocumentDataHook => { return useDocumentDataInternal(false, docRef, options); }; @@ -100,7 +101,7 @@ const useDocumentDataInternal = < >( listen: boolean, docRef?: firebase.firestore.DocumentReference | null, - options?: DataOptions + options?: DataOptions & InitialValueOptions ): DocumentDataHook => { const idField = options ? options.idField : undefined; const refField = options ? options.refField : undefined; @@ -121,7 +122,7 @@ const useDocumentDataInternal = < refField, transform ) - : undefined) as Data, + : options && options.initialValue) as Data, [snapshot, snapshotOptions, idField, refField, transform] ); From 1a4130b53a6867f5948d77806b72440e7e249f9d Mon Sep 17 00:00:00 2001 From: Dylan Watson Date: Mon, 20 Dec 2021 19:10:57 +0800 Subject: [PATCH 2/4] Add a github workflow action for the build --- firestore/.github/workflows/build.yml | 28 +++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 firestore/.github/workflows/build.yml diff --git a/firestore/.github/workflows/build.yml b/firestore/.github/workflows/build.yml new file mode 100644 index 0000000..2269792 --- /dev/null +++ b/firestore/.github/workflows/build.yml @@ -0,0 +1,28 @@ +name: build +on: push +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-node@v2 + with: + node-version: '12' + + - name: Cache node modules + uses: actions/cache@v2 + env: + cache-name: cache-node-modules + with: + # npm cache files are stored in `~/.npm` on Linux/macOS + path: ~/.npm + key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }} + restore-keys: | + ${{ runner.os }}-build-${{ env.cache-name }}- + ${{ runner.os }}-build- + ${{ runner.os }}- + + - name: Install + run: npm install + - name: Build + run: npm run build From 910345c223a68d6ffd3b29c818e666369585b6de Mon Sep 17 00:00:00 2001 From: Dylan Watson Date: Mon, 20 Dec 2021 19:16:31 +0800 Subject: [PATCH 3/4] Move workflow file to the root --- {firestore/.github => .github}/workflows/build.yml | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {firestore/.github => .github}/workflows/build.yml (100%) diff --git a/firestore/.github/workflows/build.yml b/.github/workflows/build.yml similarity index 100% rename from firestore/.github/workflows/build.yml rename to .github/workflows/build.yml From 8b56bb8b2603ae4fa106cd442facdec8d264e216 Mon Sep 17 00:00:00 2001 From: Dylan Watson Date: Tue, 21 Dec 2021 23:22:26 +0800 Subject: [PATCH 4/4] Add documentation for the initialValue option --- firestore/README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/firestore/README.md b/firestore/README.md index fc65993..a8a0842 100644 --- a/firestore/README.md +++ b/firestore/README.md @@ -126,6 +126,7 @@ The `useCollectionData` hook takes the following parameters: - `snapshotListenOptions`: (optional) `firebase.firestore.SnapshotListenOptions` to customise how the collection is loaded - `snapshotOptions`: (optional) `firebase.firestore.SnapshotOptions` to customise how data is retrieved from snapshots - `transform`: (optional) a function that receives the raw `firebase.firestore.DocumentData` for each item in the collection to allow manual transformation of the data where required by the application. See [`Transforming data`](#transforming-data) below. + - `initialValue`: (optional) the initial value returned by the hook, until data from the firestore query has loaded Returns: @@ -150,6 +151,7 @@ The `useCollectionDataOnce` hook takes the following parameters: - `refField`: (optional) name of the field that should be populated with the `firebase.firestore.QuerySnapshot.ref` property. - `snapshotOptions`: (optional) `firebase.firestore.SnapshotOptions` to customise how data is retrieved from snapshots - `transform`: (optional) a function that receives the raw `firebase.firestore.DocumentData` for each item in the collection to allow manual transformation of the data where required by the application. See [`Transforming data`](#transforming-data) below. + - `initialValue`: (optional) the initial value returned by the hook, until data from the firestore query has loaded Returns: @@ -239,6 +241,7 @@ The `useDocumentData` hook takes the following parameters: - `snapshotListenOptions`: (optional) `firebase.firestore.SnapshotListenOptions` to customise how the collection is loaded - `snapshotOptions`: (optional) `firebase.firestore.SnapshotOptions` to customise how data is retrieved from snapshots - `transform`: (optional) a function that receives the raw `firebase.firestore.DocumentData` to allow manual transformation of the data where required by the application. See [`Transforming data`](#transforming-data) below. + - `initialValue`: (optional) the initial value returned by the hook, until data from the firestore query has loaded Returns: @@ -263,6 +266,7 @@ The `useDocumentDataOnce` hook takes the following parameters: - `refField`: (optional) name of the field that should be populated with the `firebase.firestore.QuerySnapshot.ref` property. - `snapshotOptions`: (optional) `firebase.firestore.SnapshotOptions` to customise how data is retrieved from snapshots - `transform`: (optional) a function that receives the raw `firebase.firestore.DocumentData` to allow manual transformation of the data where required by the application. See [`Transforming data`](#transforming-data) below. + - `initialValue`: (optional) the initial value returned by the hook, until data from the firestore query has loaded Returns: