Skip to content

Commit

Permalink
feat: identify router
Browse files Browse the repository at this point in the history
  • Loading branch information
Haroenv committed Feb 27, 2023
1 parent 25e1ef5 commit 81c934c
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/instantsearch.js/src/lib/routers/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const setWindowTitle = (title?: string): void => {
};

class BrowserHistory<TRouteState> implements Router<TRouteState> {
public $$type = 'ais.browser';
/**
* Transforms a UI state into a title for the page.
*/
Expand Down
2 changes: 2 additions & 0 deletions packages/instantsearch.js/src/lib/stateMappings/simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export default function simpleStateMapping<
TUiState extends UiState = UiState
>(): StateMapping<TUiState, TUiState> {
return {
$$type: 'ais.simple',

stateToRoute(uiState) {
return Object.keys(uiState).reduce(
(state, indexId) => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default function singleIndexStateMapping<
indexName: keyof TUiState
): StateMapping<TUiState, TUiState[typeof indexName]> {
return {
$$type: 'ais.singleIndex',
stateToRoute(uiState) {
return getIndexStateWithoutConfigure(uiState[indexName] || {});
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ export const createRouterMiddleware = <
const initialUiState = instantSearchInstance._initialUiState;

return {
$$type: 'ais.router',
$$type: `ais.router({router:${
router.$$type || '__unknown__'
}, stateMapping:${stateMapping.$$type || '__unknown__'}})`,
$$internal,
onStateChange({ uiState }) {
const routeState = stateMapping.stateToRoute(uiState);
Expand Down
10 changes: 10 additions & 0 deletions packages/instantsearch.js/src/types/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ export type Router<TRouteState = UiState> = {
* Called when InstantSearch is disposed. Used to remove subscriptions.
*/
dispose(): void;

/**
* Identifier for this router. Used to differentiate between routers.
*/
$$type?: string;
};

/**
Expand All @@ -57,4 +62,9 @@ export type StateMapping<TUiState = UiState, TRouteState = TUiState> = {
* The format is the output of `stateToRoute`.
*/
routeToState(routeState: TRouteState): TUiState;

/**
* Identifier for this stateMapping. Used to differentiate between stateMappings.
*/
$$type?: string;
};
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ export function createInstantSearchRouterNext<TRouteState = UiState>(
...routerOptions,
});
router._isNextRouter = true;
router.$$type = 'ais.nextjs';

return router;
}

0 comments on commit 81c934c

Please sign in to comment.