jenkins-bot has submitted this change and it was merged. (
https://gerrit.wikimedia.org/r/346822 )
Change subject: EventEmitter: Document disconnect() behaviour regarding array
matching
......................................................................
EventEmitter: Document disconnect() behaviour regarding array matching
It was already covered by test, but only in a way that showed parameters
were optional. Added a test case to show they are not only optional, they
are ignored when finding matches.
Bug: T100731
Change-Id: I07df0c1e2e0a964a70ad22957fb27018aedb4b1b
---
M src/EventEmitter.js
M tests/unit/EventEmitter.test.js
2 files changed, 35 insertions(+), 8 deletions(-)
Approvals:
Krinkle: Looks good to me, approved
jenkins-bot: Verified
Jforrester: Looks good to me, approved
diff --git a/src/EventEmitter.js b/src/EventEmitter.js
index 169f3a8..494f145 100644
--- a/src/EventEmitter.js
+++ b/src/EventEmitter.js
@@ -191,7 +191,7 @@
* @param
{Object.<string,string>|Object.<string,Function>|Object.<string,Array>} methods
List of
* event bindings keyed by event name containing either method names,
functions or arrays containing
* method name or function followed by a list of arguments to be
passed to callback before emitted
- * arguments
+ * arguments.
* @chainable
*/
oo.EventEmitter.prototype.connect = function ( context, methods ) {
@@ -217,8 +217,13 @@
*
* @param {Object} context Object to disconnect methods from
* @param
{Object.<string,string>|Object.<string,Function>|Object.<string,Array>}
[methods] List of
- * event bindings keyed by event name. Values can be either method
names or functions, but must be
- * consistent with those used in the corresponding call to "connect".
+ * event bindings keyed by event name. Values can be either method
names, functions or arrays
+ * containing a method name.
+ * NOTE: To allow matching call sites with connect(), array values are
allowed to contain the
+ * parameters as well, but only the method name is used to find
bindings. Tt is discouraged to
+ * have multiple bindings for the same event to the same listener, but
if used (and only the
+ * parameters vary), disconnecting one variation of (event name, event
listener, parameters)
+ * will disconnect other variations as well.
* @chainable
*/
oo.EventEmitter.prototype.disconnect = function ( context, methods ) {
diff --git a/tests/unit/EventEmitter.test.js b/tests/unit/EventEmitter.test.js
index dd28125..70a8387 100644
--- a/tests/unit/EventEmitter.test.js
+++ b/tests/unit/EventEmitter.test.js
@@ -253,30 +253,52 @@
QUnit.test( 'disconnect( host, array methods )', function ( assert ) {
var host,
- hits = { foo: 0, barbara: 0 },
+ hits = { foo: 0, barbara: 0, barbaraAlt: 0 },
ee = new oo.EventEmitter();
host = {
onFoo: function () {
hits.foo++;
},
- barbara: function () {
- hits.barbara++;
+ barbara: function ( param ) {
+ if ( param === 'alt' ) {
+ hits.barbaraAlt++;
+ } else {
+ hits.barbara++;
+ }
}
};
ee.connect( host, {
foo: 'onFoo',
- bar: [ 'barbara', 'some', 'parameter' ]
+ bar: [ 'barbara', 'regular' ]
} );
ee.emit( 'foo' );
ee.emit( 'bar' );
+ assert.deepEqual( hits, { foo: 1, barbara: 1, barbaraAlt: 0 } );
+ // disconnect finds "barbara" by method name, parameters not
needed
ee.disconnect( host, { bar: [ 'barbara' ] } );
ee.emit( 'foo' );
ee.emit( 'bar' );
+ assert.deepEqual( hits, { foo: 2, barbara: 1, barbaraAlt: 0 } );
- assert.deepEqual( hits, { foo: 2, barbara: 1 } );
+ ee.connect( host, {
+ bar: [ 'barbara', 'regular' ]
+ } );
+ ee.connect( host, {
+ bar: [ 'barbara', 'alt' ]
+ } );
+ ee.emit( 'bar' );
+ // both barbara's increase
+ assert.deepEqual( hits, { foo: 2, barbara: 2, barbaraAlt: 1 } );
+
+ // disconnect finds both "barbara" by method name, parameters
ignored
+ ee.disconnect( host, { bar: [ 'barbara', 'ignored' ] } );
+ ee.emit( 'bar' );
+
+ // foo increased, but barabara not (following disconnect of
both)
+ assert.deepEqual( hits, { foo: 2, barbara: 2, barbaraAlt: 1 } );
} );
QUnit.test( 'disconnect( host, unbound methods )', function ( assert ) {
--
To view, visit https://gerrit.wikimedia.org/r/346822
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I07df0c1e2e0a964a70ad22957fb27018aedb4b1b
Gerrit-PatchSet: 1
Gerrit-Project: oojs/core
Gerrit-Branch: master
Gerrit-Owner: Krinkle <[email protected]>
Gerrit-Reviewer: Catrope <[email protected]>
Gerrit-Reviewer: Jforrester <[email protected]>
Gerrit-Reviewer: Krinkle <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits