From 643453547c070e148519a2444328d2ff893ef852 Mon Sep 17 00:00:00 2001 From: Miguel Fonseca <150562+mcsf@users.noreply.github.com> Date: Thu, 14 Dec 2023 12:49:33 +0000 Subject: [PATCH] Update cache name, add comments --- packages/block-library/src/pattern/edit.js | 4 +- .../src/pattern/recursion-detector.js | 44 ++++++++++++------- 2 files changed, 31 insertions(+), 17 deletions(-) diff --git a/packages/block-library/src/pattern/edit.js b/packages/block-library/src/pattern/edit.js index 165bd60ef2fa2..7befaa9cb2dee 100644 --- a/packages/block-library/src/pattern/edit.js +++ b/packages/block-library/src/pattern/edit.js @@ -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( @@ -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 ) { diff --git a/packages/block-library/src/pattern/recursion-detector.js b/packages/block-library/src/pattern/recursion-detector.js index a390ea98f4140..da2b591257929 100644 --- a/packages/block-library/src/pattern/recursion-detector.js +++ b/packages/block-library/src/pattern/recursion-detector.js @@ -15,7 +15,33 @@ */ 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 @@ -23,7 +49,7 @@ const patternDependenciesRegistry = new WeakMap(); * 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>} deps Map of pattern dependencies. * @param {Object} pattern Pattern. @@ -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>} deps Map of pattern dependencies. * @param {string} a Slug for pattern A. @@ -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 ); -}