Skip to content
This repository has been archived by the owner on Dec 30, 2022. It is now read-only.

Commit

Permalink
perf(store): cache sanitized results
Browse files Browse the repository at this point in the history
  • Loading branch information
rayrutjes committed Jul 21, 2017
1 parent 61341a9 commit 62858b2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
7 changes: 4 additions & 3 deletions src/__tests__/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ describe('Store', () => {

test('should allow to fetch sanitized results', () => {
const store = createStore();
store._helper.lastResults = {
const response = {
hits: [
{
objectID: '1',
Expand All @@ -352,8 +352,9 @@ describe('Store', () => {
],
};

const results = store.results;
expect(results).toEqual([
store._helper.emit('result', response);

expect(store.results).toEqual([
{
objectID: '1',
name: 'test',
Expand Down
28 changes: 18 additions & 10 deletions src/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ const onHelperChange = function() {
}
};

const onHelperResult = function(response) {
this._results = sanitizeResults(
response.hits,
HIGHLIGHT_PRE_TAG,
HIGHLIGHT_POST_TAG,
'em'
);
};

export class Store {
constructor(helper) {
if (!(helper instanceof algoliaHelper.AlgoliaSearchHelper)) {
Expand All @@ -47,6 +56,7 @@ export class Store {
set algoliaHelper(helper) {
if (this._helper) {
this._helper.removeListener('change', onHelperChange);
this._helper.removeListener('result', onHelperResult);
}

this._helper = helper;
Expand All @@ -58,7 +68,14 @@ export class Store {
this._helper.setQueryParameter('highlightPostTag', HIGHLIGHT_POST_TAG);
this._helper.setPage(page);

if (this._helper.lastResults) {
onHelperResult(this._helper.lastResults);
} else {
this._results = [];
}

this._helper.on('change', onHelperChange.bind(this));
this._helper.on('result', onHelperResult.bind(this));

this._helper.getClient().addAlgoliaAgent(`vue-instantsearch ${version}`);
}
Expand Down Expand Up @@ -135,16 +152,7 @@ export class Store {
}

get results() {
if (!this._helper.lastResults) {
return [];
}

return sanitizeResults(
this._helper.lastResults.hits,
HIGHLIGHT_PRE_TAG,
HIGHLIGHT_POST_TAG,
'em'
);
return this._results;
}

get page() {
Expand Down

0 comments on commit 62858b2

Please sign in to comment.