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

feat: allow duplicate registry values #7988

Merged
merged 1 commit into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
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
9 changes: 7 additions & 2 deletions core/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,13 @@ export function register<T>(
// 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 +
Expand Down
9 changes: 9 additions & 0 deletions tests/mocha/registry_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
Expand Down
Loading