From 6767717d0bf53ed93799a39728bf1677a3d26934 Mon Sep 17 00:00:00 2001 From: Maribeth Bottorff Date: Mon, 1 Apr 2024 14:13:25 -0700 Subject: [PATCH] feat: allow duplicate registry values (#7988) --- core/registry.ts | 9 +++++++-- tests/mocha/registry_test.js | 9 +++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/core/registry.ts b/core/registry.ts index 3645a6fdf..727711d48 100644 --- a/core/registry.ts +++ b/core/registry.ts @@ -162,8 +162,13 @@ export function register( // Validate that the given class has all the required properties. validate(type, registryItem); - // Don't throw an error if opt_allowOverrides is true. - if (!opt_allowOverrides && typeRegistry[caselessName]) { + // Don't throw an error if opt_allowOverrides is true, + // or if we're trying to register the same item. + if ( + !opt_allowOverrides && + typeRegistry[caselessName] && + typeRegistry[caselessName] !== registryItem + ) { throw Error( 'Name "' + caselessName + diff --git a/tests/mocha/registry_test.js b/tests/mocha/registry_test.js index fd37c6e69..b562606f4 100644 --- a/tests/mocha/registry_test.js +++ b/tests/mocha/registry_test.js @@ -47,6 +47,15 @@ suite('Registry', function () { test('Overwrite a Key', function () { Blockly.registry.register('test', 'test_name', TestClass); chai.assert.throws(function () { + // Registers a different object under the same name + Blockly.registry.register('test', 'test_name', {}); + }, 'already registered'); + }); + + test('Register a Duplicate Item', function () { + Blockly.registry.register('test', 'test_name', TestClass); + chai.assert.doesNotThrow(function () { + // Registering the same object under the same name is allowed Blockly.registry.register('test', 'test_name', TestClass); }, 'already registered'); });