Skip to content

Commit

Permalink
Change requestCacheMiddleware to always dispatch request and success …
Browse files Browse the repository at this point in the history
…actions
  • Loading branch information
klis87 committed Apr 5, 2019
1 parent 3f7fcdd commit 29d13f8
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 89 deletions.
7 changes: 6 additions & 1 deletion packages/redux-saga-requests/src/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
SUCCESS_SUFFIX,
ERROR_SUFFIX,
ABORT_SUFFIX,
GET_REQUEST_CACHE,
CLEAR_REQUESTS_CACHE,
} from './constants';

Expand All @@ -15,16 +16,18 @@ export const abort = getActionWithSuffix(ABORT_SUFFIX);

const isFSA = action => !!action.payload;

export const createSuccessAction = (action, data) => ({
export const createSuccessAction = (action, data, response) => ({
type: success(action.type),
...(isFSA(action)
? {
payload: {
data,
response,
},
}
: {
data,
response,
}),
meta: {
...action.meta,
Expand Down Expand Up @@ -89,6 +92,8 @@ export const isErrorAction = action =>
export const isAbortAction = action =>
isResponseAction(action) && action.type.endsWith(ABORT_SUFFIX);

export const getRequestCache = () => ({ type: GET_REQUEST_CACHE });

export const clearRequestsCache = (...actionTypes) => ({
type: CLEAR_REQUESTS_CACHE,
actionTypes,
Expand Down
15 changes: 12 additions & 3 deletions packages/redux-saga-requests/src/actions.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,12 @@ describe('actions', () => {
request: { url: '/' },
};

expect(createSuccessAction(requestAction, 'data')).toEqual({
expect(
createSuccessAction(requestAction, 'data', { data: 'data' }),
).toEqual({
type: 'REQUEST_SUCCESS',
data: 'data',
response: { data: 'data' },
meta: {
requestAction,
},
Expand All @@ -58,10 +61,13 @@ describe('actions', () => {
},
};

expect(createSuccessAction(requestAction, 'data')).toEqual({
expect(
createSuccessAction(requestAction, 'data', { data: 'data' }),
).toEqual({
type: 'REQUEST_SUCCESS',
payload: {
data: 'data',
response: { data: 'data' },
},
meta: {
requestAction,
Expand All @@ -78,9 +84,12 @@ describe('actions', () => {
},
};

expect(createSuccessAction(requestAction, 'data')).toEqual({
expect(
createSuccessAction(requestAction, 'data', { data: 'data' }),
).toEqual({
type: 'REQUEST_SUCCESS',
data: 'data',
response: { data: 'data' },
meta: {
requestAction,
asPromise: true,
Expand Down
1 change: 1 addition & 0 deletions packages/redux-saga-requests/src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const INTERCEPTORS = {
ON_SUCCESS: 'onSuccess',
ON_ABORT: 'onAbort',
};
export const GET_REQUEST_CACHE = 'GET_REQUEST_CACHE';
export const CLEAR_REQUESTS_CACHE = 'CLEAR_REQUESTS_CACHE';
export const INCORRECT_PAYLOAD_ERROR =
"Incorrect payload for request action. Action must have form of { type: 'TYPE', request: {} }, { type: 'TYPE', request: [{}, {}] }, { type: 'TYPE', payload: { request: {} } } or { type: 'TYPE', payload: { request: [{}, {}] } }";
7 changes: 7 additions & 0 deletions packages/redux-saga-requests/src/constants.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
ERROR_SUFFIX,
ABORT_SUFFIX,
REQUESTS_CONFIG,
GET_REQUEST_CACHE,
CLEAR_REQUESTS_CACHE,
INCORRECT_PAYLOAD_ERROR,
} from './constants';
Expand Down Expand Up @@ -32,6 +33,12 @@ describe('constants', () => {
});
});

describe('GET_REQUEST_CACHE', () => {
it('has correct value', () => {
expect(GET_REQUEST_CACHE).toBe('GET_REQUEST_CACHE');
});
});

describe('CLEAR_REQUESTS_CACHE', () => {
it('has correct value', () => {
expect(CLEAR_REQUESTS_CACHE).toBe('CLEAR_REQUESTS_CACHE');
Expand Down
37 changes: 27 additions & 10 deletions packages/redux-saga-requests/src/middleware.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { CLEAR_REQUESTS_CACHE } from './constants';
import { GET_REQUEST_CACHE, CLEAR_REQUESTS_CACHE } from './constants';
import {
success,
isRequestAction,
isSuccessAction,
isResponseAction,
getRequestActionFromResponse,
getActionPayload,
} from './actions';

const shouldActionBePromisified = (action, auto) =>
Expand Down Expand Up @@ -42,15 +43,20 @@ export const requestsPromiseMiddleware = ({ auto = false } = {}) => {
};
};

const isCacheValid = cache => cache === true || Date.now() <= cache;
const isCacheValid = cache =>
cache.expiring === null || Date.now() <= cache.expiring;

const getNewCacheValue = cache =>
cache === true ? cache : cache * 1000 + Date.now();
const getNewCacheTimeout = cache =>
cache === true ? null : cache * 1000 + Date.now();

export const requestsCacheMiddleware = () => {
const cacheMap = new Map();

return () => next => action => {
if (action.type === GET_REQUEST_CACHE) {
return cacheMap;
}

if (action.type === CLEAR_REQUESTS_CACHE) {
if (action.actionTypes.length === 0) {
cacheMap.clear();
Expand All @@ -66,14 +72,25 @@ export const requestsCacheMiddleware = () => {
cacheMap.get(action.type) &&
isCacheValid(cacheMap.get(action.type))
) {
return null;
return next({
...action,
meta: {
...action.meta,
cacheResponse: cacheMap.get(action.type).response,
},
});
}

if (isSuccessAction(action) && action.meta && action.meta.cache) {
cacheMap.set(
getRequestActionFromResponse(action).type,
getNewCacheValue(action.meta.cache),
);
if (
isSuccessAction(action) &&
action.meta &&
action.meta.cache &&
!action.meta.cacheResponse
) {
cacheMap.set(getRequestActionFromResponse(action).type, {
response: getActionPayload(action).response,
expiring: getNewCacheTimeout(action.meta.cache),
});
}

return next(action);
Expand Down
Loading

0 comments on commit 29d13f8

Please sign in to comment.