From ba1812d9f58771a3a89f65aaf1e4f05b23f96ee9 Mon Sep 17 00:00:00 2001 From: Jarda Snajdr Date: Wed, 16 Mar 2022 09:23:22 +0100 Subject: [PATCH] Use memize to memoize --- package-lock.json | 1 + packages/blocks/package.json | 1 + .../src/api/parser/get-block-attributes.js | 21 +------------------ 3 files changed, 3 insertions(+), 20 deletions(-) diff --git a/package-lock.json b/package-lock.json index 2722d78bb4ac28..67a636713f11f6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -16297,6 +16297,7 @@ "colord": "^2.7.0", "hpq": "^1.3.0", "lodash": "^4.17.21", + "memize": "^1.1.0", "rememo": "^3.0.0", "showdown": "^1.9.1", "simple-html-tokenizer": "^0.5.7", diff --git a/packages/blocks/package.json b/packages/blocks/package.json index f8a0491322b979..47cb45c14a57d7 100644 --- a/packages/blocks/package.json +++ b/packages/blocks/package.json @@ -45,6 +45,7 @@ "colord": "^2.7.0", "hpq": "^1.3.0", "lodash": "^4.17.21", + "memize": "^1.1.0", "rememo": "^3.0.0", "showdown": "^1.9.1", "simple-html-tokenizer": "^0.5.7", diff --git a/packages/blocks/src/api/parser/get-block-attributes.js b/packages/blocks/src/api/parser/get-block-attributes.js index 35514d43a6900f..804e7f18ddd4ca 100644 --- a/packages/blocks/src/api/parser/get-block-attributes.js +++ b/packages/blocks/src/api/parser/get-block-attributes.js @@ -3,6 +3,7 @@ */ import { parse as hpqParse } from 'hpq'; import { flow, mapValues, castArray } from 'lodash'; +import memoize from 'memize'; /** * WordPress dependencies @@ -181,26 +182,6 @@ export function isValidByEnum( value, enumSet ) { return ! Array.isArray( enumSet ) || enumSet.includes( value ); } -/** - * Memoize one-parameter function using a WeakMap. - * - * @param {Function} fn Function to memoize. - * - * @return {Function} The same function memoized. - */ -function memoize( fn ) { - const cache = new WeakMap(); - - return function ( param ) { - let result = cache.get( param ); - if ( result === undefined ) { - result = fn( param ); - cache.set( param, result ); - } - return result; - }; -} - /** * Returns an hpq matcher given a source object. *