Esanders has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/206371

Change subject: Provide an unregister method for registries
......................................................................

Provide an unregister method for registries

Change-Id: Id9a5b3ecd7dc10bcb6d9a2c37a5adfa29810a913
---
M src/Factory.js
M src/Registry.js
M tests/unit/Factory.test.js
M tests/unit/Registry.test.js
4 files changed, 74 insertions(+), 2 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/oojs/core refs/changes/71/206371/1

diff --git a/src/Factory.js b/src/Factory.js
index 1f23293..e9972ed 100644
--- a/src/Factory.js
+++ b/src/Factory.js
@@ -5,6 +5,7 @@
  * @constructor
  */
 oo.Factory = function OoFactory() {
+       // Parent constructor
        oo.Factory.parent.call( this );
 
        // Properties
@@ -45,10 +46,33 @@
        }
        this.entries.push( name );
 
+       // Parent method
        oo.Factory.parent.prototype.register.call( this, name, constructor );
 };
 
 /**
+ * Unegister a constructor from the factory.
+ *
+ * @param {Function} constructor Constructor to unregister
+ * @throws {Error} Name must be a string and must not be empty
+ * @throws {Error} Constructor must be a function
+ */
+oo.Factory.prototype.unregister = function ( constructor ) {
+       var name;
+
+       if ( typeof constructor !== 'function' ) {
+               throw new Error( 'constructor must be a function, cannot be a ' 
+ typeof constructor );
+       }
+       name = constructor.static && constructor.static.name;
+       if ( typeof name !== 'string' || name === '' ) {
+               throw new Error( 'Name must be a string and must not be empty' 
);
+       }
+
+       // Parent method
+       oo.Factory.parent.prototype.unregister.call( this, name, constructor );
+};
+
+/**
  * Create an object based on a name.
  *
  * Name is used to look up the constructor to use, while all additional 
arguments are passed to the
diff --git a/src/Registry.js b/src/Registry.js
index fb0a6da..b821bde 100644
--- a/src/Registry.js
+++ b/src/Registry.js
@@ -26,6 +26,12 @@
  * @param {Mixed} data
  */
 
+/**
+ * @event unregister
+ * @param {string} name
+ * @param {Mixed} data Data removed from registry
+ */
+
 /* Methods */
 
 /**
@@ -53,6 +59,28 @@
 };
 
 /**
+ * Remove one or more symbolic names from the registry
+ *
+ * @param {string|string[]} name Symbolic name or list of symbolic names
+ * @fires unregister
+ * @throws {Error} Name argument must be a string or array
+ */
+oo.Registry.prototype.unregister = function ( name ) {
+       var i, len, data;
+       if ( typeof name === 'string' ) {
+               data = this.lookup( name );
+               delete this.registry[name];
+               this.emit( 'unregister', name, data );
+       } else if ( Array.isArray( name ) ) {
+               for ( i = 0, len = name.length; i < len; i++ ) {
+                       this.unregister( name[i] );
+               }
+       } else {
+               throw new Error( 'Name must be a string or array, cannot be a ' 
+ typeof name );
+       }
+};
+
+/**
  * Get data for a given symbolic name.
  *
  * @param {string} name Symbolic name
diff --git a/tests/unit/Factory.test.js b/tests/unit/Factory.test.js
index faa9b23..fc33eb0 100644
--- a/tests/unit/Factory.test.js
+++ b/tests/unit/Factory.test.js
@@ -17,7 +17,7 @@
 
        /* Tests */
 
-       QUnit.test( 'register', 2, function ( assert ) {
+       QUnit.test( 'register/unregister', 4, function ( assert ) {
                var factory = new oo.Factory();
                assert.throws(
                        function () {
@@ -29,6 +29,17 @@
 
                factory.register( oo.FactoryObjectStub );
                assert.strictEqual( factory.lookup( 'factory-object-stub' ), 
oo.FactoryObjectStub );
+
+               assert.throws(
+                       function () {
+                               factory.unregister( 'not-a-function' );
+                       },
+                       Error,
+                       'Throws an exception when trying to unregister a 
non-function value as a constructor'
+               );
+
+               factory.unregister( oo.FactoryObjectStub );
+               assert.strictEqual( factory.lookup( 'factory-object-stub' ), 
undefined );
        } );
 
        QUnit.test( 'create', 3, function ( assert ) {
diff --git a/tests/unit/Registry.test.js b/tests/unit/Registry.test.js
index efdca80..91a70c0 100644
--- a/tests/unit/Registry.test.js
+++ b/tests/unit/Registry.test.js
@@ -2,7 +2,7 @@
 
        QUnit.module( 'Registry' );
 
-       QUnit.test( 'register', 3, function ( assert ) {
+       QUnit.test( 'register/unregister', 7, function ( assert ) {
                var registry = new oo.Registry();
 
                registry.register( 'registry-item-1', 1 );
@@ -11,6 +11,15 @@
                assert.strictEqual( registry.lookup( 'registry-item-1' ), 1 );
                assert.strictEqual( registry.lookup( 'registry-item-2' ), 23 );
                assert.strictEqual( registry.lookup( 'registry-item-3' ), 23 );
+
+               registry.unregister( 'registry-item-1', 1 );
+               assert.strictEqual( registry.lookup( 'registry-item-1' ), 
undefined );
+               assert.strictEqual( registry.lookup( 'registry-item-2' ), 23 );
+
+               registry.unregister( [ 'registry-item-2', 'registry-item-3' ], 
23 );
+               assert.strictEqual( registry.lookup( 'registry-item-2' ), 
undefined );
+               assert.strictEqual( registry.lookup( 'registry-item-3' ), 
undefined );
+
        } );
 
        QUnit.test( 'lookup', 6, function ( assert ) {

-- 
To view, visit https://gerrit.wikimedia.org/r/206371
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Id9a5b3ecd7dc10bcb6d9a2c37a5adfa29810a913
Gerrit-PatchSet: 1
Gerrit-Project: oojs/core
Gerrit-Branch: master
Gerrit-Owner: Esanders <esand...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to