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

feat: createBaseRouter in TS #1022

Merged
merged 4 commits into from
Dec 27, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions src/Redirect.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
/* eslint-disable max-classes-per-file */
import React from 'react';

import RedirectException from './RedirectException';
import { Match, RedirectOptions } from './typeUtils';
import { LocationDescriptor, Match, RedirectOptions } from './typeUtils';

class Redirect implements RedirectOptions {
path?: string;

to: string | ((match: Match) => LocationDescriptor);

status?: number;
Comment on lines +8 to +12
Copy link
Collaborator Author

@golota60 golota60 Dec 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I decided to just declare fields on classes. Technically those are functional changes, but practically they should not change anything. I'm pretty sure the only difference is that this has those initialized as undefined when creating the instance, which should not change anything


class Redirect {
constructor({ from, to, status }: RedirectOptions) {
// @ts-ignore
this.path = from;
// @ts-ignore
this.to = to;
// @ts-ignore
this.status = status;
}

Expand All @@ -32,4 +38,10 @@ if (__DEV__) {
(Redirect.prototype as any).isReactComponent = {};
}

export default Redirect;
// This actually doesn't extend a React.Component, but we need consumer to think that it does
declare class RedirectType extends React.Component<RedirectOptions> {
// @ts-ignore
constructor(config: RedirectOptions);
}

export default Redirect as unknown as RedirectType;
4 changes: 3 additions & 1 deletion src/additional.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ declare global {
declare let __DEV__: boolean;
interface Window {
__FOUND_HOT_RELOAD__: boolean;
__FOUND_REPLACE_ROUTE_CONFIG__: (routeConfig: RouteConfig) => void;
__FOUND_REPLACE_ROUTE_CONFIG__?: (
routeConfig: RouteConfig,
) => void | undefined;
}
}

Expand Down
59 changes: 53 additions & 6 deletions src/createBaseRouter.js → src/createBaseRouter.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { dequal } from 'dequal';
import React from 'react';
import { Store } from 'redux';
import warning from 'tiny-warning';

import ActionTypes from './ActionTypes';
Expand All @@ -8,6 +9,38 @@ import StaticContainer from './StaticContainer';
import createRender from './createRender';
import createStoreRouterObject from './createStoreRouterObject';
import resolveRenderArgs from './resolveRenderArgs';
import {
ConnectedRouterProps,
CreateRenderOptions,
Match,
MatchBase,
RenderArgs,
Resolver,
Router,
} from './typeUtils';

interface CreateProps extends CreateRenderOptions {
render?: (args: RenderArgs) => React.ReactElement;
}

interface BaseRouterProps extends ConnectedRouterProps {
store: Store;
match: MatchBase;
resolvedMatch: MatchBase;
}

interface BaseRouterState {
isInitialRender: boolean;
match: MatchBase;
matchContext: any;
resolver: Resolver;
iteration: number;
routerContext: {
match: Match | null;
router: Router;
};
element: React.ReactElement | null;
}

export default function createBaseRouter({
renderPending,
Expand Down Expand Up @@ -37,9 +70,19 @@ export default function createBaseRouter({
renderReady,
renderError,
}),
}) {
class BaseRouter extends React.Component {
constructor(props) {
}: CreateProps): React.ComponentClass<BaseRouterProps, BaseRouterState> {
class BaseRouter extends React.Component<BaseRouterProps, BaseRouterState> {
router: Router;

lastIteration: number;

pendingResolvedMatch: boolean;

dispatchMatch: (pendingMatch: any) => void;

mounted?: boolean;

constructor(props: BaseRouterProps) {
super(props);

const { store, match, matchContext, resolver, initialRenderArgs } =
Expand Down Expand Up @@ -99,7 +142,10 @@ export default function createBaseRouter({
}
}

static getDerivedStateFromProps({ match, resolver, matchContext }, state) {
static getDerivedStateFromProps(
{ match, resolver, matchContext }: BaseRouterProps,
state: BaseRouterState,
) {
if (state.isInitialRender) {
return { isInitialRender: false };
}
Expand Down Expand Up @@ -148,7 +194,8 @@ export default function createBaseRouter({
try {
for await (const renderArgs of resolveRenderArgs(
this.router,
this.props,
// TODO: this should be removed once resolveRenderArgs is in TS
this.props as any,
)) {
// Don't do anything if we're resolving an outdated match.
if (!this.mounted || this.lastIteration !== pendingIteration) {
Expand Down Expand Up @@ -178,7 +225,7 @@ export default function createBaseRouter({
this.dispatchMatch(pendingMatch);
}
}
} catch (e) {
} catch (e: any) {
if (!this.mounted || this.lastIteration !== pendingIteration) {
return;
}
Expand Down
15 changes: 11 additions & 4 deletions src/createConnectedRouter.js → src/createConnectedRouter.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
import React from 'react';
import { shallowEqual, useSelector, useStore } from 'react-redux';
import { Store } from 'redux';

import createBaseRouter from './createBaseRouter';
import {
ConnectedRouterOptions,
ConnectedRouterProps,
ConnectedRouter as ConnectedRouterType,
FoundState,
} from './typeUtils';

export default function createConnectedRouter({
getFound = ({ found }) => found,
getFound = ({ found }: any) => found as FoundState,
...options
}) {
}: ConnectedRouterOptions): ConnectedRouterType {
const Router = createBaseRouter(options);

const getFoundState = (state) => {
const getFoundState = (state: Store) => {
const { match, resolvedMatch } = getFound(state);
return { match, resolvedMatch };
};

function ConnectedRouter(props) {
function ConnectedRouter(props: ConnectedRouterProps) {
const store = useStore();
const foundState = useSelector(getFoundState, shallowEqual);

Expand Down
1 change: 0 additions & 1 deletion src/createFarceRouter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export default function createFarceRouter({
getFound = ({ found }: any) => found as FoundState,
...options
}: FarceRouterOptions): FarceRouter {
// @ts-ignore TODO: take care of it once createBaseRouter is in TS
const Router = createBaseRouter(options);

const store =
Expand Down