Skip to content

Commit

Permalink
Implement revalidation enhancement
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverlin committed Jan 15, 2021
1 parent 47fd830 commit f6f2cfe
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const defaultInterceptor = function ({ proceedError, err, replay, getState }) {
const noopDefaultParams = () => {
return {}
}
const executionTimeMap = {}

export default ({
baseUrl,
Expand All @@ -38,6 +39,21 @@ export default ({

return new Promise((resolve, reject) => {
const promiseCreators = action[CHAIN_API].map((createCallApiAction) => {
const apiAction = createCallApiAction()
if (apiAction.revalidationEnabled) {
const revalidationKey = _getRevalidationKey(createCallApiAction)
const lastExecutedTime = executionTimeMap[revalidationKey]
const hasKey = !!lastExecutedTime
if (!hasKey) {
return () => Promise.resolve()
}
const now = Math.floor(new Date().getTime() / 1000)
const shouldNotRevalidate = action.revalidate && (now - lastExecutedTime) > action.revalidate
if (!shouldNotRevalidate) {
return () => Promise.resolve()
}
}

return createRequestPromise({
timeout,
generateDefaultParams,
Expand All @@ -58,3 +74,18 @@ export default ({
})
}
}

function _getRevalidationKey(actionObj) {
const {
method,
url,
params,
data,
} = actionObj
return JSON.stringify({
method,
url,
params,
data,
})
}

0 comments on commit f6f2cfe

Please sign in to comment.