Skip to content

Commit

Permalink
Update cache name, add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
mcsf committed Dec 14, 2023
1 parent 940e68b commit 6434535
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 17 deletions.
4 changes: 2 additions & 2 deletions packages/block-library/src/pattern/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { __, sprintf } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import { usePatternRecursionDetector } from './recursion-detector';
import { useParsePatternDependencies } from './recursion-detector';

const PatternEdit = ( { attributes, clientId } ) => {
const selectedPattern = useSelect(
Expand All @@ -40,7 +40,7 @@ const PatternEdit = ( { attributes, clientId } ) => {
useSelect( blockEditorStore );

const [ hasRecursionError, setHasRecursionError ] = useState( false );
const parsePatternDependencies = usePatternRecursionDetector();
const parsePatternDependencies = useParsePatternDependencies();

// Duplicated in packages/edit-site/src/components/start-template-options/index.js.
function injectThemeAttributeInBlockTemplateContent( block ) {
Expand Down
44 changes: 29 additions & 15 deletions packages/block-library/src/pattern/recursion-detector.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,41 @@
*/
import { useRegistry } from '@wordpress/data';

const patternDependenciesRegistry = new WeakMap();
// Naming is hard.
//
// @see useParsePatternDependencies
const cache = new WeakMap();

/**
* Hook that returns a function, `parsePatternDependencies`, but bound
* specifically to the context's registry:
*
* @example
* ```js
* const parsePatternDependencies = useParsePatternDependencies();
* parsePatternDependencies( selectedPattern );
* ```
*
* @see parsePatternDependencies
*
* @return {Function} A bound function
*/
export function useParsePatternDependencies() {
const registry = useRegistry();

if ( ! cache.has( registry ) ) {
cache.set( registry, parsePatternDependencies.bind( null, new Map() ) );
}
return cache.get( registry );
}

/**
* Parse a given pattern and traverse its contents to detect any subsequent
* patterns on which it may depend. Such occurrences will be added to an
* internal dependency graph. If a circular dependency is detected, an
* error will be thrown.
*
* Exported for testing purposes only.
* EXPORTED FOR TESTING PURPOSES ONLY.
*
* @param {Map<string, Set<string>>} deps Map of pattern dependencies.
* @param {Object} pattern Pattern.
Expand All @@ -49,7 +75,7 @@ export function parsePatternDependencies( deps, { name, blocks } ) {
* Declare that pattern `a` depends on pattern `b`. If a circular
* dependency is detected, an error will be thrown.
*
* Exported for testing purposes only.
* EXPORTED FOR TESTING PURPOSES ONLY.
*
* @param {Map<string, Set<string>>} deps Map of pattern dependencies.
* @param {string} a Slug for pattern A.
Expand Down Expand Up @@ -106,15 +132,3 @@ function hasCycle(
currentPath.delete( slug );
return false;
}

export function usePatternRecursionDetector() {
const registry = useRegistry();

if ( ! patternDependenciesRegistry.has( registry ) ) {
patternDependenciesRegistry.set(
registry,
parsePatternDependencies.bind( null, new Map() )
);
}
return patternDependenciesRegistry.get( registry );
}

0 comments on commit 6434535

Please sign in to comment.