Skip to content

Commit

Permalink
fix(index): only set listeners on init once (#4634)
Browse files Browse the repository at this point in the history
* fix(index): only set listeners on init once

Without this, if init is called multiple times, multiple event listeners are attached, as well as the helper beign recreated.

This fixes algolia/vue-instantsearch#911 by avoiding to have a different helper instance when init gets called multiple times

* test: add tests for this behaviour
  • Loading branch information
Haroenv authored Jan 26, 2021
1 parent 32bc16f commit 730b49d
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
52 changes: 52 additions & 0 deletions src/widgets/index/__tests__/index-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2341,6 +2341,58 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/index-widge
expect.anything()
);
});

describe('multiple init calls', () => {
it('does not recreate helper', () => {
const instance = index({ indexName: 'test' });

expect(instance.getHelper()).toBe(null);

instance.init(createInitOptions());

const helper1 = instance.getHelper()!;

instance.init(createInitOptions());

const helper2 = instance.getHelper()!;

expect(helper1).toBe(helper2);
});

it('does not listen on change again multiple times', () => {
const instance = index({ indexName: 'test' });

expect(instance.getHelper()).toBe(null);

instance.init(createInitOptions());

const helper = instance.getHelper()!;

expect(helper.listenerCount('change')).toBe(2);

instance.init(createInitOptions());

expect(helper.listenerCount('change')).toBe(2);
});

it('derives only once', () => {
const instance = index({ indexName: 'test' });

const mainHelper = algoliasearchHelper(createSearchClient(), '');

const instantSearchInstance = createInstantSearch({ mainHelper });

expect(instance.getHelper()).toBe(null);

instance.init(createInitOptions({ instantSearchInstance }));

expect(mainHelper.derivedHelpers.length).toBe(1);

instance.init(createInitOptions({ instantSearchInstance }));

expect(mainHelper.derivedHelpers.length).toBe(1);
});
});
});

describe('render', () => {
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 @@ -372,6 +372,12 @@ const index = (props: IndexProps): Index => {
},

init({ instantSearchInstance, parent, uiState }: IndexInitOptions) {
if (helper !== null) {
// helper is already initialized, therefore we do not need to set up
// any listeners
return;
}

localInstantSearchInstance = instantSearchInstance;
localParent = parent;
localUiState = uiState[indexId] || {};
Expand Down Expand Up @@ -416,7 +422,7 @@ const index = (props: IndexProps): Index => {
return mainHelper.search();
};

(helper as any).searchWithoutTriggeringOnStateChange = () => {
helper.searchWithoutTriggeringOnStateChange = () => {
return mainHelper.search();
};

Expand Down

0 comments on commit 730b49d

Please sign in to comment.