Permalink
Browse files

improve remove() and has() to work with multiple contexts. closes #40

  • Loading branch information...
1 parent 34c9ae6 commit ad134845d6d66435828912bc1837813c538c6237 @millermedeiros committed Feb 2, 2012
View
@@ -1,6 +1,14 @@
# JS-Signals Changelog #
+## v0.7.3 (2012/02/02) ##
+
+### Fixes ###
+
+ - `remove()` and `has()` now accepts the `context` as second argument. (#40)
+
+
+
## v0.7.2 (2012/01/12) ##
### Fixes ###
View
@@ -1,3 +1,3 @@
#Build Number for ANT. Do not edit!
-#Thu Jan 12 22:39:15 BRST 2012
-build.number=249
+#Thu Feb 02 10:20:14 BRST 2012
+build.number=252
@@ -9,7 +9,7 @@ jsdoc-toolkit.dir = ${build.dir}/jsdoc-toolkit
jslint.jar = ${build.dir}/jslint4java/jslint4java-1.4.6.jar
product.name = signals
-version.number = 0.7.2
+version.number = 0.7.3
dist.name = ${product.name}.js
dist.min.name = ${product.name}.min.js
View
@@ -57,10 +57,10 @@
*/
_registerListener : function (listener, isOnce, listenerContext, priority) {
- var prevIndex = this._indexOfListener(listener),
+ var prevIndex = this._indexOfListener(listener, listenerContext),
binding;
- if (prevIndex !== -1 && this._bindings[prevIndex].context === listenerContext) {
+ if (prevIndex !== -1) {
binding = this._bindings[prevIndex];
if (binding.isOnce() !== isOnce) {
throw new Error('You cannot add'+ (isOnce? '' : 'Once') +'() then add'+ (!isOnce? '' : 'Once') +'() the same listener without removing the relationship first.');
@@ -93,10 +93,12 @@
* @return {number}
* @private
*/
- _indexOfListener : function (listener) {
- var n = this._bindings.length;
+ _indexOfListener : function (listener, context) {
+ var n = this._bindings.length,
+ cur;
while (n--) {
- if (this._bindings[n]._listener === listener) {
+ cur = this._bindings[n];
+ if (cur._listener === listener && cur.context === context) {
return n;
}
}
@@ -106,10 +108,11 @@
/**
* Check if listener was attached to Signal.
* @param {Function} listener
+ * @param {Object} [context]
* @return {boolean} if Signal has the specified listener.
*/
- has : function (listener) {
- return this._indexOfListener(listener) !== -1;
+ has : function (listener, context) {
+ return this._indexOfListener(listener, context) !== -1;
},
/**
@@ -139,12 +142,13 @@
/**
* Remove a single listener from the dispatch queue.
* @param {Function} listener Handler function that should be removed.
+ * @param {Object} [context] Execution context (since you can add the same handler multiple times if executing in a different context).
* @return {Function} Listener handler function.
*/
- remove : function (listener) {
+ remove : function (listener, context) {
validateListener(listener, 'remove');
- var i = this._indexOfListener(listener);
+ var i = this._indexOfListener(listener, context);
if (i !== -1) {
this._bindings[i]._destroy(); //no reason to a SignalBinding exist if it isn't attached to a signal
this._bindings.splice(i, 1);
View
@@ -92,7 +92,7 @@
* @return {Function|null} Handler function bound to the signal or `null` if binding was previously detached.
*/
detach : function () {
- return this.isBound()? this._signal.remove(this._listener) : null;
+ return this.isBound()? this._signal.remove(this._listener, this.context) : null;
},
/**
View
@@ -1228,6 +1228,65 @@ YUI().use('node', 'console', 'test', function (Y){
s.dispatch();
},
+ testRemoveDiffContext : function(){
+ var s = this.signal;
+
+ var l1 = function(){Y.Assert.fail();};
+ var obj = {};
+
+ var b1 = s.add(l1);
+ var b2 = s.add(l1, obj);
+ Y.Assert.areSame(2, s.getNumListeners());
+
+ Y.Assert.isUndefined(b1.context);
+ Y.Assert.areSame(l1, b1.getListener());
+ Y.Assert.areSame(obj, b2.context);
+ Y.Assert.areSame(l1, b2.getListener());
+
+ s.remove(l1, obj);
+
+ Y.Assert.isUndefined(b2.context);
+ Y.Assert.isUndefined(b2.getListener());
+
+ Y.Assert.isUndefined(b1.context);
+ Y.Assert.areSame(l1, b1.getListener());
+
+ Y.Assert.areSame(1, s.getNumListeners());
+ s.remove(l1);
+ Y.Assert.areSame(0, s.getNumListeners());
+ s.dispatch();
+ },
+
+ testRemoveDiffContext2 : function(){
+ var s = this.signal;
+
+ var l1 = function(){Y.Assert.fail();};
+ var obj = {};
+
+ var b1 = s.add(l1);
+ var b2 = s.add(l1, obj);
+ Y.Assert.areSame(2, s.getNumListeners());
+
+ Y.Assert.isUndefined(b1.context);
+ Y.Assert.areSame(l1, b1.getListener());
+ Y.Assert.areSame(obj, b2.context);
+ Y.Assert.areSame(l1, b2.getListener());
+
+ s.remove(l1);
+
+ Y.Assert.isUndefined(b1.context);
+ Y.Assert.isUndefined(b1.getListener());
+
+ Y.Assert.areSame(obj, b2.context);
+ Y.Assert.areSame(l1, b2.getListener());
+
+ Y.Assert.areSame(1, s.getNumListeners());
+ s.remove(l1, obj);
+ Y.Assert.areSame(0, s.getNumListeners());
+ s.dispatch();
+ },
+
+
//----------------- Memorize ----------------------------//
testMemorize : function(){
View
@@ -61,7 +61,7 @@ <h1 class="classTitle">File Index</h1>
</div>
<div class="fineprint" style="clear:both;text-align:center">
- Documentation generated by <a href="http://code.google.com/p/jsdoc-toolkit/" target="_blankt">JsDoc Toolkit</a> 2.4.0 on Thu Jan 12 2012 22:39:23 GMT-0200 (BRST)
+ Documentation generated by <a href="http://code.google.com/p/jsdoc-toolkit/" target="_blankt">JsDoc Toolkit</a> 2.4.0 on Thu Feb 02 2012 10:20:21 GMT-0200 (BRST)
| template based on Steffen Siering <a href="http://github.com/urso/jsdoc-simple">jsdoc-simple</a>.
</div>
</body>
View
@@ -85,7 +85,7 @@ <h1 class="classTitle">Class Index</h1>
</div>
<div class="fineprint" style="clear:both;text-align:center">
- Documentation generated by <a href="http://code.google.com/p/jsdoc-toolkit/" target="_blankt">JsDoc Toolkit</a> 2.4.0 on Thu Jan 12 2012 22:39:23 GMT-0200 (BRST)
+ Documentation generated by <a href="http://code.google.com/p/jsdoc-toolkit/" target="_blankt">JsDoc Toolkit</a> 2.4.0 on Thu Feb 02 2012 10:20:21 GMT-0200 (BRST)
| template based on Steffen Siering <a href="http://github.com/urso/jsdoc-simple">jsdoc-simple</a>.
</div>
</body>
@@ -314,7 +314,7 @@ <h1 id="classTitle">Symbol Index</h1>
</div>
<div class="fineprint" style="clear:both;text-align:center">
- Documentation generated by <a href="http://code.google.com/p/jsdoc-toolkit/" target="_blankt">JsDoc Toolkit</a> 2.4.0 on Thu Jan 12 2012 22:39:23 GMT-0200 (BRST)
+ Documentation generated by <a href="http://code.google.com/p/jsdoc-toolkit/" target="_blankt">JsDoc Toolkit</a> 2.4.0 on Thu Feb 02 2012 10:20:21 GMT-0200 (BRST)
| template based on Steffen Siering <a href="http://github.com/urso/jsdoc-simple">jsdoc-simple</a>.
</div>
</body>
@@ -101,7 +101,7 @@ <h1 class="classTitle">
<!-- ============================== footer ================================= -->
<div class="fineprint" style="clear:both;text-align:center">
- Documentation generated by <a href="http://code.google.com/p/jsdoc-toolkit/" target="_blankt">JsDoc Toolkit</a> 2.4.0 on Thu Jan 12 2012 22:39:23 GMT-0200 (BRST)
+ Documentation generated by <a href="http://code.google.com/p/jsdoc-toolkit/" target="_blankt">JsDoc Toolkit</a> 2.4.0 on Thu Feb 02 2012 10:20:20 GMT-0200 (BRST)
| template based on Steffen Siering <a href="http://github.com/urso/jsdoc-simple">jsdoc-simple</a>.
</div>
</body>
@@ -547,7 +547,7 @@ <h1 class="classTitle">
<div class="modifiers">
</div>
<span class="light">{boolean}</span>
- <b>has</b>(listener)
+ <b>has</b>(listener, context)
</div>
<div class="description">
@@ -568,6 +568,12 @@ <h1 class="classTitle">
</dt>
<dd></dd>
+ <dt>
+ <span class="light fixedFont">{Object}</span> <b>context</b>
+ <i>Optional</i>
+ </dt>
+ <dd></dd>
+
</dl>
@@ -592,7 +598,7 @@ <h1 class="classTitle">
<div class="modifiers">
</div>
<span class="light">{Function}</span>
- <b>remove</b>(listener)
+ <b>remove</b>(listener, context)
</div>
<div class="description">
@@ -613,6 +619,12 @@ <h1 class="classTitle">
</dt>
<dd>Handler function that should be removed.</dd>
+ <dt>
+ <span class="light fixedFont">{Object}</span> <b>context</b>
+ <i>Optional</i>
+ </dt>
+ <dd>Execution context (since you can add the same handler multiple times if executing in a different context).</dd>
+
</dl>
@@ -704,7 +716,7 @@ <h1 class="classTitle">
<!-- ============================== footer ================================= -->
<div class="fineprint" style="clear:both;text-align:center">
- Documentation generated by <a href="http://code.google.com/p/jsdoc-toolkit/" target="_blankt">JsDoc Toolkit</a> 2.4.0 on Thu Jan 12 2012 22:39:23 GMT-0200 (BRST)
+ Documentation generated by <a href="http://code.google.com/p/jsdoc-toolkit/" target="_blankt">JsDoc Toolkit</a> 2.4.0 on Thu Feb 02 2012 10:20:20 GMT-0200 (BRST)
| template based on Steffen Siering <a href="http://github.com/urso/jsdoc-simple">jsdoc-simple</a>.
</div>
</body>
@@ -526,7 +526,7 @@ <h1 class="classTitle">
<!-- ============================== footer ================================= -->
<div class="fineprint" style="clear:both;text-align:center">
- Documentation generated by <a href="http://code.google.com/p/jsdoc-toolkit/" target="_blankt">JsDoc Toolkit</a> 2.4.0 on Thu Jan 12 2012 22:39:23 GMT-0200 (BRST)
+ Documentation generated by <a href="http://code.google.com/p/jsdoc-toolkit/" target="_blankt">JsDoc Toolkit</a> 2.4.0 on Thu Feb 02 2012 10:20:20 GMT-0200 (BRST)
| template based on Steffen Siering <a href="http://github.com/urso/jsdoc-simple">jsdoc-simple</a>.
</div>
</body>
@@ -179,7 +179,7 @@ <h1 class="classTitle">
<!-- ============================== footer ================================= -->
<div class="fineprint" style="clear:both;text-align:center">
- Documentation generated by <a href="http://code.google.com/p/jsdoc-toolkit/" target="_blankt">JsDoc Toolkit</a> 2.4.0 on Thu Jan 12 2012 22:39:23 GMT-0200 (BRST)
+ Documentation generated by <a href="http://code.google.com/p/jsdoc-toolkit/" target="_blankt">JsDoc Toolkit</a> 2.4.0 on Thu Feb 02 2012 10:20:20 GMT-0200 (BRST)
| template based on Steffen Siering <a href="http://github.com/urso/jsdoc-simple">jsdoc-simple</a>.
</div>
</body>
Oops, something went wrong.

0 comments on commit ad13484

Please sign in to comment.