Skip to content

Commit

Permalink
fix: inconsistent loading state in SourceLoaderUri
Browse files Browse the repository at this point in the history
  • Loading branch information
jsamr committed Jun 9, 2021
1 parent 4bc3f38 commit a530c5c
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions packages/render-html/src/SourceLoaderUri.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@ import RenderTTree from './RenderTTree';
import sourceLoaderContext from './context/sourceLoaderContext';

interface LoaderInternalState {
loading: boolean;
error: boolean;
resolvedHTML: string | null;
}

const ERROR_STATE = {
error: true,
loading: false,
resolvedHTML: null
};

Expand All @@ -29,8 +27,7 @@ async function loadHTMLResource(
const html = await response.text();
return {
resolvedHTML: html,
error: false,
loading: false
error: false
};
}
return ERROR_STATE;
Expand All @@ -43,7 +40,6 @@ export type UriSourceLoaderProps = {
function useUriSourceLoader({ source, onHTMLLoaded }: UriSourceLoaderProps) {
const [loadState, setState] = useState<LoaderInternalState>({
error: false,
loading: false,
resolvedHTML: null
});
const { error } = loadState;
Expand All @@ -52,7 +48,7 @@ function useUriSourceLoader({ source, onHTMLLoaded }: UriSourceLoaderProps) {
useEffect(() => {
let cancelled = false;
if (!error) {
setState({ loading: true, error: false, resolvedHTML: null });
setState({ error: false, resolvedHTML: null });
loadHTMLResource(source.uri, {
body: source.body,
headers: source.headers,
Expand All @@ -79,12 +75,12 @@ export default function SourceLoaderUri(props: UriSourceLoaderProps) {
const { remoteErrorView, remoteLoadingView } = useContext(
sourceLoaderContext
);
const { resolvedHTML, error, loading } = useUriSourceLoader(props);
const { resolvedHTML, error } = useUriSourceLoader(props);
if (error) {
return remoteErrorView!.call(null, props.source);
return remoteErrorView.call(null, props.source);
}
if (loading) {
return remoteLoadingView!.call(null, props.source);
if (resolvedHTML === null) {
return remoteLoadingView.call(null, props.source);
}
return React.createElement(RenderTTree, {
document: resolvedHTML!,
Expand Down

0 comments on commit a530c5c

Please sign in to comment.