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

refactor: improve client modules types #6742

Merged
merged 1 commit into from
Feb 23, 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
5 changes: 3 additions & 2 deletions packages/docusaurus-module-type-aliases/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
*/

declare module '@generated/client-modules' {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const clientModules: readonly any[];
import type {ClientModule} from '@docusaurus/types';

const clientModules: readonly (ClientModule & {default: ClientModule})[];
export default clientModules;
}

Expand Down
9 changes: 9 additions & 0 deletions packages/docusaurus-types/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type {CommanderStatic} from 'commander';
import type {ParsedUrlQueryInput} from 'querystring';
import type Joi from 'joi';
import type {Overwrite, DeepPartial} from 'utility-types';
import type {Location} from 'history';

export type ReportingSeverity = 'ignore' | 'log' | 'warn' | 'error' | 'throw';

Expand Down Expand Up @@ -448,3 +449,11 @@ export interface TOCItem {
}

export type RouteChunksTree = {[x: string | number]: string | RouteChunksTree};

export type ClientModule = {
onRouteUpdate?: (args: {
previousLocation: Location | null;
location: Location;
}) => void;
onRouteUpdateDelayed?: (args: {location: Location}) => void;
};
7 changes: 1 addition & 6 deletions packages/docusaurus/src/client/PendingNavigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,7 @@ class PendingNavigation extends React.Component<Props, State> {
});
// Route has loaded, we can reset previousLocation.
this.previousLocation = null;
this.setState(
{
nextRouteHasLoaded: true,
},
this.stopProgressBar,
);
this.setState({nextRouteHasLoaded: true}, this.stopProgressBar);
const {hash} = nextLocation;
if (!hash) {
window.scrollTo(0, 0);
Expand Down
28 changes: 12 additions & 16 deletions packages/docusaurus/src/client/client-lifecycles-dispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,28 @@
*/

import clientModules from '@generated/client-modules';
import type {ClientModule} from '@docusaurus/types';

interface Dispatchers {
onRouteUpdate: (...args: unknown[]) => void;
onRouteUpdateDelayed: (...args: unknown[]) => void;
}

function dispatchLifecycleAction(
lifecycleAction: keyof Dispatchers,
...args: unknown[]
function dispatchLifecycleAction<K extends keyof ClientModule>(
lifecycleAction: K,
args: Parameters<NonNullable<ClientModule[K]>>,
) {
clientModules.forEach((clientModule) => {
const lifecycleFunction =
clientModule?.default?.[lifecycleAction] ?? clientModule[lifecycleAction];
const lifecycleFunction = (clientModule?.default?.[lifecycleAction] ??
clientModule[lifecycleAction]) as
| ((...a: Parameters<NonNullable<ClientModule[K]>>) => void)
| undefined;

if (lifecycleFunction) {
lifecycleFunction(...args);
}
lifecycleFunction?.(...args);
});
}

const clientLifecyclesDispatchers: Dispatchers = {
const clientLifecyclesDispatchers: Required<ClientModule> = {
onRouteUpdate(...args) {
dispatchLifecycleAction('onRouteUpdate', ...args);
dispatchLifecycleAction('onRouteUpdate', args);
},
onRouteUpdateDelayed(...args) {
dispatchLifecycleAction('onRouteUpdateDelayed', ...args);
dispatchLifecycleAction('onRouteUpdateDelayed', args);
},
};

Expand Down