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

Commit

Permalink
fix(store): delay call to listener to prevent infinite loops (#143)
Browse files Browse the repository at this point in the history
Can happen when observing the loading property that change too fast.
  • Loading branch information
mthuret authored Jun 29, 2017
1 parent 4f70a09 commit 0945958
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/react-instantsearch/src/core/createStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export default function createStore(initialState) {
let state = initialState;
const listeners = [];
function dispatch() {
listeners.forEach(listener => listener());
listeners.forEach(listener => setTimeout(listener, 0));
}
return {
getState() {
Expand Down
7 changes: 7 additions & 0 deletions packages/react-instantsearch/src/core/createStore.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/* eslint-disable no-console */

import createStore from './createStore';
jest.useFakeTimers();

describe('createStore', () => {
describe('getState', () => {
Expand All @@ -17,7 +18,10 @@ describe('createStore', () => {
const initialState = {};
const store = createStore(initialState);
const newState = {};

store.setState(newState);
jest.runAllTimers();

expect(store.getState()).toBe(newState);
});
});
Expand All @@ -31,6 +35,7 @@ describe('createStore', () => {
const newState = {};
expect(listener.mock.calls.length).toBe(0);
store.setState(newState);
jest.runAllTimers();
expect(listener.mock.calls.length).toBe(1);
});

Expand All @@ -42,10 +47,12 @@ describe('createStore', () => {
const newState = {};
expect(listener.mock.calls.length).toBe(0);
store.setState(newState);
jest.runAllTimers();
expect(listener.mock.calls.length).toBe(1);
unsubscribe();
const newerState = {};
store.setState(newerState);
jest.runAllTimers();
expect(listener.mock.calls.length).toBe(1);
});
});
Expand Down

0 comments on commit 0945958

Please sign in to comment.