Skip to content

Commit

Permalink
wip: no longer process dispose search parameters
Browse files Browse the repository at this point in the history
we have ui state and widgets reading this ui state. This means that when you dispose a widget, you don't need to call that function if you start with a clean helper state instead of persisting the state.

This needs to be done in a major version, as people _might_ rely on state without ui state (i'm not sure if they do) and that would be removed if they dispose a widget.

This allows again for classic react-instantsearch-core style virtual widgets for the modal and dynamic widget use case
  • Loading branch information
Haroenv committed Sep 29, 2022
1 parent 21f3147 commit 87d1851
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/middlewares/createInsightsMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export const createInsightsMiddleware: CreateInsightsMiddleware = (props) => {
started() {
insightsClient('addAlgoliaAgent', 'insights-middleware');

helper = instantSearchInstance.helper!;
helper = instantSearchInstance.mainHelper!;

initialParameters = {
userToken: (helper.state as PlainSearchParameters).userToken,
Expand Down
31 changes: 17 additions & 14 deletions src/widgets/index/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,28 +347,28 @@ const index = (widgetParams: IndexWidgetParams): IndexWidget => {
);

if (localInstantSearchInstance && Boolean(widgets.length)) {
const nextState = widgets.reduce((state, widget) => {
// the `dispose` method exists at this point we already assert it
const next = widget.dispose!({
widgets.forEach((widget) => {
// doing nothing with the return value of dispose, this is handled by the next part
widget.dispose!({
helper: helper!,
state,
state: helper!.state,
parent: this,
});
});

return next || state;
}, helper!.state);
const newState = getLocalWidgetsSearchParameters(localWidgets, {
uiState: localUiState,
initialSearchParameters: new algoliasearchHelper.SearchParameters({
index: this.getIndexName(),
}),
});

localUiState = getLocalWidgetsUiState(localWidgets, {
searchParameters: nextState,
searchParameters: newState,
helper: helper!,
});

helper!.setState(
getLocalWidgetsSearchParameters(localWidgets, {
uiState: localUiState,
initialSearchParameters: nextState,
})
);
helper!.setState(newState);

if (localWidgets.length) {
localInstantSearchInstance.scheduleSearch();
Expand Down Expand Up @@ -450,7 +450,10 @@ const index = (widgetParams: IndexWidgetParams): IndexWidget => {
};

derivedHelper = mainHelper.derive(() =>
mergeSearchParameters(...resolveSearchParameters(this))
mergeSearchParameters(
mainHelper.state,
...resolveSearchParameters(this)
)
);

const indexInitialResults =
Expand Down

0 comments on commit 87d1851

Please sign in to comment.