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

feat(helper): remove searchOnce callback #6563

Merged
merged 3 commits into from
Feb 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 1 addition & 22 deletions packages/algoliasearch-helper/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,24 +121,11 @@ declare namespace algoliasearchHelper {
* same as a search call before calling searchOnce.
* @param options can contain all the parameters that can be set to SearchParameters
* plus the index
* @param [callback] optional callback executed when the response from the
* server is back.
* @return if a callback is passed the method returns undefined
* otherwise it returns a promise containing an object with two keys :
* @return a promise containing an object with two keys :
* - content with a SearchResults
* - state with the state used for the query as a SearchParameters
* @example
* // Changing the number of records returned per page to 1
* // This example uses the callback API
* var state = helper.searchOnce({hitsPerPage: 1},
* function(error, content, state) {
* // if an error occurred it will be passed in error, otherwise its value is null
* // content contains the results formatted as a SearchResults
* // state is the instance of SearchParameters used for this search
* });
* @example
* // Changing the number of records returned per page to 1
* // This example uses the promise API
* var state1 = helper.searchOnce({hitsPerPage: 1})
* .then(promiseHandler);
*
Expand All @@ -153,14 +140,6 @@ declare namespace algoliasearchHelper {
searchOnce(
options: PlainSearchParameters
): Promise<{ content: SearchResults; state: SearchParameters }>;
searchOnce(
options: PlainSearchParameters,
cb: (
error: Error,
content: SearchResults,
state: SearchParameters
) => void
): void;

/**
* Search for facet values based on an query and the name of a faceted attribute. This
Expand Down
58 changes: 7 additions & 51 deletions packages/algoliasearch-helper/src/algoliasearch.helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,26 +201,14 @@ AlgoliaSearchHelper.prototype.getQuery = function () {
* same as a search call before calling searchOnce.
* @param {object} options can contain all the parameters that can be set to SearchParameters
* plus the index
* @param {function} [cb] optional callback executed when the response from the
* server is back.
* @return {promise|undefined} if a callback is passed the method returns undefined
* otherwise it returns a promise containing an object with two keys :
* @return {promise} returns a promise containing an object with two keys :
* - content with a SearchResults
* - state with the state used for the query as a SearchParameters
* @example
* // Changing the number of records returned per page to 1
* // This example uses the callback API
* var state = helper.searchOnce({hitsPerPage: 1},
* function(error, content, state) {
* // if an error occurred it will be passed in error, otherwise its value is null
* // content contains the results formatted as a SearchResults
* // state is the instance of SearchParameters used for this search
* });
* @example
* // Changing the number of records returned per page to 1
* // This example uses the promise API
* var state1 = helper.searchOnce({hitsPerPage: 1})
* .then(promiseHandler);
* .catch(errorHandler);
*
* function promiseHandler(res) {
* // res contains
Expand All @@ -229,8 +217,12 @@ AlgoliaSearchHelper.prototype.getQuery = function () {
* // state : SearchParameters (the one used for this specific search)
* // }
* }
*
* function errorHandler(err) {
* // handle error
* }
*/
AlgoliaSearchHelper.prototype.searchOnce = function (options, cb) {
AlgoliaSearchHelper.prototype.searchOnce = function (options) {
var tempState = !options
? this.state
: this.state.setQueryParameters(options);
Expand All @@ -244,29 +236,6 @@ AlgoliaSearchHelper.prototype.searchOnce = function (options, cb) {
state: tempState,
});

if (cb) {
this.client
.search(queries)
.then(function (content) {
self._currentNbQueries--;
if (self._currentNbQueries === 0) {
self.emit('searchQueueEmpty');
}

cb(null, new SearchResults(tempState, content.results), tempState);
})
.catch(function (err) {
self._currentNbQueries--;
if (self._currentNbQueries === 0) {
self.emit('searchQueueEmpty');
}

cb(err, null, tempState);
});

return undefined;
}

return this.client.search(queries).then(
function (content) {
self._currentNbQueries--;
Expand Down Expand Up @@ -1750,19 +1719,6 @@ AlgoliaSearchHelper.prototype._dispatchRecommendError = function (
if (this._currentNbRecommendQueries === 0) this.emit('recommendQueueEmpty');
};

/**
* Test if there are some disjunctive refinements on the facet
* @private
* @param {string} facet the attribute to test
* @return {boolean} true if there are refinements on this attribute
*/
AlgoliaSearchHelper.prototype._hasDisjunctiveRefinements = function (facet) {
return (
this.state.disjunctiveRefinements[facet] &&
this.state.disjunctiveRefinements[facet].length > 0
);
};

AlgoliaSearchHelper.prototype._change = function (event) {
var state = event.state;
var isPageReset = event.isPageReset;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ test('[INT][SEARCHONCE] Should be able to search once with custom parameters wit
var helper = algoliasearchHelper(client, indexName);
var state0 = helper.state;

var calls = 1;
var calls = 0;
helper.on('error', function (error) {
done.fail(error);
done(error);
});

helper.on('result', function (event) {
Expand All @@ -43,6 +43,7 @@ test('[INT][SEARCHONCE] Should be able to search once with custom parameters wit
}
});

calls++;
var state1 = state0.setFacets(['facet']).addFacetRefinement('facet', 'f1');
helper.searchOnce(state1).then(function (res) {
expect(helper.state).toBe(state0);
Expand All @@ -58,10 +59,10 @@ test('[INT][SEARCHONCE] Should be able to search once with custom parameters wit
return hit.objectID === '1';
})
).toBeTruthy();

calls++;
var state2 = state0.setFacets(['facet']).addFacetRefinement('facet', 'f2');
helper.searchOnce(state2, function (err, c, s) {
expect(err).toBe(null);
return helper.searchOnce(state2).then(function ({ content: c, state: s }) {
expect(helper.state).toBe(state0);
expect(s).toEqual(state2);
expect(c.hits.length).toBe(2);
Expand All @@ -75,6 +76,7 @@ test('[INT][SEARCHONCE] Should be able to search once with custom parameters wit
return hit.objectID === '3';
})
).toBeTruthy();

calls++;
helper.search();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,7 @@ algoliasearch = algoliasearch.algoliasearch || algoliasearch;

var algoliasearchHelper = require('../../../index');

test('When searchOnce with callback, hasPendingRequests is true', function (done) {
var testData = require('../../datasets/SearchParameters/search.dataset')();
var client = algoliasearch('dsf', 'dsfdf');

var triggerCb;
client.search = function () {
return new Promise(function (resolve) {
triggerCb = function () {
resolve(testData.response);
};
});
};

var helper = algoliasearchHelper(client, 'test_hotels-node');
var countNoMoreSearch = 0;
helper.on('searchQueueEmpty', function () {
countNoMoreSearch += 1;
});

expect(helper.hasPendingRequests()).toBe(false);

helper.searchOnce(helper.state, function () {
expect(helper.hasPendingRequests()).toBe(false);
expect(countNoMoreSearch).toBe(1);
done();
});

expect(helper.hasPendingRequests()).toBe(true);
expect(countNoMoreSearch).toBe(0);

triggerCb();
});

test('When searchOnce with promises, hasPendingRequests is true', function (done) {
test('When calling searchOnce, hasPendingRequests is true', function (done) {
var testData = require('../../datasets/SearchParameters/search.dataset')();
var client = algoliasearch('dsf', 'dsfdf');

Expand Down Expand Up @@ -111,7 +78,7 @@ test('When searchForFacetValues, hasPendingRequests is true', function (done) {
triggerCb();
});

test('When helper.search(), hasPendingRequests is true', function (done) {
test('When calling search, hasPendingRequests is true', function (done) {
var testData = require('../../datasets/SearchParameters/search.dataset')();
var client = algoliasearch('dsf', 'dsfdf');

Expand Down Expand Up @@ -146,7 +113,7 @@ test('When helper.search(), hasPendingRequests is true', function (done) {
triggerCb();
});

test('When helper.search() and one request is discarded, hasPendingRequests is true unless all come back', function (done) {
test('When calling sarech and one request is discarded, hasPendingRequests is true unless all come back', function (done) {
var testData = require('../../datasets/SearchParameters/search.dataset')();
var client = algoliasearch('dsf', 'dsfdf');

Expand Down