Skip to content
This repository
Browse code

Merge pull request #5 from mahemoff/master

Add reset functionality
  • Loading branch information...
commit d9b7c93ccc018e437ae98a6cf83652682ba0cfd2 2 parents 0ca8c53 + 2014193
Scott Murphy authored June 22, 2012

Showing 1 changed file with 139 additions and 128 deletions. Show diff stats Hide diff stats

  1. 267  radio.js
267  radio.js
@@ -26,140 +26,151 @@
26 26
  OTHER DEALINGS IN THE SOFTWARE.
27 27
  */
28 28
 (function (name, global, definition) {
29  
-	if (typeof module !== 'undefined') module.exports = definition(name, global);
30  
-	else if (typeof define === 'function' && typeof define.amd  === 'object') define(definition);
31  
-	else global[name] = definition(name, global);
  29
+  if (typeof module !== 'undefined') module.exports = definition(name, global);
  30
+  else if (typeof define === 'function' && typeof define.amd  === 'object') define(definition);
  31
+  else global[name] = definition(name, global);
32 32
 })('radio', this, function (name, global) {
33 33
 
34  
-	"use strict";
  34
+  "use strict";
35 35
 
36  
-	/**
37  
-	 * Main Wrapper for radio.$ and create a function radio to accept the channelName
38  
-	 * @param {String} channelName topic of event
39  
-	 */
40  
-	function radio(channelName) {
41  
-		radio.$.channel(channelName);
42  
-		return radio.$;
43  
-	}
  36
+  /**
  37
+   * Main Wrapper for radio.$ and create a function radio to accept the channelName
  38
+   * @param {String} channelName topic of event
  39
+   */
  40
+  function radio(channelName) {
  41
+    arguments.length ? radio.$.channel(channelName) : radio.$.reset();
  42
+    return radio.$;
  43
+  }
44 44
 
45  
-	radio.$ = {
46  
-		version: '0.2',
47  
-		channelName: "",
48  
-		channels: [],
49  
-		/**
50  
-		 * Broadcast (publish)
51  
-		 * Iterate through all listeners (callbacks) in current channel and pass arguments to subscribers
52  
-		 * @param arguments data to be sent to listeners
53  
-		 * @example
54  
-		 *    //basic usage
55  
-		 *    radio('channel1').broadcast('my message'); 
56  
-		 *    //send an unlimited number of parameters
57  
-		 *    radio('channel2').broadcast(param1, param2, param3 ... );
58  
-		 */
59  
-		broadcast: function() {
60  
-			var i, c = this.channels[this.channelName],
61  
-				l = c.length,
62  
-				subscriber, callback, context;
63  
-			//iterate through current channel and run each subscriber
64  
-			for (i = 0; i < l; i++) {
65  
-				subscriber = c[i];
66  
-				//if subscriber was an array, set the callback and context.
67  
-				if ((typeof(subscriber) === 'object') && (subscriber.length)) {
68  
-					callback = subscriber[0];
69  
-					//if user set the context, set it to the context otherwise, it is a globally scoped function
70  
-					context = subscriber[1] || global;
71  
-				}
72  
-				callback.apply(context, arguments);
73  
-			}
74  
-			return this;
75  
-		},
  45
+  radio.$ = {
  46
+    version: '0.2',
  47
+    channelName: "",
  48
+    channels: [],
76 49
 
77  
-		/**
78  
-		 * Create the channel if it doesn't exist and set the current channel/event name
79  
-		 * @param {String} name the name of the channel
80  
-		 * @example
81  
-		 *    radio('channel1');
82  
-		 */
83  
-		channel: function(name) {
84  
-			var c = this.channels;
85  
-			//create a new channel if it doesn't exists
86  
-			if (!c[name]) c[name] = [];
87  
-			this.channelName = name;
88  
-			return this;
89  
-		},
  50
+    /**
  51
+     * Reset global state, by removing all channels
  52
+     * @example
  53
+     *    radio()
  54
+     */
  55
+    reset: function() {
  56
+      radio.$.channelName = "";
  57
+      radio.$.channels = [];
  58
+    },
  59
+ 
  60
+    /**
  61
+     * Broadcast (publish)
  62
+     * Iterate through all listeners (callbacks) in current channel and pass arguments to subscribers
  63
+     * @param arguments data to be sent to listeners
  64
+     * @example
  65
+     *    //basic usage
  66
+     *    radio('channel1').broadcast('my message'); 
  67
+     *    //send an unlimited number of parameters
  68
+     *    radio('channel2').broadcast(param1, param2, param3 ... );
  69
+     */
  70
+    broadcast: function() {
  71
+      var i, c = this.channels[this.channelName],
  72
+        l = c.length,
  73
+        subscriber, callback, context;
  74
+      //iterate through current channel and run each subscriber
  75
+      for (i = 0; i < l; i++) {
  76
+        subscriber = c[i];
  77
+        //if subscriber was an array, set the callback and context.
  78
+        if ((typeof(subscriber) === 'object') && (subscriber.length)) {
  79
+          callback = subscriber[0];
  80
+          //if user set the context, set it to the context otherwise, it is a globally scoped function
  81
+          context = subscriber[1] || global;
  82
+        }
  83
+        callback.apply(context, arguments);
  84
+      }
  85
+      return this;
  86
+    },
  87
+
  88
+    /**
  89
+     * Create the channel if it doesn't exist and set the current channel/event name
  90
+     * @param {String} name the name of the channel
  91
+     * @example
  92
+     *    radio('channel1');
  93
+     */
  94
+    channel: function(name) {
  95
+      var c = this.channels;
  96
+      //create a new channel if it doesn't exists
  97
+      if (!c[name]) c[name] = [];
  98
+      this.channelName = name;
  99
+      return this;
  100
+    },
90 101
 
91  
-		/**
92  
-		 * Add Subscriber to channel
93  
-		 * Take the arguments and add it to the this.channels array.
94  
-		 * @param {Function|Array} arguments list of callbacks or arrays[callback, context] separated by commas
95  
-		 * @example
96  
-		 *      //basic usage
97  
-		 *      var callback = function() {};
98  
-		 *      radio('channel1').subscribe(callback); 
99  
-		 *
100  
-		 *      //subscribe an endless amount of callbacks
101  
-		 *      radio('channel1').subscribe(callback, callback2, callback3 ...);
102  
-		 *
103  
-		 *      //adding callbacks with context
104  
-		 *      radio('channel1').subscribe([callback, context],[callback1, context], callback3);
105  
-		 *     
106  
-		 *      //subscribe by chaining
107  
-		 *      radio('channel1').subscribe(callback).radio('channel2').subscribe(callback).subscribe(callback2);
108  
-		 */
109  
-		subscribe: function() {
110  
-			var a = arguments,
111  
-				c = this.channels[this.channelName],
112  
-				i, l = a.length,
113  
-				p, ai = [];
  102
+    /**
  103
+     * Add Subscriber to channel
  104
+     * Take the arguments and add it to the this.channels array.
  105
+     * @param {Function|Array} arguments list of callbacks or arrays[callback, context] separated by commas
  106
+     * @example
  107
+     *      //basic usage
  108
+     *      var callback = function() {};
  109
+     *      radio('channel1').subscribe(callback); 
  110
+     *
  111
+     *      //subscribe an endless amount of callbacks
  112
+     *      radio('channel1').subscribe(callback, callback2, callback3 ...);
  113
+     *
  114
+     *      //adding callbacks with context
  115
+     *      radio('channel1').subscribe([callback, context],[callback1, context], callback3);
  116
+     *     
  117
+     *      //subscribe by chaining
  118
+     *      radio('channel1').subscribe(callback).radio('channel2').subscribe(callback).subscribe(callback2);
  119
+     */
  120
+    subscribe: function() {
  121
+      var a = arguments,
  122
+        c = this.channels[this.channelName],
  123
+        i, l = a.length,
  124
+        p, ai = [];
114 125
 
115  
-			//run through each arguments and subscribe it to the channel
116  
-			for (i = 0; i < l; i++) {
117  
-				ai = a[i];
118  
-				//if the user sent just a function, wrap the fucntion in an array [function]
119  
-				p = (typeof(ai) === "function") ? [ai] : ai;
120  
-				if ((typeof(p) === 'object') && (p.length)) c.push(p);
121  
-			}
122  
-			return this;
123  
-		},
  126
+      //run through each arguments and subscribe it to the channel
  127
+      for (i = 0; i < l; i++) {
  128
+        ai = a[i];
  129
+        //if the user sent just a function, wrap the fucntion in an array [function]
  130
+        p = (typeof(ai) === "function") ? [ai] : ai;
  131
+        if ((typeof(p) === 'object') && (p.length)) c.push(p);
  132
+      }
  133
+      return this;
  134
+    },
124 135
 
125  
-		/**
126  
-		 * Remove subscriber from channel
127  
-		 * Take arguments with functions and unsubscribe it if there is a match against existing subscribers.
128  
-		 * @param {Function} arguments callbacks separated by commas
129  
-		 * @example
130  
-		 *      //basic usage
131  
-		 *      radio('channel1').unsubscribe(callback); 
132  
-		 *      //you can unsubscribe as many callbacks as you want
133  
-		 *      radio('channel1').unsubscribe(callback, callback2, callback3 ...);
134  
-		 *       //removing callbacks with context is the same
135  
-		 *      radio('channel1').subscribe([callback, context]).unsubscribe(callback);
136  
-		 */
137  
-		unsubscribe: function() {
138  
-			var a = arguments,
139  
-				i, j, c = this.channels[this.channelName],
140  
-				l = a.length,
141  
-				cl = c.length,
142  
-				offset = 0,
143  
-				jo;
144  
-			//loop through each argument
145  
-			for (i = 0; i < l; i++) {
146  
-				//need to reset vars that change as the channel array items are removed
147  
-				offset = 0;
148  
-				cl = c.length;
149  
-				//loop through the channel
150  
-				for (j = 0; j < cl; j++) {
151  
-					jo = j - offset;
152  
-					//if there is a match with the argument and the channel function, unsubscribe it from the channel array
153  
-					if (c[jo][0] === a[i]) {
154  
-						//unsubscribe matched item from the channel array
155  
-						c.splice(jo, 1);
156  
-						offset++;
157  
-					}
158  
-				}
159  
-			}
160  
-			return this;
161  
-		}
162  
-	};
  136
+    /**
  137
+     * Remove subscriber from channel
  138
+     * Take arguments with functions and unsubscribe it if there is a match against existing subscribers.
  139
+     * @param {Function} arguments callbacks separated by commas
  140
+     * @example
  141
+     *      //basic usage
  142
+     *      radio('channel1').unsubscribe(callback); 
  143
+     *      //you can unsubscribe as many callbacks as you want
  144
+     *      radio('channel1').unsubscribe(callback, callback2, callback3 ...);
  145
+     *       //removing callbacks with context is the same
  146
+     *      radio('channel1').subscribe([callback, context]).unsubscribe(callback);
  147
+     */
  148
+    unsubscribe: function() {
  149
+      var a = arguments,
  150
+        i, j, c = this.channels[this.channelName],
  151
+        l = a.length,
  152
+        cl = c.length,
  153
+        offset = 0,
  154
+        jo;
  155
+      //loop through each argument
  156
+      for (i = 0; i < l; i++) {
  157
+        //need to reset vars that change as the channel array items are removed
  158
+        offset = 0;
  159
+        cl = c.length;
  160
+        //loop through the channel
  161
+        for (j = 0; j < cl; j++) {
  162
+          jo = j - offset;
  163
+          //if there is a match with the argument and the channel function, unsubscribe it from the channel array
  164
+          if (c[jo][0] === a[i]) {
  165
+            //unsubscribe matched item from the channel array
  166
+            c.splice(jo, 1);
  167
+            offset++;
  168
+          }
  169
+        }
  170
+      }
  171
+      return this;
  172
+    }
  173
+  };
163 174
 
164  
-	return radio;
165  
-});
  175
+  return radio;
  176
+});

0 notes on commit d9b7c93

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