@wordpress/data
Version:
Data module for WordPress.
57 lines (45 loc) • 2.44 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _store = _interopRequireDefault(require("./store"));
/**
* Internal dependencies
*/
/** @typedef {import('./registry').WPDataRegistry} WPDataRegistry */
/**
* Creates a middleware handling resolvers cache invalidation.
*
* @param {WPDataRegistry} registry The registry reference for which to create
* the middleware.
* @param {string} reducerKey The namespace for which to create the
* middleware.
*
* @return {Function} Middleware function.
*/
const createResolversCacheMiddleware = (registry, reducerKey) => () => next => action => {
const resolvers = registry.select(_store.default).getCachedResolvers(reducerKey);
Object.entries(resolvers).forEach(_ref => {
var _registry$stores, _registry$stores$redu, _registry$stores$redu2;
let [selectorName, resolversByArgs] = _ref;
const resolver = (_registry$stores = registry.stores) === null || _registry$stores === void 0 ? void 0 : (_registry$stores$redu = _registry$stores[reducerKey]) === null || _registry$stores$redu === void 0 ? void 0 : (_registry$stores$redu2 = _registry$stores$redu.resolvers) === null || _registry$stores$redu2 === void 0 ? void 0 : _registry$stores$redu2[selectorName];
if (!resolver || !resolver.shouldInvalidate) {
return;
}
resolversByArgs.forEach((value, args) => {
// resolversByArgs is the map Map([ args ] => boolean) storing the cache resolution status for a given selector.
// If the value is "finished" or "error" it means this resolver has finished its resolution which means we need
// to invalidate it, if it's true it means it's inflight and the invalidation is not necessary.
if ((value === null || value === void 0 ? void 0 : value.status) !== 'finished' && (value === null || value === void 0 ? void 0 : value.status) !== 'error' || !resolver.shouldInvalidate(action, ...args)) {
return;
} // Trigger cache invalidation
registry.dispatch(_store.default).invalidateResolution(reducerKey, selectorName, args);
});
});
return next(action);
};
var _default = createResolversCacheMiddleware;
exports.default = _default;
//# sourceMappingURL=resolvers-cache-middleware.js.map