Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(createInstantSearchManager): drop outdated response #1765

Merged
merged 1 commit into from
Dec 22, 2016

Conversation

bobylito
Copy link
Contributor

@bobylito bobylito commented Dec 19, 2016

This PR is for React-InstantSearch

The event model for the helper is implemented in a way that it will drop outdated requests.
This change makes react-instantsearch use the event model. This mechanism ensures reliability in the algolia answers.

It also results in some fewer instanciations of objects for callbacks and the base search parameters.

Details of what was done

The previous implementation was based on searchOnce. This method does not protect you from responses that come back unordered because it is based on the cb / promise from the algolia client which will always call the callback even if they are unordered.

NOW

Use the helper properly with events and its protection against this kind of behaviour.

**The tests are now up to date 🍾 **

A note about the test 🤔

The tests were based on mocks that were making a lot of assumptions about the internals. That's why they were all broken after my changes. I tried to redo them by making less assumptions about the internals, and use the external API as much as possible.

That being said, this refactoring is now based on the event API of the helper, which is very hard to manipulate without some heavy mocking. Writing those tests I also learned a bit about the jest magic concerning the mocks and their limitations. For example you can't change the implementation on the fly (calling again jest.mock doesn't do anything) nor can you reference a closed variable. That's why I ended up with three test files, instead of one (one without mocks and two with different behaviours for the mocks)

If you have any other solution about how to make those tests I would be very grateful 🙇 Thanks 🤓

@algobot
Copy link
Contributor

algobot commented Dec 19, 2016

By analyzing the blame information on this pull request, we identified @Morhaus, @mthuret and @vvo to be potential reviewers

@mthuret
Copy link
Contributor

mthuret commented Dec 19, 2016

I like this change, this will simplify the code for the search for facet values feature when calling the helper.

@bobylito bobylito force-pushed the fix/request-response-order branch from 72a6b68 to 136f327 Compare December 21, 2016 08:57
@bobylito
Copy link
Contributor Author

/o\ OMG the tests are behaving differently on CI :(

@bobylito bobylito force-pushed the fix/request-response-order branch from 136f327 to fb4689d Compare December 21, 2016 09:58
@bobylito
Copy link
Contributor Author

Test are passing!! 🎉

});

describe('createInstantSearchManager', () => {
describe('with correct result from algolia', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wasn't supposed to be "with error" here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, copy pasting too quick :/ Thanks :)

console.log('setup');
const Helper = require.requireActual('algoliasearch-helper/src/algoliasearch.helper.js');
Helper.prototype._handleResponse = function(state) {
this.emit('error', {count: count++}, state);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid having several files, maybe something you could do is emitting a different event depending of the query present in the state parameter?

@bobylito bobylito force-pushed the fix/request-response-order branch from fb4689d to ee30547 Compare December 21, 2016 13:27
@vvo
Copy link
Contributor

vvo commented Dec 21, 2016

Since I am not deeply into this code, could you detail a bit more:

  • what was done not the right way before
  • what were the bad behaviors being trigged by the previous implementation
  • how the new implementation fix it

That would help a bit grasp the PR context, thanks!

algoliaClient,
searchParameters = {},
}) {
const helper = algoliasearchHelper(algoliaClient);
if (!algoliaClient || !indexName) throw new Error('indexName and algoliaClient are mandatory');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move this to another PR? Also the error message is not exactly right

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes sure!

@bobylito
Copy link
Contributor Author

bobylito commented Dec 21, 2016

I updated the description @vvo Let me know if you need more details.

The helper is implemented in a way that it will drop outdated requests.
This change makes react-instantsearch use the event model of the helper
that let it implement this mechanism that ensures reliability in the
algolia answers. It also results in some fewer instanciation of objects
for callbacks and the base search parameters.
Copy link
Contributor

@mthuret mthuret left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@bobylito bobylito merged commit 76c5312 into v2 Dec 22, 2016
@bobylito bobylito deleted the fix/request-response-order branch December 22, 2016 14:43
vvo pushed a commit that referenced this pull request Jan 4, 2017
<a name="2.1.0"></a>
# [2.1.0](v2.0.1...v2.1.0) (2017-01-04)

### Bug Fixes

* **createInstantSearchManager:** drop outdated response (#1765) ([76c5312](76c5312))
* **highlight:** highlight should work even if the attribute is missing (#1791) ([5b79b15](5b79b15)), closes [#1790](#1790)
* **InfiniteHits:** better classname to loadmore btn (#1789) ([ad2ded3](ad2ded3))
* **starRatings:** click on selected range doesn't unselect it (#1766) ([beacc72](beacc72))
* **website:** broken demo links (#1802) ([0abe2f5](0abe2f5))
* **widgets:** add 300px width for the default SearchBox (#1803) ([bf5d791](bf5d791))

### Features

* **InfiniteHits:** Add class to load more button (#1787) ([416febd](416febd))
* **RefinementList, connectRefinementList:** allow to search for facet values ([e086a81](e086a81))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants