You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* If Signal should keep record of previously dispatched parameters and
40
33
* automatically execute listener during `add()`/`addOnce()` if Signal was
41
34
* already dispatched before.
42
35
* @property {boolean} memorize
43
36
*/
44
-
this.memorize=false;
37
+
memorize: false,
45
38
46
39
/**
47
40
* @property {boolean} _shouldPropagate
48
41
* @private
49
42
*/
50
-
this._shouldPropagate=true;
43
+
_shouldPropagate: true,
51
44
52
45
/**
53
46
* If Signal is active and should broadcast events.
54
47
* 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.
55
48
* @property {boolean} active
56
49
* @default
57
50
*/
58
-
this.active=true;
59
-
60
-
};
51
+
active: true,
61
52
62
-
Phaser.Signal.prototype={
53
+
/**
54
+
* @property {function} _boundDispatch - The bound dispatch function, if any.
55
+
* @private
56
+
*/
57
+
_boundDispatch: true,
63
58
64
59
/**
65
60
* @method Phaser.Signal#validateListener
@@ -121,6 +116,11 @@ Phaser.Signal.prototype = {
121
116
*/
122
117
_addBinding: function(binding){
123
118
119
+
if(!this._bindings)
120
+
{
121
+
this._bindings=[];
122
+
}
123
+
124
124
// Simplified insertion sort
125
125
varn=this._bindings.length;
126
126
@@ -137,10 +137,18 @@ Phaser.Signal.prototype = {
137
137
* @method Phaser.Signal#_indexOfListener
138
138
* @private
139
139
* @param {function} listener - Signal handler function.
140
+
* @param {object} [context=null] - Signal handler function.
140
141
* @return {number} The index of the listener within the private bindings array.
141
142
*/
142
143
_indexOfListener: function(listener,context){
143
144
145
+
if(!this._bindings)
146
+
{
147
+
return-1;
148
+
}
149
+
150
+
if(typeofcontext==='undefined'){context=null;}
151
+
144
152
varn=this._bindings.length;
145
153
varcur;
146
154
@@ -211,7 +219,7 @@ Phaser.Signal.prototype = {
211
219
*
212
220
* @method Phaser.Signal#remove
213
221
* @param {function} listener - Handler function that should be removed.
214
-
* @param {object} [context] - Execution context (since you can add the same handler multiple times if executing in a different context).
222
+
* @param {object} [context=null] - Execution context (since you can add the same handler multiple times if executing in a different context).
215
223
* @return {function} Listener handler function.
216
224
*/
217
225
remove: function(listener,context){
@@ -240,6 +248,11 @@ Phaser.Signal.prototype = {
240
248
241
249
if(typeofcontext==='undefined'){context=null;}
242
250
251
+
if(!this._bindings)
252
+
{
253
+
return;
254
+
}
255
+
243
256
varn=this._bindings.length;
244
257
245
258
while(n--)
@@ -273,7 +286,7 @@ Phaser.Signal.prototype = {
273
286
*/
274
287
getNumListeners: function(){
275
288
276
-
returnthis._bindings.length;
289
+
returnthis._bindings ? this._bindings.length : 0;
277
290
278
291
},
279
292
@@ -293,12 +306,14 @@ Phaser.Signal.prototype = {
293
306
/**
294
307
* Dispatch/Broadcast Signal to all listeners added to the queue.
295
308
*
309
+
* To create a bound dispatch for this Signal, use {@link Phaser.Signal#boundDispatch}.
310
+
*
296
311
* @method Phaser.Signal#dispatch
297
312
* @param {any} [params] - Parameters that should be passed to each handler.
298
313
*/
299
314
dispatch: function(){
300
315
301
-
if(!this.active)
316
+
if(!this.active||!this._bindings)
302
317
{
303
318
return;
304
319
}
@@ -338,7 +353,10 @@ Phaser.Signal.prototype = {
338
353
*/
339
354
forget: function(){
340
355
341
-
this._prevParams=null;
356
+
if(this._prevParams)
357
+
{
358
+
this._prevParams=null;
359
+
}
342
360
343
361
},
344
362
@@ -352,8 +370,11 @@ Phaser.Signal.prototype = {
352
370
353
371
this.removeAll();
354
372
355
-
deletethis._bindings;
356
-
deletethis._prevParams;
373
+
this._bindings=null;
374
+
if(this._prevParams)
375
+
{
376
+
this._prevParams=null;
377
+
}
357
378
358
379
},
359
380
@@ -370,4 +391,22 @@ Phaser.Signal.prototype = {
370
391
371
392
};
372
393
394
+
/**
395
+
* If the dispatch function needs to be passed somewhere, or called independently
Copy file name to clipboardExpand all lines: src/core/SignalBinding.js
+30-15Lines changed: 30 additions & 15 deletions
Original file line number
Diff line number
Diff line change
@@ -15,7 +15,7 @@
15
15
* @param {Phaser.Signal} signal - Reference to Signal object that listener is currently bound to.
16
16
* @param {function} listener - Handler function bound to the signal.
17
17
* @param {boolean} isOnce - If binding should be executed just once.
18
-
* @param {object} [listenerContext] - Context on which listener will be executed (object that should represent the `this` variable inside listener function).
18
+
* @param {object} [listenerContext=null] - Context on which listener will be executed (object that should represent the `this` variable inside listener function).
19
19
* @param {number} [priority] - The priority level of the event listener. (default = 0).
* @property {boolean} _isOnce - If binding should be executed just once.
40
+
* @property {Phaser.Signal} _signal - Reference to Signal object that listener is currently bound to.
31
41
* @private
32
42
*/
33
-
this._isOnce=isOnce;
43
+
this._signal=signal;
44
+
45
+
if(priority)
46
+
{
47
+
this._priority=priority;
48
+
}
49
+
50
+
};
51
+
52
+
Phaser.SignalBinding.prototype={
34
53
35
54
/**
36
-
* @property {object|undefined|null} context - Context on which listener will be executed (object that should represent the `this` variable inside listener function).
55
+
* @property {?object} context - Context on which listener will be executed (object that should represent the `this` variable inside listener function).
37
56
*/
38
-
this.context=listenerContext;
57
+
context: null,
39
58
40
59
/**
41
-
* @property {Phaser.Signal} _signal - Reference to Signal object that listener is currently bound to.
60
+
* @property {boolean} _isOnce - If binding should be executed just once.
0 commit comments