mirror of
https://github.com/facebook/docusaurus.git
synced 2025-08-06 10:20:09 +02:00
refactor: improve client modules types (#6742)
This commit is contained in:
parent
cfcc8b31f4
commit
ddad9713e6
4 changed files with 25 additions and 24 deletions
|
@ -6,8 +6,9 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
declare module '@generated/client-modules' {
|
declare module '@generated/client-modules' {
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
import type {ClientModule} from '@docusaurus/types';
|
||||||
const clientModules: readonly any[];
|
|
||||||
|
const clientModules: readonly (ClientModule & {default: ClientModule})[];
|
||||||
export default clientModules;
|
export default clientModules;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
9
packages/docusaurus-types/src/index.d.ts
vendored
9
packages/docusaurus-types/src/index.d.ts
vendored
|
@ -11,6 +11,7 @@ import type {CommanderStatic} from 'commander';
|
||||||
import type {ParsedUrlQueryInput} from 'querystring';
|
import type {ParsedUrlQueryInput} from 'querystring';
|
||||||
import type Joi from 'joi';
|
import type Joi from 'joi';
|
||||||
import type {Overwrite, DeepPartial} from 'utility-types';
|
import type {Overwrite, DeepPartial} from 'utility-types';
|
||||||
|
import type {Location} from 'history';
|
||||||
|
|
||||||
export type ReportingSeverity = 'ignore' | 'log' | 'warn' | 'error' | 'throw';
|
export type ReportingSeverity = 'ignore' | 'log' | 'warn' | 'error' | 'throw';
|
||||||
|
|
||||||
|
@ -448,3 +449,11 @@ export interface TOCItem {
|
||||||
}
|
}
|
||||||
|
|
||||||
export type RouteChunksTree = {[x: string | number]: string | RouteChunksTree};
|
export type RouteChunksTree = {[x: string | number]: string | RouteChunksTree};
|
||||||
|
|
||||||
|
export type ClientModule = {
|
||||||
|
onRouteUpdate?: (args: {
|
||||||
|
previousLocation: Location | null;
|
||||||
|
location: Location;
|
||||||
|
}) => void;
|
||||||
|
onRouteUpdateDelayed?: (args: {location: Location}) => void;
|
||||||
|
};
|
||||||
|
|
|
@ -69,12 +69,7 @@ class PendingNavigation extends React.Component<Props, State> {
|
||||||
});
|
});
|
||||||
// Route has loaded, we can reset previousLocation.
|
// Route has loaded, we can reset previousLocation.
|
||||||
this.previousLocation = null;
|
this.previousLocation = null;
|
||||||
this.setState(
|
this.setState({nextRouteHasLoaded: true}, this.stopProgressBar);
|
||||||
{
|
|
||||||
nextRouteHasLoaded: true,
|
|
||||||
},
|
|
||||||
this.stopProgressBar,
|
|
||||||
);
|
|
||||||
const {hash} = nextLocation;
|
const {hash} = nextLocation;
|
||||||
if (!hash) {
|
if (!hash) {
|
||||||
window.scrollTo(0, 0);
|
window.scrollTo(0, 0);
|
||||||
|
|
|
@ -6,32 +6,28 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import clientModules from '@generated/client-modules';
|
import clientModules from '@generated/client-modules';
|
||||||
|
import type {ClientModule} from '@docusaurus/types';
|
||||||
|
|
||||||
interface Dispatchers {
|
function dispatchLifecycleAction<K extends keyof ClientModule>(
|
||||||
onRouteUpdate: (...args: unknown[]) => void;
|
lifecycleAction: K,
|
||||||
onRouteUpdateDelayed: (...args: unknown[]) => void;
|
args: Parameters<NonNullable<ClientModule[K]>>,
|
||||||
}
|
|
||||||
|
|
||||||
function dispatchLifecycleAction(
|
|
||||||
lifecycleAction: keyof Dispatchers,
|
|
||||||
...args: unknown[]
|
|
||||||
) {
|
) {
|
||||||
clientModules.forEach((clientModule) => {
|
clientModules.forEach((clientModule) => {
|
||||||
const lifecycleFunction =
|
const lifecycleFunction = (clientModule?.default?.[lifecycleAction] ??
|
||||||
clientModule?.default?.[lifecycleAction] ?? clientModule[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) {
|
onRouteUpdate(...args) {
|
||||||
dispatchLifecycleAction('onRouteUpdate', ...args);
|
dispatchLifecycleAction('onRouteUpdate', args);
|
||||||
},
|
},
|
||||||
onRouteUpdateDelayed(...args) {
|
onRouteUpdateDelayed(...args) {
|
||||||
dispatchLifecycleAction('onRouteUpdateDelayed', ...args);
|
dispatchLifecycleAction('onRouteUpdateDelayed', args);
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue