Skip to content

Commit 9db29b8

Browse files
committed
fix: let useSubscription reject when onlyOnce fetchFn fails
1 parent 9ec098f commit 9db29b8

File tree

5 files changed

+49
-7
lines changed

5 files changed

+49
-7
lines changed

firebase.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,8 @@
1818
},
1919
"functions": {
2020
"predeploy": "npm --prefix \"$RESOURCE_DIR\" run build"
21+
},
22+
"firestore": {
23+
"rules": "./firestore.rules"
2124
}
22-
}
25+
}

firestore.rules

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
service cloud.firestore {
2+
match /databases/{database}/documents {
3+
match /noread/{document=**} {
4+
allow read: if false;
5+
}
6+
}
7+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,4 @@
5959
"format": "prettier --write ."
6060
},
6161
"dependencies": {}
62-
}
62+
}

packages/firestore/__test__/useFirestoreDocument.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,37 @@ describe("useFirestoreDocument", () => {
369369

370370
expect(mock2.mock.calls.length).toBe(2);
371371
});
372+
373+
test("it should propagate the error when not subscribing", async () => {
374+
const hookId = genId();
375+
const id = genId();
376+
const ref = doc(firestore, "noread", id);
377+
const { result, waitFor } = renderHook(
378+
() =>
379+
useFirestoreDocument(hookId, ref, {
380+
subscribe: false,
381+
}),
382+
{
383+
wrapper,
384+
}
385+
);
386+
await waitFor(() => result.current.isError, { timeout: 5000 });
387+
});
388+
test("it should propagate the error when subscribing", async () => {
389+
const hookId = genId();
390+
const id = genId();
391+
const ref = doc(firestore, "noread", id);
392+
const { result, waitFor } = renderHook(
393+
() =>
394+
useFirestoreDocument(hookId, ref, {
395+
subscribe: true,
396+
}),
397+
{
398+
wrapper,
399+
}
400+
);
401+
await waitFor(() => result.current.isError, { timeout: 5000 });
402+
});
372403
});
373404

374405
describe("useFirestoreDocumentData", () => {

packages/utils/src/useSubscription.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ type UseSubscriptionOptions<TData, TError, R> = UseQueryOptions<
5151
* Utility hook to subscribe to events, given a function that returns an observer callback.
5252
* @param queryKey The react-query queryKey
5353
* @param subscriptionKey A hashable key to store the subscription
54-
* @param subscribeFn Returns an unsubscribe function to the event, the argument of subscribeFn is a callback to set data.
54+
* @param subscribeFn Returns an unsubscribe function to the event
5555
* @param options
5656
* @returns
5757
*/
@@ -89,10 +89,11 @@ export function useSubscription<TData, TError, R = TData>(
8989
}, []);
9090

9191
let resolvePromise: (data: TData | null) => void = () => null;
92-
92+
let rejectPromise: (err: any) => void = () => null;
9393
const result: CancellablePromise<TData | null> = new Promise<TData | null>(
94-
(resolve) => {
94+
(resolve, reject) => {
9595
resolvePromise = resolve;
96+
rejectPromise = reject;
9697
}
9798
);
9899

@@ -121,9 +122,9 @@ export function useSubscription<TData, TError, R = TData>(
121122
}
122123
} else {
123124
if (!options.fetchFn) {
124-
throw new Error("please specify fetchFn");
125+
throw new Error("You must specify fetchFn if using onlyOnce mode.");
125126
} else {
126-
options.fetchFn().then((res: TData) => resolvePromise(res));
127+
options.fetchFn().then(resolvePromise).catch(rejectPromise);
127128
}
128129
}
129130

0 commit comments

Comments
 (0)