Skip to content

Commit ab1b872

Browse files
authored
fix(instantsearch): allow dispose before start (#5533)
We were using non-null assertions in `dispose` of InstantSearch and index widget as we were assuming start/init comes first, but that's not necessarily the case.
1 parent 9cfe037 commit ab1b872

File tree

4 files changed

+22
-3
lines changed

4 files changed

+22
-3
lines changed

.eslintrc.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,16 @@ const config = {
124124
// these sorts of errors anyway.
125125
// See: https://github.com/typescript-eslint/typescript-eslint/issues/342
126126
'no-undef': 'off',
127+
// This rule only supports ?. with the TypeScript parser.
128+
'no-unused-expressions': 'off',
129+
'@typescript-eslint/no-unused-expressions': [
130+
'error',
131+
{
132+
allowShortCircuit: true,
133+
allowTernary: true,
134+
allowTaggedTemplates: true,
135+
},
136+
],
127137
},
128138
},
129139
{

packages/instantsearch.js/src/lib/InstantSearch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,7 @@ See ${createDocumentationLink({
625625
// The helper needs to be reset to perform the next search from a fresh state.
626626
// If not reset, it would use the state stored before calling `dispose()`.
627627
this.removeAllListeners();
628-
this.mainHelper!.removeAllListeners();
628+
this.mainHelper?.removeAllListeners();
629629
this.mainHelper = null;
630630
this.helper = null;
631631

packages/instantsearch.js/src/lib/__tests__/InstantSearch-test.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,6 +1177,15 @@ describe('dispose', () => {
11771177
expect(search.mainHelper).not.toBe(null);
11781178
expect(search.helper).not.toBe(null);
11791179
});
1180+
1181+
it("doesn't throw without starting", () => {
1182+
const search = new InstantSearch({
1183+
indexName: 'indexName',
1184+
searchClient: createSearchClient(),
1185+
});
1186+
1187+
expect(() => search.dispose()).not.toThrow();
1188+
});
11801189
});
11811190

11821191
describe('scheduleSearch', () => {

packages/instantsearch.js/src/widgets/index/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -665,10 +665,10 @@ const index = (widgetParams: IndexWidgetParams): IndexWidget => {
665665

666666
localInstantSearchInstance = null;
667667
localParent = null;
668-
helper!.removeAllListeners();
668+
helper?.removeAllListeners();
669669
helper = null;
670670

671-
derivedHelper!.detach();
671+
derivedHelper?.detach();
672672
derivedHelper = null;
673673
},
674674

0 commit comments

Comments
 (0)