Skip to content

Commit 177d51f

Browse files
committed
Signal.removeAll now has a new context parameter. If specified only listeners matching the given context are removed (thanks @lucbloom for the idea, phaserjs#880)
1 parent 92dbabb commit 177d51f

3 files changed

Lines changed: 34 additions & 18 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ Version 2.0.6 - "Jornhill" - -in development-
123123
* Group.removeAll has a new optional boolean parameter: `silent`. If set to `true` the children will not dispatch their `onRemovedFromGroup` events.
124124
* Internal child movements in Group (such as bringToTop) now uses the new `silent` parameter to avoid the child emitting incorrect Group addition and deletion events.
125125
* Camera.updateTarget has had a make-over and now is a lot smoother under certain conditions (thanks @tjkopena, fix #966)
126+
* Signal.removeAll now has a new `context` parameter. If specified only listeners matching the given context are removed (thanks @lucbloom for the idea, #880)
126127

127128

128129
### Bug Fixes

src/core/Signal.js

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,12 @@ Phaser.Signal.prototype = {
7777

7878
/**
7979
* @method Phaser.Signal#_registerListener
80+
* @private
8081
* @param {function} listener - Signal handler function.
81-
* @param {boolean} isOnce - Description.
82-
* @param {object} [listenerContext] - Description.
82+
* @param {boolean} isOnce - Should the listener only be called once?
83+
* @param {object} [listenerContext] - The context under which the listener is invoked.
8384
* @param {number} [priority] - The priority level of the event listener. Listeners with higher priority will be executed before listeners with lower priority. Listeners with same priority level will be executed at the same order as they were added. (default = 0).
8485
* @return {Phaser.SignalBinding} An Object representing the binding between the Signal and listener.
85-
* @private
8686
*/
8787
_registerListener: function (listener, isOnce, listenerContext, priority) {
8888

@@ -115,8 +115,8 @@ Phaser.Signal.prototype = {
115115

116116
/**
117117
* @method Phaser.Signal#_addBinding
118-
* @param {Phaser.SignalBinding} binding - An Object representing the binding between the Signal and listener.
119118
* @private
119+
* @param {Phaser.SignalBinding} binding - An Object representing the binding between the Signal and listener.
120120
*/
121121
_addBinding: function (binding) {
122122

@@ -134,9 +134,9 @@ Phaser.Signal.prototype = {
134134

135135
/**
136136
* @method Phaser.Signal#_indexOfListener
137-
* @param {function} listener - Signal handler function.
138-
* @return {number} Description.
139137
* @private
138+
* @param {function} listener - Signal handler function.
139+
* @return {number} The index of the listener within the private bindings array.
140140
*/
141141
_indexOfListener: function (listener, context) {
142142

@@ -161,8 +161,8 @@ Phaser.Signal.prototype = {
161161
* Check if listener was attached to Signal.
162162
*
163163
* @method Phaser.Signal#has
164-
* @param {Function} listener - Signal handler function.
165-
* @param {Object} [context] - Context on which listener will be executed (object that should represent the `this` variable inside listener function).
164+
* @param {function} listener - Signal handler function.
165+
* @param {object} [context] - Context on which listener will be executed (object that should represent the `this` variable inside listener function).
166166
* @return {boolean} If Signal has the specified listener.
167167
*/
168168
has: function (listener, context) {
@@ -175,9 +175,9 @@ Phaser.Signal.prototype = {
175175
* Add a listener to the signal.
176176
*
177177
* @method Phaser.Signal#add
178-
* @param {function} listener - Signal handler function.
179-
* @param {object} [listenerContext] Context on which listener will be executed (object that should represent the `this` variable inside listener function).
180-
* @param {number} [priority] The priority level of the event listener. Listeners with higher priority will be executed before listeners with lower priority. Listeners with same priority level will be executed at the same order as they were added. (default = 0).
178+
* @param {function} listener - The function to call when this Signal is dispatched.
179+
* @param {object} [listenerContext] - The context under which the listener will be executed (i.e. the object that should represent the `this` variable).
180+
* @param {number} [priority] - The priority level of the event listener. Listeners with higher priority will be executed before listeners with lower priority. Listeners with same priority level will be executed at the same order as they were added (default = 0)
181181
* @return {Phaser.SignalBinding} An Object representing the binding between the Signal and listener.
182182
*/
183183
add: function (listener, listenerContext, priority) {
@@ -192,9 +192,9 @@ Phaser.Signal.prototype = {
192192
* Add listener to the signal that should be removed after first execution (will be executed only once).
193193
*
194194
* @method Phaser.Signal#addOnce
195-
* @param {function} listener Signal handler function.
196-
* @param {object} [listenerContext] Context on which listener will be executed (object that should represent the `this` variable inside listener function).
197-
* @param {number} [priority] The priority level of the event listener. Listeners with higher priority will be executed before listeners with lower priority. Listeners with same priority level will be executed at the same order as they were added. (default = 0)
195+
* @param {function} listener - The function to call when this Signal is dispatched.
196+
* @param {object} [listenerContext] - The context under which the listener will be executed (i.e. the object that should represent the `this` variable).
197+
* @param {number} [priority] - The priority level of the event listener. Listeners with higher priority will be executed before listeners with lower priority. Listeners with same priority level will be executed at the same order as they were added (default = 0)
198198
* @return {Phaser.SignalBinding} An Object representing the binding between the Signal and listener.
199199
*/
200200
addOnce: function (listener, listenerContext, priority) {
@@ -209,8 +209,8 @@ Phaser.Signal.prototype = {
209209
* Remove a single listener from the dispatch queue.
210210
*
211211
* @method Phaser.Signal#remove
212-
* @param {function} listener Handler function that should be removed.
213-
* @param {object} [context] Execution context (since you can add the same handler multiple times if executing in a different context).
212+
* @param {function} listener - Handler function that should be removed.
213+
* @param {object} [context] - Execution context (since you can add the same handler multiple times if executing in a different context).
214214
* @return {function} Listener handler function.
215215
*/
216216
remove: function (listener, context) {
@@ -233,14 +233,28 @@ Phaser.Signal.prototype = {
233233
* Remove all listeners from the Signal.
234234
*
235235
* @method Phaser.Signal#removeAll
236+
* @param {object} [context=null] - If specified only listeners for the given context will be removed.
236237
*/
237-
removeAll: function () {
238+
removeAll: function (context) {
239+
240+
if (typeof context === 'undefined') { context = null; }
238241

239242
var n = this._bindings.length;
240243

241244
while (n--)
242245
{
243-
this._bindings[n]._destroy();
246+
if (context)
247+
{
248+
if (this._bindings[n].context === context)
249+
{
250+
this._bindings[n].destroy();
251+
this._bindings.splice(n, 1);
252+
}
253+
}
254+
else
255+
{
256+
this._bindings[n]._destroy();
257+
}
244258
}
245259

246260
this._bindings.length = 0;

src/core/Stage.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ Phaser.Stage.prototype.postUpdate = function () {
190190
*
191191
* @method Phaser.Stage#parseConfig
192192
* @protected
193+
* @param {object} config -The configuration object to parse.
193194
*/
194195
Phaser.Stage.prototype.parseConfig = function (config) {
195196

0 commit comments

Comments
 (0)