Skip to content

Commit 8b160a1

Browse files
committed
Tidied up formatting and docs.
1 parent 8c41f6c commit 8b160a1

2 files changed

Lines changed: 71 additions & 20 deletions

File tree

src/core/Signal.js

Lines changed: 69 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ Phaser.Signal.prototype = {
5454

5555
/**
5656
* If Signal is active and should broadcast events.
57-
* <p><strong>IMPORTANT:</strong> Setting this property during a dispatch will only affect the next dispatch, if you want to stop the propagation of a signal use `halt()` instead.</p>
57+
* IMPORTANT: Setting this property during a dispatch will only affect the next dispatch, if you want to stop the propagation of a signal use `halt()` instead.
5858
* @property {boolean} active
5959
* @default
6060
*/
@@ -67,9 +67,12 @@ Phaser.Signal.prototype = {
6767
* @private
6868
*/
6969
validateListener: function (listener, fnName) {
70-
if (typeof listener !== 'function') {
71-
throw new Error('listener is a required param of {fn}() and should be a Function.'.replace('{fn}', fnName));
70+
71+
if (typeof listener !== 'function')
72+
{
73+
throw new Error('Phaser.Signal: listener is a required param of {fn}() and should be a Function.'.replace('{fn}', fnName));
7274
}
75+
7376
},
7477

7578
/**
@@ -83,24 +86,31 @@ Phaser.Signal.prototype = {
8386
*/
8487
_registerListener: function (listener, isOnce, listenerContext, priority) {
8588

86-
var prevIndex = this._indexOfListener(listener, listenerContext),
87-
binding;
89+
var prevIndex = this._indexOfListener(listener, listenerContext);
90+
var binding;
8891

89-
if (prevIndex !== -1) {
92+
if (prevIndex !== -1)
93+
{
9094
binding = this._bindings[prevIndex];
91-
if (binding.isOnce() !== isOnce) {
95+
96+
if (binding.isOnce() !== isOnce)
97+
{
9298
throw new Error('You cannot add' + (isOnce ? '' : 'Once') + '() then add' + (!isOnce ? '' : 'Once') + '() the same listener without removing the relationship first.');
9399
}
94-
} else {
100+
}
101+
else
102+
{
95103
binding = new Phaser.SignalBinding(this, listener, isOnce, listenerContext, priority);
96104
this._addBinding(binding);
97105
}
98106

99-
if (this.memorize && this._prevParams) {
107+
if (this.memorize && this._prevParams)
108+
{
100109
binding.execute(this._prevParams);
101110
}
102111

103112
return binding;
113+
104114
},
105115

106116
/**
@@ -109,10 +119,17 @@ Phaser.Signal.prototype = {
109119
* @private
110120
*/
111121
_addBinding: function (binding) {
112-
//simplified insertion sort
122+
123+
// Simplified insertion sort
113124
var n = this._bindings.length;
114-
do { --n; } while (this._bindings[n] && binding._priority <= this._bindings[n]._priority);
125+
126+
do {
127+
n--;
128+
}
129+
while (this._bindings[n] && binding._priority <= this._bindings[n]._priority);
130+
115131
this._bindings.splice(n + 1, 0, binding);
132+
116133
},
117134

118135
/**
@@ -122,15 +139,22 @@ Phaser.Signal.prototype = {
122139
* @private
123140
*/
124141
_indexOfListener: function (listener, context) {
125-
var n = this._bindings.length,
126-
cur;
127-
while (n--) {
142+
143+
var n = this._bindings.length;
144+
var cur;
145+
146+
while (n--)
147+
{
128148
cur = this._bindings[n];
129-
if (cur._listener === listener && cur.context === context) {
149+
150+
if (cur._listener === listener && cur.context === context)
151+
{
130152
return n;
131153
}
132154
}
155+
133156
return -1;
157+
134158
},
135159

136160
/**
@@ -142,7 +166,9 @@ Phaser.Signal.prototype = {
142166
* @return {boolean} If Signal has the specified listener.
143167
*/
144168
has: function (listener, context) {
169+
145170
return this._indexOfListener(listener, context) !== -1;
171+
146172
},
147173

148174
/**
@@ -155,8 +181,11 @@ Phaser.Signal.prototype = {
155181
* @return {Phaser.SignalBinding} An Object representing the binding between the Signal and listener.
156182
*/
157183
add: function (listener, listenerContext, priority) {
184+
158185
this.validateListener(listener, 'add');
186+
159187
return this._registerListener(listener, false, listenerContext, priority);
188+
160189
},
161190

162191
/**
@@ -169,8 +198,11 @@ Phaser.Signal.prototype = {
169198
* @return {Phaser.SignalBinding} An Object representing the binding between the Signal and listener.
170199
*/
171200
addOnce: function (listener, listenerContext, priority) {
201+
172202
this.validateListener(listener, 'addOnce');
203+
173204
return this._registerListener(listener, true, listenerContext, priority);
205+
174206
},
175207

176208
/**
@@ -203,11 +235,16 @@ Phaser.Signal.prototype = {
203235
* @method Phaser.Signal#removeAll
204236
*/
205237
removeAll: function () {
238+
206239
var n = this._bindings.length;
207-
while (n--) {
240+
241+
while (n--)
242+
{
208243
this._bindings[n]._destroy();
209244
}
245+
210246
this._bindings.length = 0;
247+
211248
},
212249

213250
/**
@@ -217,7 +254,9 @@ Phaser.Signal.prototype = {
217254
* @return {number} Number of listeners attached to the Signal.
218255
*/
219256
getNumListeners: function () {
257+
220258
return this._bindings.length;
259+
221260
},
222261

223262
/**
@@ -228,7 +267,9 @@ Phaser.Signal.prototype = {
228267
* @method Phaser.Signal#halt
229268
*/
230269
halt: function () {
270+
231271
this._shouldPropagate = false;
272+
232273
},
233274

234275
/**
@@ -264,7 +305,10 @@ Phaser.Signal.prototype = {
264305

265306
//execute all callbacks until end of the list or until a callback returns `false` or stops propagation
266307
//reverse loop since listeners with higher priority will be added at the end of the list
267-
do { n--; } while (bindings[n] && this._shouldPropagate && bindings[n].execute(paramsArr) !== false);
308+
do {
309+
n--;
310+
}
311+
while (bindings[n] && this._shouldPropagate && bindings[n].execute(paramsArr) !== false);
268312

269313
},
270314

@@ -274,8 +318,10 @@ Phaser.Signal.prototype = {
274318
*
275319
* @method Phaser.Signal#forget
276320
*/
277-
forget: function(){
321+
forget: function() {
322+
278323
this._prevParams = null;
324+
279325
},
280326

281327
/**
@@ -285,9 +331,12 @@ Phaser.Signal.prototype = {
285331
* @method Phaser.Signal#dispose
286332
*/
287333
dispose: function () {
334+
288335
this.removeAll();
336+
289337
delete this._bindings;
290338
delete this._prevParams;
339+
291340
},
292341

293342
/**
@@ -296,7 +345,9 @@ Phaser.Signal.prototype = {
296345
* @return {string} String representation of the object.
297346
*/
298347
toString: function () {
348+
299349
return '[Phaser.Signal active:'+ this.active +' numListeners:'+ this.getNumListeners() +']';
350+
300351
}
301352

302353
};

src/core/SignalBinding.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
/**
88
* @class Phaser.SignalBinding
99
* @classdesc Object that represents a binding between a Signal and a listener function.
10-
* This is an internal constructor and shouldn't be called by regular users.
10+
* This is an internal constructor and shouldn't be created directly.
1111
* Inspired by Joa Ebert AS3 SignalBinding and Robert Penner's Slot classes.
1212
*
1313
* @author Miller Medeiros http://millermedeiros.github.com/js-signals/
@@ -136,8 +136,8 @@ Phaser.SignalBinding.prototype = {
136136
},
137137

138138
/**
139-
* @method Phaser.SignalBinding#_destroy
140139
* Delete instance properties
140+
* @method Phaser.SignalBinding#_destroy
141141
* @private
142142
*/
143143
_destroy: function () {

0 commit comments

Comments
 (0)