Skip to content

Commit

Permalink
fix(index): allow dispose to be called before init
Browse files Browse the repository at this point in the history
fixes #6073
  • Loading branch information
Haroenv committed Apr 30, 2024
1 parent 68fe2ee commit c225ee4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3155,6 +3155,16 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/index-widge

expect(mainHelper.derivedHelpers).toHaveLength(0);
});

it('does not crash when calling `dispose` before `init`', () => {
const instance = index({ indexName: 'indexName' });

instance.addWidgets([virtualSearchBox({})]);

expect(() => {
instance.dispose(createDisposeOptions());
}).not.toThrow();
});
});

describe('getWidgetState', () => {
Expand Down
6 changes: 3 additions & 3 deletions packages/instantsearch.js/src/widgets/index/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -758,16 +758,16 @@ const index = (widgetParams: IndexWidgetParams): IndexWidget => {

dispose() {
localWidgets.forEach((widget) => {
if (widget.dispose) {
if (widget.dispose && helper) {
// The dispose function is always called once the instance is started
// (it's an effect of `removeWidgets`). The index is initialized and
// the Helper is available. We don't care about the return value of
// `dispose` because the index is removed. We can't call `removeWidgets`
// because we want to keep the widgets on the instance, to allow idempotent
// operations on `add` & `remove`.
widget.dispose({
helper: helper!,
state: helper!.state,
helper,
state: helper.state,
parent: this,
});
}
Expand Down

0 comments on commit c225ee4

Please sign in to comment.