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

Allow user to write action specific policy for batching #6

Closed
wants to merge 1 commit into from
Closed
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
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,19 @@ const finalCreateStore = batchedSubscribe(batchDebounce)(createStore);
const store = finalCreateStore(reducer, intialState);
```

### Debounced subscribe handlers for specific action:

```js
import { createStore } from 'redux';
import { batchedSubscribe } from 'redux-batched-subscribe';
import debounce from 'lodash.debounce';

const batchDebounce = debounce(notify => notify());
const batchDebounceOnlyRegister = (notify, action) => action.type == 'REGISTER' ? batchDebounce(notify) : notify();
const finalCreateStore = batchedSubscribe(dontBatchDebounceRegister)(createStore);
const store = finalCreateStore(reducer, intialState);
```

### React batched updates

```js
Expand Down
12 changes: 12 additions & 0 deletions src/__tests__/batchedSubscribe-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ describe('batchedSubscribe()', () => {
expect(batchSpy.calls.length).toEqual(1);
});

it('it calls batch function with the action as second parameter', () => {
const batchSpy = createSpy();
const baseStore = createStoreShape();
const createStore = () => baseStore;
const store = batchedSubscribe(batchSpy)(createStore)();

const action = { type: 'foo' };
store.dispatch(action);

expect(batchSpy.calls[0].arguments[1]).toEqual(action);
});

it('batch callback executes listeners', () => {
const subscribeCallbackSpy = createSpy();
const baseStore = createStoreShape();
Expand Down
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ export function batchedSubscribe(batch) {
};
}

function notifyListenersBatched() {
batch(() => listeners.slice().forEach(listener => listener()));
function notifyListenersBatched(...dispatchArgs) {
batch(() => listeners.slice().forEach(listener => listener()), ...dispatchArgs);
}

return next => (...args) => {
Expand All @@ -24,7 +24,7 @@ export function batchedSubscribe(batch) {

function dispatch(...dispatchArgs) {
const res = store.dispatch(...dispatchArgs);
notifyListenersBatched();
notifyListenersBatched(...dispatchArgs);
return res;
}

Expand Down