Skip to content

Commit 236eb7b

Browse files
committed
fix(helper): expose .lastResults to .helper (#4170)
* fix(helper): expose .lastResults to .helper In v3, the helper exposed on the instance is the same helper as the one who searches. This means that lastResults is set as expected. However, with v4 we split this up in the derived helper which actually searches and the exposed helper. This is fixed now by just setting the last results once the search is done on the derived helper. * edit comment
1 parent 004bd3e commit 236eb7b

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/lib/__tests__/InstantSearch-test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,24 @@ See https://www.algolia.com/doc/api-reference/widgets/configure/js/`);
277277

278278
expect(search.insightsClient).toBe(insightsClient);
279279
});
280+
281+
it("exposes helper's last results", async () => {
282+
const searchClient = createSearchClient();
283+
284+
const search = new InstantSearch({
285+
indexName: 'indexName',
286+
searchClient,
287+
});
288+
289+
expect(search.helper).toBe(null);
290+
291+
search.start();
292+
293+
await runAllMicroTasks();
294+
295+
// could be null if we don't pretend the main helper is the one who searched
296+
expect(search.helper.lastResults).not.toBe(null);
297+
});
280298
});
281299

282300
describe('addWidget(s)', () => {

src/widgets/index/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,11 +459,17 @@ See https://www.algolia.com/doc/guides/building-search-ui/widgets/customize-an-e
459459
}
460460
});
461461

462-
derivedHelper.on('result', () => {
462+
derivedHelper.on('result', ({ results }) => {
463463
// The index does not render the results it schedules a new render
464464
// to let all the other indices emit their own results. It allows us to
465465
// run the render process in one pass.
466466
instantSearchInstance.scheduleRender();
467+
468+
// the derived helper is the one which actually searches, but the helper
469+
// which is exposed e.g. via instance.helper, doesn't search, and thus
470+
// does not have access to lastResults, which it used to in pre-federated
471+
// search behavior.
472+
helper!.lastResults = results;
467473
});
468474

469475
localWidgets.forEach(widget => {

0 commit comments

Comments
 (0)