Skip to content

Commit

Permalink
fix(helper): expose .lastResults to .helper (#4170)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Haroenv committed Oct 23, 2019
1 parent 004bd3e commit 236eb7b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/lib/__tests__/InstantSearch-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,24 @@ See https://www.algolia.com/doc/api-reference/widgets/configure/js/`);

expect(search.insightsClient).toBe(insightsClient);
});

it("exposes helper's last results", async () => {
const searchClient = createSearchClient();

const search = new InstantSearch({
indexName: 'indexName',
searchClient,
});

expect(search.helper).toBe(null);

search.start();

await runAllMicroTasks();

// could be null if we don't pretend the main helper is the one who searched
expect(search.helper.lastResults).not.toBe(null);
});
});

describe('addWidget(s)', () => {
Expand Down
8 changes: 7 additions & 1 deletion src/widgets/index/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -459,11 +459,17 @@ See https://www.algolia.com/doc/guides/building-search-ui/widgets/customize-an-e
}
});

derivedHelper.on('result', () => {
derivedHelper.on('result', ({ results }) => {
// The index does not render the results it schedules a new render
// to let all the other indices emit their own results. It allows us to
// run the render process in one pass.
instantSearchInstance.scheduleRender();

// the derived helper is the one which actually searches, but the helper
// which is exposed e.g. via instance.helper, doesn't search, and thus
// does not have access to lastResults, which it used to in pre-federated
// search behavior.
helper!.lastResults = results;
});

localWidgets.forEach(widget => {
Expand Down

0 comments on commit 236eb7b

Please sign in to comment.