Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

createResolversCacheMiddleware: remove dependency on core/data store #54733

Merged
merged 2 commits into from
Sep 25, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 21 additions & 27 deletions packages/data/src/resolvers-cache-middleware.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,47 @@
/**
* Internal dependencies
*/
import coreDataStore from './store';

/** @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.
* @param {WPDataRegistry} registry Registry for which to create the middleware.
* @param {string} storeName Name of the store for which to create the middleware.
*
* @return {Function} Middleware function.
*/
const createResolversCacheMiddleware =
( registry, reducerKey ) => () => ( next ) => ( action ) => {
const resolvers = registry
.select( coreDataStore )
.getCachedResolvers( reducerKey );
Object.entries( resolvers ).forEach(
( [ selectorName, resolversByArgs ] ) => {
( registry, storeName ) => () => ( next ) => ( action ) => {
const resolvers = registry.select( storeName ).getCachedResolvers();
const resolverEntries = Object.entries( resolvers );
resolverEntries.forEach( ( [ selectorName, resolversByArgs ] ) => {
const resolver =
registry.stores?.[ reducerKey ]?.resolvers?.[
selectorName
];
registry.stores[ storeName ]?.resolvers?.[ selectorName ];
if ( ! resolver || ! resolver.shouldInvalidate ) {
return;
}
resolversByArgs.forEach( ( value, args ) => {
// Works around a bug in `EquivalentKeyMap` where `map.delete` merely sets an entry value
// to `undefined` and `map.forEach` then iterates also over these orphaned entries.
if ( value === undefined ) {
return;
}

// 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?.status !== 'finished' &&
value?.status !== 'error' ) ||
! resolver.shouldInvalidate( action, ...args )
) {
if ( value.status !== 'finished' && value.status !== 'error' ) {
return;
}

if ( ! resolver.shouldInvalidate( action, ...args ) ) {
return;
}

// Trigger cache invalidation
registry
.dispatch( coreDataStore )
.invalidateResolution( reducerKey, selectorName, args );
.dispatch( storeName )
.invalidateResolution( selectorName, args );
} );
} );
}
);
return next( action );
};

Expand Down