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

Implement queue deselector #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
11 changes: 8 additions & 3 deletions src/redux/createNetworkMiddleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type Arguments = {|
regexActionType: RegExp,
actionTypes: Array<string>,
queueReleaseThrottle: number,
shouldDequeueSelector: (state: State) => boolean
|};

function validateParams(regexActionType, actionTypes) {
Expand Down Expand Up @@ -70,11 +71,12 @@ function didComeBackOnline(action, wasConnected) {
);
}

export const createReleaseQueue = (getState, next, delay) => async queue => {
export const createReleaseQueue = (getState, next, delay, shouldDequeueSelector) => async queue => {
// eslint-disable-next-line
for (const action of queue) {
const { isConnected } = getState().network;
if (isConnected) {
if (isConnected &&
shouldDequeueSelector(getState())) {
next(removeActionFromQueue(action));
next(action);
// eslint-disable-next-line
Expand All @@ -89,6 +91,7 @@ function createNetworkMiddleware({
regexActionType = /FETCH.*REQUEST/,
actionTypes = [],
queueReleaseThrottle = 50,
shouldDequeueSelector = (state) => true,
}: Arguments = {}) {
return ({ getState }: MiddlewareAPI<State>) => (
next: (action: any) => void,
Expand All @@ -98,6 +101,7 @@ function createNetworkMiddleware({
getState,
next,
queueReleaseThrottle,
shouldDequeueSelector
);
validateParams(regexActionType, actionTypes);

Expand All @@ -114,7 +118,8 @@ function createNetworkMiddleware({
}

const isBackOnline = didComeBackOnline(action, isConnected);
if (isBackOnline) {
let shouldDequeue = (isConnected || isBackOnline) && actionQueue.length > 0 && shouldDequeueSelector(getState());
if (shouldDequeue) {
// Dispatching queued actions in order of arrival (if we have any)
next(action);
return releaseQueue(actionQueue);
Expand Down
28 changes: 27 additions & 1 deletion test/createNetworkMiddleware.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -385,11 +385,37 @@ describe('createReleaseQueue', () => {
mockDispatch.mockClear();
mockGetState.mockClear();
});
it('empties the queue if we are online', async () => {

it('empties the queue if we are online and dequeue selector returns true', async () => {
const mockDequeueSelector = () => true;
const releaseQueue = createReleaseQueue(
mockGetState,
mockDispatch,
mockDelay,
mockDequeueSelector,
);
const actionQueue = ['foo', 'bar'];
await releaseQueue(actionQueue);
expect(mockDispatch).toHaveBeenCalledTimes(4);
expect(mockDispatch).toHaveBeenNthCalledWith(
1,
removeActionFromQueue('foo'),
);
expect(mockDispatch).toHaveBeenNthCalledWith(2, 'foo');
expect(mockDispatch).toHaveBeenNthCalledWith(
3,
removeActionFromQueue('bar'),
);
expect(mockDispatch).toHaveBeenNthCalledWith(4, 'bar');
});

it('does not empty the queue if we are online and dequeue selector returns false', async () => {
const mockDequeueSelector = () => false;
const releaseQueue = createReleaseQueue(
mockGetState,
mockDispatch,
mockDelay,
mockDequeueSelector,
);
const actionQueue = ['foo', 'bar'];
await releaseQueue(actionQueue);
Expand Down
Loading