lareCacheableTask = function ({ getCacheKey, task }) { const cache = new TaskCache(); return function (...args) { return async function ({ dispatch, getState }) { const key = getCacheKey(...args); const extantResult = cache.get(key); if (extantResult) { return extantResult; } // Ensure that we have our new entry in the cache *before* dispatching the // task! let resolve; cache.put( key, new Promise(r => { resolve = r; }) ); resolve( dispatch(async function () { try { args.push(() => cache.remove(key), dispatch, getState); return await task(...args); } catch (error) { // Don't perma-cache errors. if (cache.get(key)) { cache.remove(key); } throw error; } }) ); return cache.get(key); }; }; }; PK