Permalink
Browse files

Merge branch 'release-0.1'

  • Loading branch information...
2 parents 3d3638f + 5a6c705 commit ce5106e27f1193c594f13de91bd9fe108ddb7e36 @millermedeiros committed Nov 26, 2010
View
@@ -1 +1,53 @@
-Custom event/messaging system for JavaScript inspired by Robert Penner's [AS3-Signals](https://github.com/robertpenner/as3-signals).
+
+# JS-Signals #
+
+Custom event/messaging system for JavaScript inspired by [AS3-Signals](https://github.com/robertpenner/as3-signals).
+
+## Introduction ##
+
+A Signal is similar to a EventTarget/EventDispatcher or a pub/sub system, the main difference is that each event kind has it's own controller and doesn't rely on strings to call proper callbacks.
+
+Another advantage is that you can pass arbitrary parameters to callbacks and it also have some convenience methods that aren't present on other *observer pattern* implementations.
+
+
+## Examples ##
+
+ //store local reference for brevity
+ var Signal = signals.Signal;
+
+ //custom object that dispatch signals
+ var myObject = {
+ started : new Signal(),
+ stopped : new Signal()
+ };
+
+ function onStarted(param1, param2){
+ alert(param1 + param2);
+ }
+
+ //add listener
+ myObject.started.add(onStarted);
+
+ //dispatch signal passing custom parameters
+ myObject.started.dispatch('foo', 'bar');
+
+ //remove a single listener
+ myObject.started.remove(onStarted);
+
+ function onStopped(){
+ alert('stopped');
+ }
+
+ function onStopped2(){
+ alert('stopped listener 2');
+ }
+
+ //add listeners
+ myObject.stopped.add(onStopped);
+ myObject.stopped.add(onStopped2);
+
+ //dispatch signal
+ myObject.stopped.dispatch();
+
+ //remove all listeners of the `stopped` signal
+ myObject.stopped.removeAll();
View
@@ -28,9 +28,8 @@
<concat destfile="${dist.dir}/${dist.name}" fixlastline="yes" eol="unix">
<filelist dir="${src.dir}">
<file name="intro.js" />
- <file name="Signal.js" />
- <file name="SignalBinding.js" />
- <file name="SignalBindingList.js" />
+ <file name="Signal.js" />
+ <file name="SignalBinding.js" />
<file name="outro.js" />
</filelist>
</concat>
View
@@ -1,3 +1,3 @@
#Build Number for ANT. Do not edit!
-#Wed Nov 24 01:58:14 EST 2010
-build.number=15
+#Fri Nov 26 03:47:05 EST 2010
+build.number=30
View
@@ -2,36 +2,51 @@
/**
* Simple Signal "Class" inpired by Robert Penner's AS3Signals <https://github.com/robertpenner/as3-signals/>
* @author Miller Medeiros
- * @version 0.1 (2010/11/24)
* @constructor
*/
signals.Signal = function(){
/**
* Event Handlers
- * @type signals.BindingList
+ * @type Array.<SignalBinding>
+ * @private
*/
- this._bindingList = new signals.SignalBindingList();
+ this._bindings = [];
};
signals.Signal.prototype = {
_registerListener : function(listener, isOnce, scope){
- var list = this._bindingList,
- prevIndex = list.indexOfListener(listener),
+ var prevBinding,
+ prevIndex = this._indexOfListener(listener),
binding;
if(prevIndex != -1){ //avoid creating a new Binding if already added to list
- //TODO: throw errors based on isOnce
- binding = list[prevIndex];
+ prevBinding = this._bindings[prevIndex];
+
+ if(prevBinding.isOnce() && !isOnce){
+ throw new Error('You cannot addOnce() then add() the same listener without removing the relationship first.');
+ }else if(!prevBinding.isOnce() && isOnce){
+ throw new Error('You cannot add() then addOnce() the same listener without removing the relationship first.');
+ }
+
+ binding = prevBinding;
} else {
binding = new signals.SignalBinding(listener, isOnce, scope, this);
- list.add(binding);
+ this._bindings.push(binding);
}
return binding;
},
+ _indexOfListener : function _indexOfListener(listener){
+ var n = this._bindings.length;
+ while(n--){
+ if(this._bindings[n].listener === listener) return n;
+ }
+ return -1;
+ },
+
add : function(listener, scope){
return this._registerListener(listener, false, scope);
},
@@ -41,21 +56,30 @@
},
remove : function remove(listener){
- this._bindingList.removeByListener(listener);
+ var i = this._indexOfListener(listener);
+ if(i != -1){
+ this._bindings.splice(i, 1);
+ }
return listener;
},
removeAll : function removeAll(){
- this._bindingList.removeAll();
+ this._bindings.length = 0;
},
getNumListeners : function getNumListeners(){
- return this._bindingList.length;
+ return this._bindings.length;
},
dispatch : function(){
- var params = Array.prototype.slice.call(arguments);
- this._bindingList.execute(params);
+ var paramsArr = Array.prototype.slice.call(arguments),
+ i = 0,
+ bindings = this._bindings.slice(), //clone array in case add/remove items during dispatch
+ cur;
+ while(cur = bindings[i++]){
+ cur.execute(paramsArr);
+ }
+
},
toString : function toString(){
View
@@ -1,9 +1,8 @@
/**
* Class that represents a Signal Binding
- * - inspired on Joa Ebert AS3 Signal Binding and Robert Penner's Slot
+ * - inspired by Joa Ebert AS3 SignalBinding and Robert Penner's Slot classes.
* @author Miller Medeiros
- * @version 0.1 (2010/11/24)
* @constructor
*/
signals.SignalBinding = function SignalBinding(listener, isOnce, listenerScope, signal){
@@ -20,7 +19,7 @@
execute : function execute(paramsArr){
if(! this._isPaused){
- if(this._isOnce) this.remove();
+ if(this._isOnce) this._signal.remove(this.listener);
this.listener.apply(this.listenerScope, paramsArr);
}
},
@@ -41,10 +40,6 @@
return this._isOnce;
},
- remove : function remove(){
- this._signal.remove(this.listener);
- },
-
toString : function toString(){
return '[SignalBinding listener: '+ this.listener +', isOnce: '+ this._isOnce +', isPaused: '+ this._isPaused +', listenerScope: '+ this.listenerScope +']';
}
@@ -1,51 +0,0 @@
-
- /**
- * Object representing a Collection of SignalBinds (Pseudo-Array Object).
- * - name inspired by Joa Ebert AS3 SignalBindingList but with complete different implementation.
- * @author Miller Medeiros
- * @constructor
- * @private
- * @version 0.1 (2010/11/24)
- */
- signals.SignalBindingList = function SignalBindingList(){};
-
-
- signals.SignalBindingList.prototype = {
-
- length : 0,
-
- add : function add(binding){
- Array.prototype.push.call(this, binding);
- },
-
- execute : function execute(paramsArr){
- var i,
- n = this.length;
- for(i = 0; i < n; i++){
- this[i].execute(paramsArr);
- }
- },
-
- indexOfListener : function indexOfListener(listener){
- var n = this.length;
- while(n--){
- if(this[n].listener === listener) return n;
- }
- return -1;
- },
-
- removeByListener : function removeByListener(listener){
- var i = this.indexOfListener(listener);
- if(i != -1){
- delete this[i];
- this.length--;
- }
- },
-
- removeAll : function removeAll(){
- while(this.length--){
- delete this[this.length];
- }
- }
-
- };
View
@@ -5,16 +5,13 @@
<meta http-equiv="content-style-type" content="text/css" />
<title>js-signals unit test</title>
<link type="text/css" rel="stylesheet" href="http://yui.yahooapis.com/3.2.0/build/cssfonts/fonts-min.css" />
- <link type="text/css" rel="stylesheet" href="./style/main.css" />
</head>
<body>
<div id="wrapper">
<div id="testLogger" class="yui3-skin-sam"></div>
</div>
<script type="text/javascript" src="http://yui.yahooapis.com/3.2.0/build/yui/yui-min.js"></script>
- <script type="text/javascript" src="./lib/yuitest-min.js"></script>
<script type="text/javascript" src="../../dist/js-signals.js"></script>
<script type="text/javascript" src="./src/01.js"></script>
- <script type="text/javascript" src="./src/TestRunner.js"></script>
</body>
</html>
Oops, something went wrong.
Oops, something went wrong.

0 comments on commit ce5106e

Please sign in to comment.