diff --git a/packages/instantsearch-core/src/__tests__/instantsearch.test.tsx b/packages/instantsearch-core/src/__tests__/instantsearch.test.tsx index 387bd3e6ea..b5a299092b 100644 --- a/packages/instantsearch-core/src/__tests__/instantsearch.test.tsx +++ b/packages/instantsearch-core/src/__tests__/instantsearch.test.tsx @@ -378,7 +378,7 @@ search.addWidgets([ See https://www.algolia.com/doc/api-reference/widgets/configure/js/`); }); - it("exposes helper's last results", async () => { + it("helper's results are null", async () => { const searchClient = createSearchClient(); const search = new InstantSearch({ @@ -394,8 +394,7 @@ See https://www.algolia.com/doc/api-reference/widgets/configure/js/`); await wait(0); - // could be null if we don't pretend the main helper is the one who searched - expect(search.helper!.lastResults).not.toBe(null); + expect(search.helper!.lastResults).toBe(null); }); describe('insights middleware', () => { @@ -822,9 +821,9 @@ describe('start', () => { await wait(0); - expect( - search.mainHelper!.searchOnlyWithDerivedHelpers - ).toHaveBeenCalledTimes(1); + expect(search.helper!.searchOnlyWithDerivedHelpers).toHaveBeenCalledTimes( + 1 + ); }); it('forwards the `initialUiState` to the main index', () => { @@ -1004,7 +1003,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/instantsear `); }); - it('keeps a mainHelper already set on the instance (Vue SSR)', () => { + it('keeps a helper already set on the instance (Vue SSR)', () => { const searchClient = createSearchClient(); const instance = new InstantSearch({ indexName: 'indexName', @@ -1013,17 +1012,17 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/instantsear const helper = algoliasearchHelper(searchClient, ''); - // explicitly setting the mainHelper before start is used to force render to + // explicitly setting the helper before start is used to force render to // happen before the results of the first search are done. We need to make // sure no extra helper is created, as that can cause certain things (like routing) // to be listening to the wrong helper. - instance.mainHelper = helper; + instance.helper = helper; - expect(instance.mainHelper).toBe(helper); + expect(instance.helper).toBe(helper); instance.start(); - expect(instance.mainHelper).toBe(helper); + expect(instance.helper).toBe(helper); }); it('no query for root if indexName is not given', async () => { @@ -1122,7 +1121,7 @@ describe('dispose', () => { await wait(0); // Simulate a search - search.mainHelper!.search(); + search.helper!.search(); search.dispose(); @@ -1195,17 +1194,17 @@ describe('dispose', () => { search.start(); - const mainHelper = search.mainHelper!; + const helper = search.helper!; - mainHelper.on('searchQueueEmpty', onEventName); + helper.on('searchQueueEmpty', onEventName); - mainHelper.emit('searchQueueEmpty'); + helper.emit('searchQueueEmpty'); expect(onEventName).toHaveBeenCalledTimes(1); search.dispose(); - mainHelper.emit('searchQueueEmpty'); + helper.emit('searchQueueEmpty'); expect(onEventName).toHaveBeenCalledTimes(1); }); @@ -1240,7 +1239,7 @@ describe('dispose', () => { expect(onRender).toHaveBeenCalledTimes(2); }); - it('removes the Helpers references', () => { + it('removes the helper references', () => { const search = new InstantSearch({ indexName: 'indexName', searchClient: createSearchClient(), @@ -1248,17 +1247,14 @@ describe('dispose', () => { search.start(); - expect(search.mainHelper).not.toBe(null); expect(search.helper).not.toBe(null); search.dispose(); - expect(search.mainHelper).toBe(null); expect(search.helper).toBe(null); search.start(); - expect(search.mainHelper).not.toBe(null); expect(search.helper).not.toBe(null); }); @@ -1283,15 +1279,15 @@ describe('scheduleSearch', () => { search.start(); - const mainHelperSearch = jest.spyOn(search.mainHelper!, 'search'); + const helperSearch = jest.spyOn(search.helper!, 'search'); search.scheduleSearch(); - expect(mainHelperSearch).toHaveBeenCalledTimes(0); + expect(helperSearch).toHaveBeenCalledTimes(0); await wait(0); - expect(mainHelperSearch).toHaveBeenCalledTimes(1); + expect(helperSearch).toHaveBeenCalledTimes(1); }); it('deduplicates the calls to the `search` method', async () => { @@ -1304,18 +1300,18 @@ describe('scheduleSearch', () => { search.start(); - const mainHelperSearch = jest.spyOn(search.mainHelper!, 'search'); + const helperSearch = jest.spyOn(search.helper!, 'search'); search.scheduleSearch(); search.scheduleSearch(); search.scheduleSearch(); search.scheduleSearch(); - expect(mainHelperSearch).toHaveBeenCalledTimes(0); + expect(helperSearch).toHaveBeenCalledTimes(0); await wait(0); - expect(mainHelperSearch).toHaveBeenCalledTimes(1); + expect(helperSearch).toHaveBeenCalledTimes(1); }); }); @@ -1418,7 +1414,7 @@ describe('scheduleStalledRender', () => { castToJestMock(widget.render!).mockReset(); // Trigger a new search - search.mainHelper!.search(); + search.helper!.search(); // search starts await wait(0); @@ -1456,10 +1452,10 @@ describe('scheduleStalledRender', () => { castToJestMock(widget.render!).mockClear(); // Trigger multiple searches - search.mainHelper!.search(); - search.mainHelper!.search(); - search.mainHelper!.search(); - search.mainHelper!.search(); + search.helper!.search(); + search.helper!.search(); + search.helper!.search(); + search.helper!.search(); await wait(0); @@ -1507,7 +1503,7 @@ describe('scheduleStalledRender', () => { ); // Trigger a new search - search.mainHelper!.search(); + search.helper!.search(); expect(widget.render).toHaveBeenCalledTimes(1); castToJestMock(widget.render!).mockClear(); diff --git a/packages/instantsearch-core/src/connectors/__tests__/connectGeoSearch.test.ts b/packages/instantsearch-core/src/connectors/__tests__/connectGeoSearch.test.ts index 6a41ad6cb9..711356b85c 100644 --- a/packages/instantsearch-core/src/connectors/__tests__/connectGeoSearch.test.ts +++ b/packages/instantsearch-core/src/connectors/__tests__/connectGeoSearch.test.ts @@ -73,7 +73,7 @@ describe('connectGeoSearch', () => { const widget = customGeoSearch({}); const instantSearchInstance = createInstantSearch(); - const helper = instantSearchInstance.mainHelper!; + const helper = instantSearchInstance.helper!; widget.init!( createInitOptions({ @@ -160,7 +160,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/geo-search/ const widget = customGeoSearch({}); const instantSearchInstance = createInstantSearch(); - const { mainHelper: helper } = instantSearchInstance; + const { helper } = instantSearchInstance; widget.init!(createInitOptions({ instantSearchInstance })); diff --git a/packages/instantsearch-core/src/instantsearch.ts b/packages/instantsearch-core/src/instantsearch.ts index b9a93dee37..d13cee3892 100644 --- a/packages/instantsearch-core/src/instantsearch.ts +++ b/packages/instantsearch-core/src/instantsearch.ts @@ -65,7 +65,6 @@ export class InstantSearch< onStateChange: InstantSearchOptions['onStateChange'] | null = null; future: NonNullable['future']>; helper: AlgoliaSearchHelper | null; - mainHelper: AlgoliaSearchHelper | null; mainIndex: IndexWidget; started: boolean; renderState: RenderState = {}; @@ -74,7 +73,7 @@ export class InstantSearch< _initialUiState: TUiState; _initialResults: InitialResults | null; _createURL: CreateURL; - _mainHelperSearch?: AlgoliaSearchHelper['search']; + _helperSearch?: AlgoliaSearchHelper['search']; _hasSearchWidget: boolean = false; _hasRecommendWidget: boolean = false; _insights: InstantSearchOptions['insights']; @@ -150,7 +149,6 @@ See ${createDocumentationLink({ this.future = future; this.indexName = indexName; this.helper = null; - this.mainHelper = null; this.mainIndex = index({ indexName, }); @@ -296,11 +294,11 @@ See ${createDocumentationLink({ // DerivedHelper scoped into the `index` widgets. // In Vue InstantSearch' hydrate, a main helper gets set before start, so // we need to respect this helper as a way to keep all listeners correct. - const mainHelper = - this.mainHelper || + const helper = + this.helper || algoliasearchHelper(this.client, this.indexName, undefined); - mainHelper.search = () => { + helper.search = () => { this.status = 'loading'; this.scheduleRender(false); @@ -315,19 +313,19 @@ See ${createDocumentationLink({ // completely transparent for the rest of the codebase. Only this module // is impacted. if (this._hasSearchWidget) { - mainHelper.searchOnlyWithDerivedHelpers(); + helper.searchOnlyWithDerivedHelpers(); } if (this._hasRecommendWidget) { - mainHelper.recommend(); + helper.recommend(); } - return mainHelper; + return helper; }; // Only the "main" Helper emits the `error` event vs the one for `search` // and `results` that are also emitted on the derived one. - mainHelper.on('error', (error) => { + helper.on('error', (error) => { if (!(error instanceof Error)) { // typescript lies here, error is in some cases { name: string, message: string } const err = error as Record; @@ -345,7 +343,7 @@ See ${createDocumentationLink({ this.emit('error', this.error); }); - this.mainHelper = mainHelper; + this.helper = helper; this.middleware.forEach(({ instance }) => { instance.subscribe(); @@ -359,7 +357,7 @@ See ${createDocumentationLink({ if (this._initialResults) { hydrateSearchClient(this.client, this._initialResults); - hydrateRecommendCache(this.mainHelper, this._initialResults); + hydrateRecommendCache(this.helper, this._initialResults); const originalScheduleSearch = this.scheduleSearch; // We don't schedule a first search when initial results are provided @@ -387,10 +385,6 @@ See ${createDocumentationLink({ this.scheduleSearch(); } - // Keep the previous reference for legacy purpose, some pattern use - // the direct Helper access `search.helper` (e.g multi-index). - this.helper = this.mainIndex.getHelper(); - // track we started the search if we add more widgets, // to init them directly after add this.started = true; @@ -403,7 +397,7 @@ See ${createDocumentationLink({ // added when `insights` is unset and the initial results possess `queryID`. // Any user-provided middleware will be added later and override this one. if (typeof this._insights === 'undefined') { - mainHelper.derivedHelpers[0].once('result', () => { + helper.derivedHelpers[0].once('result', () => { const hasAutomaticInsights = this.mainIndex .getScopedResults() .some(({ results }) => results?._automaticInsights); @@ -435,8 +429,7 @@ See ${createDocumentationLink({ // The helper needs to be reset to perform the next search from a fresh state. // If not reset, it would use the state stored before calling `dispose()`. this.removeAllListeners(); - this.mainHelper?.removeAllListeners(); - this.mainHelper = null; + this.helper?.removeAllListeners(); this.helper = null; this.middleware.forEach(({ instance }) => { @@ -446,12 +439,12 @@ See ${createDocumentationLink({ scheduleSearch = defer(() => { if (this.started) { - this.mainHelper!.search(); + this.helper!.search(); } }); scheduleRender = defer((shouldResetStatus: boolean = true) => { - if (!this.mainHelper?.hasPendingRequests()) { + if (!this.helper?.hasPendingRequests()) { clearTimeout(this._searchStalledTimer); this._searchStalledTimer = null; @@ -481,7 +474,7 @@ See ${createDocumentationLink({ uiState: TUiState | ((previousUiState: TUiState) => TUiState), callOnStateChange = true ) { - if (!this.mainHelper) { + if (!this.helper) { throw new Error( withUsage('The `start` method needs to be called before `setUiState`.') ); @@ -548,13 +541,13 @@ See ${createDocumentationLink({ } refresh() { - if (!this.mainHelper) { + if (!this.helper) { throw new Error( withUsage('The `start` method needs to be called before `refresh`.') ); } - this.mainHelper.clearCache().search(); + this.helper.clearCache().search(); } } diff --git a/packages/instantsearch-core/src/middlewares/__tests__/createInsightsMiddleware.ts b/packages/instantsearch-core/src/middlewares/__tests__/createInsightsMiddleware.ts index fd15e44f51..b335c21d23 100644 --- a/packages/instantsearch-core/src/middlewares/__tests__/createInsightsMiddleware.ts +++ b/packages/instantsearch-core/src/middlewares/__tests__/createInsightsMiddleware.ts @@ -72,8 +72,7 @@ describe('insights', () => { } const getUserToken = () => - (instantSearchInstance.mainHelper!.state as PlainSearchParameters) - .userToken; + (instantSearchInstance.helper!.state as PlainSearchParameters).userToken; return { analytics, @@ -103,11 +102,10 @@ describe('insights', () => { }); instantSearchInstance.start(); - const helper = instantSearchInstance.mainHelper!; + const helper = instantSearchInstance.helper!; const getUserToken = () => - (instantSearchInstance.mainHelper!.state as PlainSearchParameters) - .userToken; + (instantSearchInstance.helper!.state as PlainSearchParameters).userToken; return { analytics, @@ -452,7 +450,7 @@ describe('insights', () => { insightsClient, }) ); - expect(instantSearchInstance.mainHelper!.state.clickAnalytics).toBe(true); + expect(instantSearchInstance.helper!.state.clickAnalytics).toBe(true); }); it('applies clickAnalytics if $$automatic: false', () => { @@ -463,7 +461,7 @@ describe('insights', () => { $$automatic: false, }) ); - expect(instantSearchInstance.mainHelper!.state.clickAnalytics).toBe(true); + expect(instantSearchInstance.helper!.state.clickAnalytics).toBe(true); }); it('does not apply clickAnalytics if $$automatic: true', () => { @@ -484,9 +482,9 @@ describe('insights', () => { const middleware = createInsightsMiddleware({ insightsClient, })({ instantSearchInstance }); - instantSearchInstance.mainHelper!.setPage(100); + instantSearchInstance.helper!.setPage(100); middleware.subscribe(); - expect(instantSearchInstance.mainHelper!.state.page).toBe(100); + expect(instantSearchInstance.helper!.state.page).toBe(100); }); it('adds user agent', () => { @@ -679,7 +677,7 @@ describe('insights', () => { insightsClient('setUserToken', 'abc'); instantSearchInstance.start(); - instantSearchInstance.mainHelper!.setPage(100); + instantSearchInstance.helper!.setPage(100); instantSearchInstance.use( createInsightsMiddleware({ @@ -687,7 +685,7 @@ describe('insights', () => { }) ); - expect(instantSearchInstance.mainHelper!.state.page).toBe(100); + expect(instantSearchInstance.helper!.state.page).toBe(100); expect(getUserToken()).toEqual('abc'); }); @@ -711,7 +709,7 @@ describe('insights', () => { createTestEnvironment({ started: false }); instantSearchInstance.start(); - instantSearchInstance.mainHelper!.setPage(100); + instantSearchInstance.helper!.setPage(100); instantSearchInstance.use( createInsightsMiddleware({ @@ -723,7 +721,7 @@ describe('insights', () => { await wait(0); - expect(instantSearchInstance.mainHelper!.state.page).toEqual(100); + expect(instantSearchInstance.helper!.state.page).toEqual(100); expect(getUserToken()).toEqual('def'); }); @@ -1376,7 +1374,7 @@ describe('insights', () => { }, }); - instantSearchInstance.mainHelper!.derivedHelpers[0].emit('result', { + instantSearchInstance.helper!.derivedHelpers[0].emit('result', { results: { queryId: '2' }, }); @@ -1567,7 +1565,7 @@ describe('insights', () => { instantSearchInstance .helper!.setState({ - ...instantSearchInstance.mainHelper!.state, + ...instantSearchInstance.helper!.state, query: 'test', }) .search(); diff --git a/packages/instantsearch-core/src/middlewares/createInsightsMiddleware.ts b/packages/instantsearch-core/src/middlewares/createInsightsMiddleware.ts index 1503037151..61f7ec9cc5 100644 --- a/packages/instantsearch-core/src/middlewares/createInsightsMiddleware.ts +++ b/packages/instantsearch-core/src/middlewares/createInsightsMiddleware.ts @@ -170,7 +170,7 @@ export function createInsightsMiddleware< started() { insightsClient('addAlgoliaAgent', 'insights-middleware'); - helper = instantSearchInstance.mainHelper!; + helper = instantSearchInstance.helper!; const { queue: queueAtStart } = insightsClient; @@ -324,7 +324,7 @@ export function createInsightsMiddleware< const viewedObjectIDs = new Set(); let lastQueryId: string | undefined; - instantSearchInstance.mainHelper!.derivedHelpers[0].on( + instantSearchInstance.helper!.derivedHelpers[0].on( 'result', ({ results }) => { if ( @@ -416,7 +416,7 @@ function getInitialParameters( instantSearchInstance._initialResults?.[instantSearchInstance.indexName] ?.state || {}; - const stateFromHelper = instantSearchInstance.mainHelper!.state; + const stateFromHelper = instantSearchInstance.helper!.state; return { userToken: stateFromInitialResults.userToken || stateFromHelper.userToken, diff --git a/packages/instantsearch-core/src/server.ts b/packages/instantsearch-core/src/server.ts index 6b6999e90d..0910936332 100644 --- a/packages/instantsearch-core/src/server.ts +++ b/packages/instantsearch-core/src/server.ts @@ -11,7 +11,7 @@ export function waitForResults( search: InstantSearch, skipRecommend: boolean = false ): Promise { - const helper = search.mainHelper!; + const helper = search.helper!; // Extract search parameters from the search client to use them // later during hydration. diff --git a/packages/instantsearch-core/src/widgets/__tests__/index-widget.test.ts b/packages/instantsearch-core/src/widgets/__tests__/index-widget.test.ts index c600238853..6647181d23 100644 --- a/packages/instantsearch-core/src/widgets/__tests__/index-widget.test.ts +++ b/packages/instantsearch-core/src/widgets/__tests__/index-widget.test.ts @@ -1042,12 +1042,12 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/index-widge describe('init', () => { it('forwards the `search` call to the main instance', () => { const instance = index({ indexName: 'indexName' }); - const mainHelper = algoliasearchHelper({} as any, '', {}); + const helper = algoliasearchHelper({} as any, '', {}); const instantSearchInstance = createInstantSearch({ - mainHelper, + helper, }); - const search = jest.spyOn(mainHelper, 'search').mockImplementation(); + const search = jest.spyOn(helper, 'search').mockImplementation(); instance.init( createIndexInitOptions({ @@ -1064,13 +1064,13 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/index-widge it('forwards the `searchForFacetValues` call to the main instance', () => { const instance = index({ indexName: 'indexName' }); - const mainHelper = algoliasearchHelper({} as any, '', {}); + const helper = algoliasearchHelper({} as any, '', {}); const instantSearchInstance = createInstantSearch({ - mainHelper, + helper, }); const searchForFacetValues = jest - .spyOn(mainHelper, 'searchForFacetValues') + .spyOn(helper, 'searchForFacetValues') .mockImplementation(); instance.init( @@ -1102,9 +1102,9 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/index-widge it('uses the internal state for the queries', () => { const instance = index({ indexName: 'indexName' }); const searchClient = createSearchClient(); - const mainHelper = algoliasearchHelper(searchClient, '', {}); + const helper = algoliasearchHelper(searchClient, '', {}); const instantSearchInstance = createInstantSearch({ - mainHelper, + helper, }); instance.addWidgets([ @@ -1177,9 +1177,9 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/index-widge it('uses the index set by the widget for the queries', () => { const instance = index({ indexName: 'indexName' }); const searchClient = createSearchClient(); - const mainHelper = algoliasearchHelper(searchClient, '', {}); + const helper = algoliasearchHelper(searchClient, '', {}); const instantSearchInstance = createInstantSearch({ - mainHelper, + helper, }); instance.addWidgets([ @@ -1215,9 +1215,9 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/index-widge const level1 = index({ indexName: 'level1IndexName' }); const level2 = index({ indexName: 'level2IndexName' }); const searchClient = createSearchClient(); - const mainHelper = algoliasearchHelper(searchClient, '', {}); + const helper = algoliasearchHelper(searchClient, '', {}); const instantSearchInstance = createInstantSearch({ - mainHelper, + helper, }); level0.addWidgets([ @@ -1308,9 +1308,9 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/index-widge it('uses the internal state for the SFFV queries', () => { const instance = index({ indexName: 'indexName' }); const searchClient = createSearchClient(); - const mainHelper = algoliasearchHelper(searchClient, '', {}); + const helper = algoliasearchHelper(searchClient, '', {}); const instantSearchInstance = createInstantSearch({ - mainHelper, + helper, }); instance.addWidgets([ @@ -1447,9 +1447,9 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/index-widge const level2 = index({ indexName: 'level2IndexName' }); const level3 = index({ indexName: 'level3IndexName' }); const searchClient = createSearchClient(); - const mainHelper = algoliasearchHelper(searchClient, '', {}); + const helper = algoliasearchHelper(searchClient, '', {}); const instantSearchInstance = createInstantSearch({ - mainHelper, + helper, }); level0.addWidgets([ @@ -1658,9 +1658,9 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/index-widge const level2 = index({ indexName: 'level2IndexName' }); const level3 = index({ indexName: 'level3IndexName' }); const searchClient = createSearchClient(); - const mainHelper = algoliasearchHelper(searchClient, '', {}); + const helper = algoliasearchHelper(searchClient, '', {}); const instantSearchInstance = createInstantSearch({ - mainHelper, + helper, }); level0.addWidgets([ @@ -1785,9 +1785,9 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/index-widge const level2 = index({ indexName: 'level2IndexName' }); const level3 = index({ indexName: 'level3IndexName' }); const searchClient = createSearchClient(); - const mainHelper = algoliasearchHelper(searchClient, '', {}); + const helper = algoliasearchHelper(searchClient, '', {}); const instantSearchInstance = createInstantSearch({ - mainHelper, + helper, }); level0.addWidgets([ @@ -2269,9 +2269,9 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/index-widge const level0 = index({ indexName: 'level0IndexName' }); const searchClient = createSearchClient(); - const mainHelper = algoliasearchHelper(searchClient, '', {}); + const helper = algoliasearchHelper(searchClient, '', {}); const instantSearchInstance = createInstantSearch({ - mainHelper, + helper, _initialUiState: { level0IndexName: { query: 'something', @@ -2441,15 +2441,15 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/index-widge test('calls `getRenderState` with the index render state', () => { const searchIndex = index({ indexName: 'indexName' }); const searchClient = createSearchClient(); - const mainHelper = algoliasearchHelper(searchClient, 'indexName', {}); - const instantSearchInstance = createInstantSearch({ mainHelper }); + const helper = algoliasearchHelper(searchClient, 'indexName', {}); + const instantSearchInstance = createInstantSearch({ helper }); const searchBox = createSearchBox({ dependsOn: 'search', - getRenderState: jest.fn((renderState, { helper }) => { + getRenderState: jest.fn((renderState, { helper: { state } }) => { return { ...renderState, searchBox: { - query: helper.state.query || '', + query: state.query || '', refine: () => {}, clear: () => {}, widgetParams: {}, @@ -2554,19 +2554,19 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/index-widge it('derives only once', () => { const instance = index({ indexName: 'test' }); - const mainHelper = algoliasearchHelper(createSearchClient(), ''); + const helper = algoliasearchHelper(createSearchClient(), ''); - const instantSearchInstance = createInstantSearch({ mainHelper }); + const instantSearchInstance = createInstantSearch({ helper }); expect(instance.getHelper()).toBe(null); instance.init(createIndexInitOptions({ instantSearchInstance })); - expect(mainHelper.derivedHelpers.length).toBe(1); + expect(helper.derivedHelpers.length).toBe(1); instance.init(createIndexInitOptions({ instantSearchInstance })); - expect(mainHelper.derivedHelpers.length).toBe(1); + expect(helper.derivedHelpers.length).toBe(1); }); }); }); @@ -2892,9 +2892,9 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/index-widge }) ), }); - const mainHelper = algoliasearchHelper(searchClient, '', {}); + const helper = algoliasearchHelper(searchClient, '', {}); const instantSearchInstance = createInstantSearch({ - mainHelper, + helper, }); const fbt = createFrequentlyBoughtTogether({ @@ -2910,9 +2910,9 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/index-widge parent: null, }) ); - mainHelper.search(); + helper.search(); await wait(0); - mainHelper.recommend(); + helper.recommend(); await wait(0); instance.render({ @@ -3066,9 +3066,9 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/index-widge it('removes the DerivedHelper', () => { const instance = index({ indexName: 'indexName' }); const searchClient = createSearchClient(); - const mainHelper = algoliasearchHelper(searchClient, '', {}); + const helper = algoliasearchHelper(searchClient, '', {}); const instantSearchInstance = createInstantSearch({ - mainHelper, + helper, }); instance.init( @@ -3078,11 +3078,11 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/index-widge }) ); - expect(mainHelper.derivedHelpers).toHaveLength(1); + expect(helper.derivedHelpers).toHaveLength(1); instance.dispose(createDisposeOptions()); - expect(mainHelper.derivedHelpers).toHaveLength(0); + expect(helper.derivedHelpers).toHaveLength(0); }); it('does not crash when calling `dispose` before `init`', () => { diff --git a/packages/instantsearch-core/src/widgets/index-widget.ts b/packages/instantsearch-core/src/widgets/index-widget.ts index 719a12c620..b9bba96d73 100644 --- a/packages/instantsearch-core/src/widgets/index-widget.ts +++ b/packages/instantsearch-core/src/widgets/index-widget.ts @@ -485,10 +485,10 @@ export const index = (widgetParams: IndexWidgetParams): IndexWidget => { localParent = parent; localUiState = uiState[indexId] || {}; - // The `mainHelper` is already defined at this point. The instance is created + // The helper is already defined at this point. The instance is created // inside InstantSearch at the `start` method, which occurs before the `init` // step. - const mainHelper = instantSearchInstance.mainHelper!; + const searchHelper = instantSearchInstance.helper!; const parameters = getLocalWidgetsSearchParameters(localWidgets, { uiState: localUiState, initialSearchParameters: new algoliasearchHelper.SearchParameters({ @@ -527,14 +527,14 @@ export const index = (widgetParams: IndexWidgetParams): IndexWidget => { // We don't trigger a search when controlled because it becomes the // responsibility of `setUiState`. - return mainHelper; + return searchHelper; } - return mainHelper.search(); + return searchHelper.search(); }; helper.searchWithoutTriggeringOnStateChange = () => { - return mainHelper.search(); + return searchHelper.search(); }; // We use the same pattern for the `searchForFacetValues`. @@ -546,7 +546,7 @@ export const index = (widgetParams: IndexWidgetParams): IndexWidget => { ) => { const state = helper!.state.setQueryParameters(userState); - return mainHelper.searchForFacetValues( + return searchHelper.searchForFacetValues( facetName, facetValue, maxFacetHits, @@ -554,10 +554,10 @@ export const index = (widgetParams: IndexWidgetParams): IndexWidget => { ); }; - derivedHelper = mainHelper.derive( + derivedHelper = searchHelper.derive( () => mergeSearchParameters( - mainHelper.state, + searchHelper.state, ...resolveSearchParameters(this) ), () => this.getHelper()!.recommendState @@ -710,7 +710,7 @@ export const index = (widgetParams: IndexWidgetParams): IndexWidget => { // then would no longer be thrown for global handlers. if ( instantSearchInstance.status === 'error' && - !instantSearchInstance.mainHelper!.hasPendingRequests() && + !instantSearchInstance.helper!.hasPendingRequests() && lastValidSearchParameters ) { helper!.setState(lastValidSearchParameters); diff --git a/packages/instantsearch-core/test/createInstantSearch.ts b/packages/instantsearch-core/test/createInstantSearch.ts index 09ef3a1bab..db2d63d115 100644 --- a/packages/instantsearch-core/test/createInstantSearch.ts +++ b/packages/instantsearch-core/test/createInstantSearch.ts @@ -9,13 +9,13 @@ export const createInstantSearch = ( args: Partial = {} ): InstantSearch => { const { indexName = 'indexName', client = createSearchClient() } = args; - const mainHelper = algoliasearchHelper(client, indexName, {}); + const helper = algoliasearchHelper(client, indexName, {}); const mainIndex = index({ indexName }); return { indexName, mainIndex, - mainHelper, + helper, client, started: false, status: 'idle', @@ -27,7 +27,6 @@ export const createInstantSearch = ( this.started = false; }, refresh: jest.fn(), - helper: mainHelper, // @TODO: use the Helper from the index once the RoutingManger uses the index middleware: [], renderState: {}, scheduleStalledRender: defer(jest.fn()), diff --git a/packages/instantsearch-core/test/createWidget.ts b/packages/instantsearch-core/test/createWidget.ts index 7f9b57c267..a35b361aab 100644 --- a/packages/instantsearch-core/test/createWidget.ts +++ b/packages/instantsearch-core/test/createWidget.ts @@ -55,7 +55,7 @@ export const createRenderOptions = ( const response = createMultiSearchResponse(); const helper = args.helper || instantSearchInstance.helper!; const results = new algoliasearchHelper.SearchResults( - instantSearchInstance.helper!.state, + helper.state, response.results as Array> ); diff --git a/packages/instantsearch.js/src/widgets/dynamic-widgets/__tests__/dynamic-widgets-test.ts b/packages/instantsearch.js/src/widgets/dynamic-widgets/__tests__/dynamic-widgets-test.ts index 3600a5f4e7..9867eb1890 100644 --- a/packages/instantsearch.js/src/widgets/dynamic-widgets/__tests__/dynamic-widgets-test.ts +++ b/packages/instantsearch.js/src/widgets/dynamic-widgets/__tests__/dynamic-widgets-test.ts @@ -185,7 +185,7 @@ describe('dynamicWidgets()', () => { indexWidget.init(createInitOptions({ instantSearchInstance })); // set results to the relevant index, so it renders all children - instantSearchInstance.mainHelper!.derivedHelpers[0].lastResults = + instantSearchInstance.helper!.derivedHelpers[0].lastResults = new SearchResults( indexWidget.getWidgetSearchParameters(new SearchParameters(), { uiState: {}, @@ -450,7 +450,7 @@ describe('dynamicWidgets()', () => { indexWidget.init(createInitOptions({ instantSearchInstance })); // set results to the relevant index, so it renders all children - instantSearchInstance.mainHelper!.derivedHelpers[0].lastResults = + instantSearchInstance.helper!.derivedHelpers[0].lastResults = new SearchResults( indexWidget.getWidgetSearchParameters(new SearchParameters(), { uiState: {}, diff --git a/packages/instantsearch.js/src/widgets/hits/__tests__/hits.test.tsx b/packages/instantsearch.js/src/widgets/hits/__tests__/hits.test.tsx index 391bfafc35..59a62c265b 100644 --- a/packages/instantsearch.js/src/widgets/hits/__tests__/hits.test.tsx +++ b/packages/instantsearch.js/src/widgets/hits/__tests__/hits.test.tsx @@ -203,7 +203,6 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/hits/js/" search.start(); // prevent warning from insights view event because insightsClient isn't yet loaded - // @ts-ignore search.helper!.state.userToken = 'userToken'; await wait(0); diff --git a/packages/instantsearch.js/src/widgets/infinite-hits/__tests__/infinite-hits.test.tsx b/packages/instantsearch.js/src/widgets/infinite-hits/__tests__/infinite-hits.test.tsx index 22829b8ccb..b0d0a5d44b 100644 --- a/packages/instantsearch.js/src/widgets/infinite-hits/__tests__/infinite-hits.test.tsx +++ b/packages/instantsearch.js/src/widgets/infinite-hits/__tests__/infinite-hits.test.tsx @@ -190,7 +190,6 @@ describe('infiniteHits', () => { search.start(); // prevent warning from insights view event because insightsClient isn't yet loaded - // @ts-ignore search.helper!.state.userToken = 'userToken'; await wait(0); diff --git a/packages/instantsearch.js/src/widgets/menu-select/__tests__/menu-select-test.ts b/packages/instantsearch.js/src/widgets/menu-select/__tests__/menu-select-test.ts index 56552c8c8f..9d9769126c 100644 --- a/packages/instantsearch.js/src/widgets/menu-select/__tests__/menu-select-test.ts +++ b/packages/instantsearch.js/src/widgets/menu-select/__tests__/menu-select-test.ts @@ -215,7 +215,7 @@ describe('menuSelect', () => { await wait(0); - expect(search.helper!.state).toEqual( + expect(search.mainIndex.getHelper()!.state).toEqual( new SearchParameters({ index: 'test', hierarchicalFacets: [{ attributes: ['test'], name: 'test' }], diff --git a/packages/react-instantsearch-core/src/lib/useInstantSearchApi.ts b/packages/react-instantsearch-core/src/lib/useInstantSearchApi.ts index 45c45d47d6..883961a6ea 100644 --- a/packages/react-instantsearch-core/src/lib/useInstantSearchApi.ts +++ b/packages/react-instantsearch-core/src/lib/useInstantSearchApi.ts @@ -140,7 +140,10 @@ export function useInstantSearchApi( const prevProps = prevPropsRef.current; if (prevProps.indexName !== props.indexName) { - search.helper!.setIndex(props.indexName || '').search(); + search.mainIndex + .getHelper()! + .setIndex(props.indexName || '') + .search(); prevPropsRef.current = props; } @@ -154,7 +157,7 @@ export function useInstantSearchApi( ...defaultUserAgents, serverContext && serverUserAgent, ]); - search.mainHelper!.setClient(props.searchClient).search(); + search.helper!.setClient(props.searchClient).search(); prevPropsRef.current = props; } diff --git a/packages/react-instantsearch-nextjs/src/InitializePromise.tsx b/packages/react-instantsearch-nextjs/src/InitializePromise.tsx index 9b658c868e..505f882762 100644 --- a/packages/react-instantsearch-nextjs/src/InitializePromise.tsx +++ b/packages/react-instantsearch-nextjs/src/InitializePromise.tsx @@ -63,8 +63,8 @@ export function InitializePromise({ nonce }: InitializePromiseProps) { // Extract search parameters from the search client to use them // later during hydration. let requestParamsList: SearchOptions[]; - search.mainHelper!.setClient({ - ...search.mainHelper!.getClient(), + search.helper!.setClient({ + ...search.helper!.getClient(), search(queries) { requestParamsList = queries.map(({ params }) => params); return search.client.search(queries); @@ -77,13 +77,13 @@ export function InitializePromise({ nonce }: InitializePromiseProps) { new Promise((resolve) => { let searchReceived = false; let recommendReceived = false; - search.mainHelper!.derivedHelpers[0].once('result', () => { + search.helper!.derivedHelpers[0].once('result', () => { searchReceived = true; if (!search._hasRecommendWidget || recommendReceived) { resolve(); } }); - search.mainHelper!.derivedHelpers[0].once('recommend:result', () => { + search.helper!.derivedHelpers[0].once('recommend:result', () => { recommendReceived = true; if (!search._hasSearchWidget || searchReceived) { resolve(); diff --git a/packages/react-instantsearch-nextjs/src/TriggerSearch.ts b/packages/react-instantsearch-nextjs/src/TriggerSearch.ts index 7a2fdcb9c7..d4e3ed042c 100644 --- a/packages/react-instantsearch-nextjs/src/TriggerSearch.ts +++ b/packages/react-instantsearch-nextjs/src/TriggerSearch.ts @@ -9,8 +9,8 @@ export function TriggerSearch() { if (waitForResultsRef?.current?.status === 'pending') { instantsearch._hasSearchWidget && - instantsearch.mainHelper?.searchOnlyWithDerivedHelpers(); - instantsearch._hasRecommendWidget && instantsearch.mainHelper?.recommend(); + instantsearch.helper?.searchOnlyWithDerivedHelpers(); + instantsearch._hasRecommendWidget && instantsearch.helper?.recommend(); } return null; diff --git a/packages/vue-instantsearch/src/components/Stats.vue b/packages/vue-instantsearch/src/components/Stats.vue index e65ce20761..22eb20291f 100644 --- a/packages/vue-instantsearch/src/components/Stats.vue +++ b/packages/vue-instantsearch/src/components/Stats.vue @@ -1,9 +1,6 @@