Created
November 21, 2018 20:45
-
-
Save jcuffe/8812663f669ce3f737e4af32fbf7b603 to your computer and use it in GitHub Desktop.
axios caching
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Cache the response of an axios request | |
// | |
const cacheRequest = request => { | |
let cache = null; | |
return () => { | |
cache = cache || request.then(({ data }) => data); | |
return cache; | |
}; | |
}; | |
// | |
// Expensive requests used in multiple calculations | |
// | |
const getSystemCosts = cacheRequest(axios.get(endpoints.systemCosts)); | |
const getAdjustedPrices = cacheRequest(axios.get(endpoints.marketPrices)); | |
// | |
// Add adjusted price for each item for job cost calculation | |
// | |
const injectAdjustedPrice = types => { | |
return new Promise(resolve => { | |
getAdjustedPrices().then(prices => { | |
prices.forEach(({ type_id: id, adjusted_price }) => { | |
if (types[id] && types[id].outputs) { | |
types[id].adjusted_price = adjusted_price; | |
} | |
}); | |
resolve(types); | |
}); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment