Skip to content

Commit

Permalink
chore: add subscribe method to transition manager (#3964)
Browse files Browse the repository at this point in the history
chore: update RemixEntry to use new subscribe method
  • Loading branch information
jacob-ebey committed Aug 8, 2022
1 parent 845c6c3 commit 1497ab3
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/slow-apples-pretend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/react": patch
---

Add subscribe method to transition manager to allow subscribing and un-subscribing for React 18 strict mode compliance.
4 changes: 1 addition & 3 deletions packages/remix-react/__tests__/transition-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ describe("init", () => {
actionData: { root: "ACTION DATA" },
error: new Error("lol"),
errorBoundaryId: "root",
onChange: () => {},
onRedirect: () => {},
});
expect(tm.getState()).toMatchInlineSnapshot(`
Expand Down Expand Up @@ -1956,7 +1955,6 @@ function createTestTransitionManager(
loaderData: { root: "ROOT" },
location,
routes: [],
onChange() {},
onRedirect() {},
...init,
});
Expand Down Expand Up @@ -2090,11 +2088,11 @@ let setup = ({ url } = { url: "/" }) => {
];

let tm = createTestTransitionManager(url, {
onChange: handleChange,
onRedirect: handleRedirect,
loaderData: { root: "ROOT" },
routes,
});
tm.subscribe(handleChange);

let navigate_ = (
location: Location | string,
Expand Down
34 changes: 22 additions & 12 deletions packages/remix-react/components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ import type { RouteMatch as BaseRouteMatch } from "./routeMatching";
import { matchClientRoutes } from "./routeMatching";
import type { RouteModules, HtmlMetaDescriptor } from "./routeModules";
import { createTransitionManager } from "./transition";
import type { Transition, Fetcher, Submission } from "./transition";
import type {
Transition,
TransitionManagerState,
Fetcher,
Submission,
} from "./transition";

////////////////////////////////////////////////////////////////////////////////
// RemixEntry
Expand Down Expand Up @@ -117,20 +122,25 @@ export function RemixEntry({
catch: entryComponentDidCatchEmulator.catch,
catchBoundaryId: entryComponentDidCatchEmulator.catchBoundaryRouteId,
onRedirect: _navigator.replace,
onChange: (state) => {
setClientState({
catch: state.catch,
error: state.error,
catchBoundaryRouteId: state.catchBoundaryId,
loaderBoundaryRouteId: state.errorBoundaryId,
renderBoundaryRouteId: null,
trackBoundaries: false,
trackCatchBoundaries: false,
});
},
});
});

React.useEffect(() => {
let subscriber = (state: TransitionManagerState) => {
setClientState({
catch: state.catch,
error: state.error,
catchBoundaryRouteId: state.catchBoundaryId,
loaderBoundaryRouteId: state.errorBoundaryId,
renderBoundaryRouteId: null,
trackBoundaries: false,
trackCatchBoundaries: false,
});
};

return transitionManager.subscribe(subscriber);
}, [transitionManager]);

// Ensures pushes interrupting pending navigations use replace
// TODO: Move this to React Router
let navigator: Navigator = React.useMemo(() => {
Expand Down
19 changes: 17 additions & 2 deletions packages/remix-react/transition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,13 @@ export interface TransitionManagerInit {
error?: Error;
catchBoundaryId?: null | string;
errorBoundaryId?: null | string;
onChange: (state: TransitionManagerState) => void;
onRedirect: (to: string, state?: any) => void;
}

export type TransitionManagerSubscriber = (
state: TransitionManagerState
) => void;

export interface Submission {
action: string;
method: string;
Expand Down Expand Up @@ -422,6 +425,7 @@ export function createTransitionManager(init: TransitionManagerInit) {
let navigationLoadId = -1;
let fetchReloadIds = new Map<string, number>();
let fetchRedirectIds = new Set<string>();
let subscribers = new Set<TransitionManagerSubscriber>();

let matches = matchClientRoutes(routes, init.location);

Expand Down Expand Up @@ -462,7 +466,10 @@ export function createTransitionManager(init: TransitionManagerInit) {
}

state = Object.assign({}, state, updates);
init.onChange(state);

for (let subscriber of subscribers.values()) {
subscriber(state);
}
}

function getState(): TransitionManagerState {
Expand Down Expand Up @@ -1382,7 +1389,15 @@ export function createTransitionManager(init: TransitionManagerInit) {
markFetchersDone(doneKeys);
}

function subscribe(subscriber: TransitionManagerSubscriber) {
subscribers.add(subscriber);
return () => {
subscribers.delete(subscriber);
};
}

return {
subscribe,
send,
getState,
getFetcher,
Expand Down

0 comments on commit 1497ab3

Please sign in to comment.