-
Notifications
You must be signed in to change notification settings - Fork 404
fix: surface observable errors via status instead of re-throwing #735
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: v5
Are you sure you want to change the base?
Changes from all commits
b113bc3
776c993
74aee91
26cd552
c7d3d6a
13bfb96
91ea1ac
c6ddca8
e021670
0971021
7286c57
11286bf
0fde812
bacf14b
a26a9c0
7b2adce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| CLAUDE.local* | ||
| .DS_Store | ||
| npm-debug.log | ||
|
|
||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| import * as React from 'react'; | ||
| import { getDownloadURL, fromTask } from 'rxfire/storage'; | ||
| import { defer } from 'rxjs'; | ||
| import { ReactFireOptions, useObservable, ObservableStatus, useStorage } from './'; | ||
| import { useSuspenseEnabledFromConfigAndContext } from './firebaseApp'; | ||
| import { ref } from 'firebase/storage'; | ||
|
|
@@ -28,7 +29,7 @@ export function useStorageTask<T = unknown>(task: UploadTask, ref: StorageRefere | |
| */ | ||
| export function useStorageDownloadURL<T = string>(ref: StorageReference, options?: ReactFireOptions<T>): ObservableStatus<string | T> { | ||
| const observableId = `storage:downloadUrl:${ref.toString()}`; | ||
| const observable$ = getDownloadURL(ref); | ||
| const observable$ = defer(() => getDownloadURL(ref)); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this fix related to the error behavior change? If not, could you please break it out into its own PR so we can include it in a v4 patch release? |
||
|
|
||
| return useObservable(observableId, observable$, options); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,9 @@ import '@testing-library/jest-dom/extend-expect'; | |
| import { act, cleanup, render, renderHook, waitFor } from '@testing-library/react'; | ||
| import * as React from 'react'; | ||
| import { of, Subject, BehaviorSubject, throwError } from 'rxjs'; | ||
| import { useObservable } from '../src/index'; | ||
| import { useObservable, FirebaseAppProvider } from '../src/index'; | ||
| import { initializeApp } from 'firebase/app'; | ||
| import { baseConfig } from './appConfig'; | ||
|
|
||
| describe('useObservable', () => { | ||
| afterEach(cleanup); | ||
|
|
@@ -124,10 +126,46 @@ describe('useObservable', () => { | |
|
|
||
| act(() => observable$.next('val')); | ||
| expect(result.current.isComplete).toEqual(false); | ||
|
|
||
| act(() => observable$.complete()); | ||
| await waitFor(() => expect(result.current.isComplete).toEqual(true)); | ||
| }); | ||
|
|
||
| it('surfaces errors via status in non-suspense mode', async () => { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The #535 marquee scenario (
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added. Had to pair it with a |
||
| const error = new Error('I am an error'); | ||
| const observable$ = throwError(error); | ||
|
|
||
| const { result } = renderHook(() => useObservable('test-error-non-suspense', observable$, { suspense: false })); | ||
|
|
||
| await waitFor(() => expect(result.current.status).toEqual('error')); | ||
| expect(result.current.error).toEqual(error); | ||
| }); | ||
|
|
||
| it('surfaces errors via status when no suspense option is provided', async () => { | ||
| const error = new Error('default mode error'); | ||
| const observable$ = throwError(error); | ||
|
|
||
| const { result } = renderHook(() => useObservable('test-error-default-mode', observable$)); | ||
|
|
||
| await waitFor(() => expect(result.current.status).toEqual('error')); | ||
| expect(result.current.error).toEqual(error); | ||
| }); | ||
|
|
||
| it('retains last emitted data when observable errors after emitting', async () => { | ||
| const subject$ = new Subject<string>(); | ||
| const error = new Error('late error'); | ||
|
|
||
| const { result } = renderHook(() => useObservable('test-late-error', subject$, { suspense: false })); | ||
|
|
||
| act(() => subject$.next('good value')); | ||
| await waitFor(() => expect(result.current.status).toEqual('success')); | ||
| expect(result.current.data).toEqual('good value'); | ||
|
|
||
| act(() => subject$.error(error)); | ||
| await waitFor(() => expect(result.current.status).toEqual('error')); | ||
| expect(result.current.error).toEqual(error); | ||
| expect(result.current.data).toEqual('good value'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('Suspense Mode', () => { | ||
|
|
@@ -328,5 +366,29 @@ describe('useObservable', () => { | |
| // if useObservable doesn't re-emit, the value here will still be "Jeff" | ||
| expect(refreshedComp).toHaveTextContent('James'); | ||
| }); | ||
| it('throws an error via FirebaseAppProvider suspense context path', () => { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Verified this exercises the context branch (hook called with no config, so the provider's |
||
| const spy = vi.spyOn(console, 'error'); | ||
| spy.mockImplementation(() => {}); | ||
|
|
||
| const onError = (e: ErrorEvent) => e.preventDefault(); | ||
| window.addEventListener('error', onError); | ||
|
|
||
| const app = initializeApp(baseConfig, 'suspense-context-test'); | ||
| const error = new Error('context-path error'); | ||
| const observable$ = throwError(error); | ||
|
|
||
| const wrapper = ({ children }: { children: React.ReactNode }) => ( | ||
| <FirebaseAppProvider firebaseApp={app} suspense={true}> | ||
| {children} | ||
| </FirebaseAppProvider> | ||
| ); | ||
|
|
||
| expect(() => renderHook(() => useObservable('test-context-suspense-error', observable$), { wrapper })).toThrow( | ||
| expect.objectContaining({ message: 'context-path error' }) | ||
| ); | ||
|
|
||
| spy.mockRestore(); | ||
| window.removeEventListener('error', onError); | ||
| }); | ||
| }); | ||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This
defer()is a real fix, not just scope creep: rxfire'sgetDownloadURLis eager, so every re-render fired a discarded request and a rejecting one produced an unhandled rejection (your new test fails without this wrapper). Worth noting the same eager pattern remains inuseIdTokenResultanduseCallableFunctionResponse(the callable side-effects per render) — fine to leave for follow-ups, just worth a line in the PR body so it doesn't read as an oversight.