Skip to content

Commit

Permalink
fix(perf): don't delay routing events
Browse files Browse the repository at this point in the history
This was like this due to an older performance issue where the popstate handler (that this code runs in) was taking a long time leading to UI lag. So the processing of the events and subsequent react re-render was moved to the next tick.

However the delay introduced by moving it to the next tick is actually worse
  • Loading branch information
UberMouse committed Sep 8, 2022
1 parent 7effe85 commit fb898fc
Showing 1 changed file with 9 additions and 13 deletions.
22 changes: 9 additions & 13 deletions src/routing/handleLocationChange/handleLocationChange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,16 @@ export function handleLocationChange(

setActiveRouteEvents([...routingEvents, match.event]);

// Bumps the processing for the state machines to the next event tick
// and out of the popstate handler
setTimeout(() => {
while (routingEvents.length > 0) {
const event = routingEvents.pop()!;
// copy the originalUrl to all parent events
event.originalUrl = match.event.originalUrl;

// @ts-ignore the event won't match GlobalEvents
broadcast(event);
}
while (routingEvents.length > 0) {
const event = routingEvents.pop()!;
// copy the originalUrl to all parent events
event.originalUrl = match.event.originalUrl;

// @ts-ignore the event won't match GlobalEvents
broadcast(matchedEvent);
}, 0);
broadcast(event);
}

// @ts-ignore the event won't match GlobalEvents
broadcast(matchedEvent);
}
}

0 comments on commit fb898fc

Please sign in to comment.