Skip to content
Browse files

For all cases, never register two identical functions twice.

Two functions are considered identical if they

 * compared with '==' operator and told to be eqaul
 * converted into string and are equal told by '==' operator



git-svn-id: http://code.handlino.com/svn/Ebisu/javascript@27 b19cb30a-9f2e-4084-8847-5e1a13269302
  • Loading branch information...
1 parent e12fc9a commit 38d5313516db187ed71be07cb49d23a16fb61df1 @gugod gugod committed Oct 3, 2007
Showing with 49 additions and 0 deletions.
  1. +6 −0 lib/Ebisu.js
  2. +43 −0 t/06.duplicated_handler.t.html
View
6 lib/Ebisu.js
@@ -81,6 +81,12 @@
if ( registry[e] == null )
registry[e] = [];
+ for ( var i = 0; i < registry[e].length ; i++) {
+ if (registry[e][i][0] == s && ( registry[e][i][1] == cb || registry[e][i][1].toString() == cb.toString() ) ) {
+ return this;
+ }
+ }
+
registry[e].push([s, cb]);
}
return this;
View
43 t/06.duplicated_handler.t.html
@@ -0,0 +1,43 @@
+<html>
+ <head>
+ <script src="lib/jsan/Test/Builder.js"></script>
+ <script src="lib/jsan/Test/More.js"></script>
+
+ <script src="../lib/jquery-1.2.js"></script>
+ <script src="../lib/Ebisu.js"></script>
+
+ </head>
+ <body>
+ <div id="e1"></div>
+ <div id="e2"></div>
+
+ <pre id="test">
+ <script type="text/javascript">
+
+plan({tests: 2});
+
+var i = 0;
+
+var set_foobar_handler = function() {
+ Ebisu("foobar").attach(function() { i = i + 1; });
+}
+
+set_foobar_handler();
+set_foobar_handler();
+
+Ebisu("foobar").trigger();
+
+is(i, 1, "var i is only incremented once.");
+
+i = 0;
+Ebisu("t2").attach(function() { i = i + 1; });
+Ebisu("t2").attach(function() { i = i + 1; });
+Ebisu("t2").attach(function() { i = i + 1; });
+Ebisu("t2").trigger();
+is(i, 1, "var i is only incremented once.");
+
+ </script>
+ </pre>
+ </body>
+</html>
+

0 comments on commit 38d5313

Please sign in to comment.
Something went wrong with that request. Please try again.