Skip to content
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

(preact-iso) - avoid calling onLoadEnd on every render #864

Merged
merged 9 commits into from
Sep 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/great-drinks-obey.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"preact-iso": patch
---

Avoid calling onLoadEnd on every render
5 changes: 5 additions & 0 deletions .changeset/tall-beers-lay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'preact-iso': minor
---

Add `onRouteChange` prop to the Router to observe synchronous route changes
1 change: 1 addition & 0 deletions packages/preact-iso/router.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { AnyComponent, FunctionComponent, VNode } from 'preact';
export const LocationProvider: FunctionComponent;

export function Router(props: {
onRouteChange?: (url: string) => void;
onLoadEnd?: (url: string) => void;
onLoadStart?: (url: string) => void;
children?: VNode[];
Expand Down
18 changes: 13 additions & 5 deletions packages/preact-iso/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,14 @@ export function LocationProvider(props) {
}

const RESOLVED = Promise.resolve();

export function Router(props) {
const [, update] = useReducer(c => c + 1, 0);
const [c, update] = useReducer(c => c + 1, 0);

const { url, query, wasPush, path } = useLocation();
const { rest = path, params = {} } = useContext(RouteContext);

const isLoading = useRef(false);
const prevRoute = useRef(path);
// Monotonic counter used to check if an un-suspending route is still the current route:
const count = useRef(0);
// The current route:
Expand Down Expand Up @@ -140,6 +141,7 @@ export function Router(props) {

// Fire an event saying we're waiting for the route:
if (props.onLoadStart) props.onLoadStart(url);
isLoading.current = true;

// Re-render on unsuspend:
let c = count.current;
Expand Down Expand Up @@ -175,9 +177,15 @@ export function Router(props) {
hasEverCommitted.current = true;

// The route is loaded and rendered.
if (wasPush) scrollTo(0, 0);
if (props.onLoadEnd) props.onLoadEnd(url);
});
if (prevRoute.current !== path) {
if (wasPush) scrollTo(0, 0);
if (props.onLoadEnd && isLoading.current) props.onLoadEnd(url);
if (props.onRouteChange) props.onRouteChange(url);

isLoading.current = false;
prevRoute.current = path;
}
}, [path, wasPush, c]);

// Note: curChildren MUST render first in order to set didSuspend & prev.
return [h(RenderRef, { r: cur }), h(RenderRef, { r: prev })];
Expand Down
8 changes: 7 additions & 1 deletion packages/preact-iso/test/router.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,16 @@ describe('Router', () => {
const Profiles = jest.fn(() => html`<h1>Profiles</h1>`);
const Profile = jest.fn(({ params }) => html`<h1>Profile: ${params.id}</h1>`);
const Fallback = jest.fn(() => html`<h1>Fallback</h1>`);
const stack = [];
let loc;
render(
html`
<${LocationProvider}>
<${Router}>
<${Router}
onRouteChange=${url => {
stack.push(url);
}}
>
<${Home} path="/" />
<${Profiles} path="/profiles" />
<${Profile} path="/profiles/:id" />
Expand Down Expand Up @@ -111,6 +116,7 @@ describe('Router', () => {
path: '/other',
query: { a: 'b', c: 'd' }
});
expect(stack).toEqual(['/profiles', '/profiles/bob', '/other?a=b&c=d']);
});

it('should wait for asynchronous routes', async () => {
Expand Down