From 22e79ae49617e7fe655a46f473e564396ee200f5 Mon Sep 17 00:00:00 2001 From: Monica Kozbial Date: Thu, 26 Sep 2019 13:23:45 -0700 Subject: [PATCH] Updating behavior of register functions and adding unregister. (#3085) * Updating behavior of register functions and adding unregister. * Updates jsdoc comments. --- core/extensions.js | 13 +++++++++++++ core/field_registry.js | 23 ++++++++++++++++++++--- core/renderers/common/block_rendering.js | 13 +++++++++++++ tests/mocha/field_registry_test.js | 6 +++--- 4 files changed, 49 insertions(+), 6 deletions(-) diff --git a/core/extensions.js b/core/extensions.js index 938f7c684..1f19a087d 100644 --- a/core/extensions.js +++ b/core/extensions.js @@ -128,6 +128,19 @@ Blockly.Extensions.registerMutator = function(name, mixinObj, opt_helperFn, }); }; +/** + * Unregisters the extension registered with the given name. + * @param {string} name The name of the extension to unregister. + */ +Blockly.Extensions.unregister = function(name) { + if (Blockly.Extensions.ALL_[name]) { + Blockly.Extensions.ALL_[name] = undefined; + } else { + console.warn('No extension mapping for name "' + name + + '" found to unregister'); + } +}; + /** * Applies an extension method to a block. This should only be called during * block construction. diff --git a/core/field_registry.js b/core/field_registry.js index 7556bd0d6..c20be98b7 100644 --- a/core/field_registry.js +++ b/core/field_registry.js @@ -38,20 +38,24 @@ goog.provide('Blockly.fieldRegistry'); Blockly.fieldRegistry.typeMap_ = {}; /** - * Registers a field type. May also override an existing field type. + * Registers a field type. * Blockly.fieldRegistry.fromJson uses this registry to * find the appropriate field type. * @param {string} type The field type name as used in the JSON definition. * @param {!{fromJson: Function}} fieldClass The field class containing a * fromJson function that can construct an instance of the field. - * @throws {Error} if the type name is empty, or the fieldClass is not an - * object containing a fromJson function. + * @throws {Error} if the type name is empty, the field is already + * registered, or the fieldClass is not an object containing a fromJson + * function. */ Blockly.fieldRegistry.register = function(type, fieldClass) { if ((typeof type != 'string') || (type.trim() == '')) { throw Error('Invalid field type "' + type + '". The type must be a' + ' non-empty string.'); } + if (Blockly.fieldRegistry.typeMap_[type]) { + throw Error('Error: Field "' + type + '" is already registered.'); + } if (!fieldClass || (typeof fieldClass.fromJson != 'function')) { throw Error('Field "' + fieldClass + '" must have a fromJson function'); } @@ -59,6 +63,19 @@ Blockly.fieldRegistry.register = function(type, fieldClass) { Blockly.fieldRegistry.typeMap_[type] = fieldClass; }; +/** + * Unregisters the field registered with the given type. + * @param {string} type The field type name as used in the JSON definition. + */ +Blockly.fieldRegistry.unregister = function(type) { + if (Blockly.fieldRegistry.typeMap_[type]) { + Blockly.fieldRegistry.typeMap_[type] = undefined; + } else { + console.warn('No field mapping for type "' + type + + '" found to unregister'); + } +}; + /** * Construct a Field from a JSON arg object. * Finds the appropriate registered field by the type name as registered using diff --git a/core/renderers/common/block_rendering.js b/core/renderers/common/block_rendering.js index 353936e08..f476d1400 100644 --- a/core/renderers/common/block_rendering.js +++ b/core/renderers/common/block_rendering.js @@ -61,6 +61,19 @@ Blockly.blockRendering.register = function(name, rendererClass) { Blockly.blockRendering.rendererMap_[name] = rendererClass; }; +/** + * Unregisters the renderer registered with the given name. + * @param {string} name The name of the renderer. + */ +Blockly.blockRendering.unregister = function(name) { + if (Blockly.blockRendering.rendererMap_[name]) { + Blockly.blockRendering.rendererMap_[name] = undefined; + } else { + console.warn('No renderer mapping for name "' + name + + '" found to unregister'); + } +}; + /** * Turn on the blocks debugger. * @package diff --git a/tests/mocha/field_registry_test.js b/tests/mocha/field_registry_test.js index 7f636f540..7f1e4574e 100644 --- a/tests/mocha/field_registry_test.js +++ b/tests/mocha/field_registry_test.js @@ -57,11 +57,11 @@ suite('Field Registry', function() { Blockly.fieldRegistry.register(CustomFieldType.fromJson, ''); }, 'Invalid field type'); }); - // TODO (#2788): What do you want it to do if you overwrite a key? test('Overwrite a Key', function() { Blockly.fieldRegistry.register('field_custom_test', CustomFieldType); - - Blockly.fieldRegistry.register('field_custom_test', CustomFieldType); + chai.assert.throws(function() { + Blockly.fieldRegistry.register('field_custom_test', CustomFieldType); + }, 'already registered'); }); test('Null Value', function() { chai.assert.throws(function() {