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

chore(remix-react): add subscribe method to transitionManager #3964

Merged
merged 1 commit into from
Aug 8, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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