-
-
Notifications
You must be signed in to change notification settings - Fork 614
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
fix(core): Allow retries for errored async reads #705
Changes from all commits
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,4 +1,4 @@ | ||
import { Component, Suspense } from 'react' | ||
import { Suspense } from 'react' | ||
import { fireEvent, render } from '@testing-library/react' | ||
import { Observable, Subject } from 'rxjs' | ||
import { useAtom } from '../../src/index' | ||
|
@@ -7,31 +7,6 @@ import { getTestProvider } from '../testUtils' | |
|
||
const Provider = getTestProvider() | ||
|
||
class ErrorBoundary extends Component< | ||
{ message?: string }, | ||
{ hasError: boolean } | ||
> { | ||
constructor(props: { message?: string }) { | ||
super(props) | ||
this.state = { hasError: false } | ||
} | ||
static getDerivedStateFromError() { | ||
return { hasError: true } | ||
} | ||
render() { | ||
return this.state.hasError ? ( | ||
<div> | ||
<div>{this.props.message || 'errored'}</div> | ||
<button onClick={() => this.setState({ hasError: false })}> | ||
retry | ||
</button> | ||
</div> | ||
) : ( | ||
this.props.children | ||
) | ||
} | ||
} | ||
|
||
it('count state', async () => { | ||
const observableAtom = atomWithObservable( | ||
() => | ||
|
@@ -95,32 +70,3 @@ it('writable count state', async () => { | |
fireEvent.click(getByText('button')) | ||
await findByText('count: 9') | ||
}) | ||
|
||
// FIXME we would like to support retry | ||
it.skip('count state with error', 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. This test wasn't valid - an observable can't recover from an error state. For the notion of "retrying" observables to work, the error would never reach 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. So, I didn't know about the observable error behavior, and created this test. But, it's an expected behavior. |
||
const myObservable = new Observable<number>((subscriber) => { | ||
subscriber.error('err1') | ||
subscriber.next(1) | ||
}) | ||
const observableAtom = atomWithObservable(() => myObservable) | ||
|
||
const Counter = () => { | ||
const [state] = useAtom(observableAtom) | ||
|
||
return <div>count: {state}</div> | ||
} | ||
|
||
const { findByText, getByText } = render( | ||
<Provider> | ||
<ErrorBoundary> | ||
<Suspense fallback="loading"> | ||
<Counter /> | ||
</Suspense> | ||
</ErrorBoundary> | ||
</Provider> | ||
) | ||
|
||
await findByText('errored') | ||
fireEvent.click(getByText('retry')) | ||
await findByText('count: 1') | ||
}) |
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 was being done when writing to a sync atom, but not an async atom.
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.
Wow! Nice catch!
Feels like you have fairly deeper understanding of core.