phaser/src/core/SignalBinding.js

165 lines
4.9 KiB
JavaScript
Raw Normal View History

2013-10-01 12:54:29 +00:00
/**
* @author Richard Davey <rich@photonstorm.com>
2014-02-05 05:54:25 +00:00
* @copyright 2014 Photon Storm Ltd.
2013-10-01 12:54:29 +00:00
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* Phaser.SignalBinding
*
* Object that represents a binding between a Signal and a listener function.
2013-11-28 15:57:09 +00:00
* This is an internal constructor and shouldn't be called by regular users.
* Inspired by Joa Ebert AS3 SignalBinding and Robert Penner's Slot classes.
*
2013-10-01 12:54:29 +00:00
* @class Phaser.SignalBinding
* @name SignalBinding
* @author Miller Medeiros http://millermedeiros.github.com/js-signals/
* @constructor
2013-10-01 12:54:29 +00:00
* @inner
2013-11-28 15:57:09 +00:00
* @param {Phaser.Signal} signal - Reference to Signal object that listener is currently bound to.
2013-10-01 12:54:29 +00:00
* @param {function} listener - Handler function bound to the signal.
2013-10-01 15:39:39 +00:00
* @param {boolean} isOnce - If binding should be executed just once.
2013-10-01 12:54:29 +00:00
* @param {object} [listenerContext] - Context on which listener will be executed (object that should represent the `this` variable inside listener function).
* @param {number} [priority] - The priority level of the event listener. (default = 0).
*/
Phaser.SignalBinding = function (signal, listener, isOnce, listenerContext, priority) {
/**
* @property {Phaser.Game} _listener - Handler function bound to the signal.
* @private
*/
this._listener = listener;
/**
* @property {boolean} _isOnce - If binding should be executed just once.
* @private
*/
this._isOnce = isOnce;
/**
* @property {object|undefined|null} context - Context on which listener will be executed (object that should represent the `this` variable inside listener function).
* @memberof SignalBinding.prototype
*/
this.context = listenerContext;
/**
2013-11-28 15:57:09 +00:00
* @property {Phaser.Signal} _signal - Reference to Signal object that listener is currently bound to.
* @private
*/
this._signal = signal;
/**
* @property {number} _priority - Listener priority.
* @private
*/
this._priority = priority || 0;
};
Phaser.SignalBinding.prototype = {
/**
2013-10-01 12:54:29 +00:00
* If binding is active and should be executed.
2013-10-01 15:39:39 +00:00
* @property {boolean} active
2013-10-01 12:54:29 +00:00
* @default
*/
active: true,
/**
2013-10-01 12:54:29 +00:00
* Default parameters passed to listener during `Signal.dispatch` and `SignalBinding.execute` (curried parameters).
2014-03-23 07:59:28 +00:00
* @property {array|null} params
* @default
2013-10-01 12:54:29 +00:00
*/
params: null,
/**
2013-10-01 12:54:29 +00:00
* Call listener passing arbitrary parameters.
2013-11-28 15:57:09 +00:00
* If binding was added using `Signal.addOnce()` it will be automatically removed from signal dispatch queue, this method is used internally for the signal dispatch.
2013-10-02 10:22:20 +00:00
* @method Phaser.SignalBinding#execute
2013-10-01 12:54:29 +00:00
* @param {array} [paramsArr] - Array of parameters that should be passed to the listener.
2013-11-28 15:57:09 +00:00
* @return {any} Value returned by the listener.
2013-10-01 12:54:29 +00:00
*/
execute: function (paramsArr) {
var handlerReturn, params;
if (this.active && !!this._listener)
{
params = this.params? this.params.concat(paramsArr) : paramsArr;
handlerReturn = this._listener.apply(this.context, params);
if (this._isOnce)
{
this.detach();
}
}
return handlerReturn;
},
/**
2013-10-01 12:54:29 +00:00
* Detach binding from signal.
2013-11-28 15:57:09 +00:00
* alias to: @see mySignal.remove(myBinding.getListener());
2013-10-02 10:22:20 +00:00
* @method Phaser.SignalBinding#detach
2013-10-01 12:54:29 +00:00
* @return {function|null} Handler function bound to the signal or `null` if binding was previously detached.
*/
detach: function () {
return this.isBound() ? this._signal.remove(this._listener, this.context) : null;
},
/**
2013-10-02 10:22:20 +00:00
* @method Phaser.SignalBinding#isBound
2013-10-01 15:39:39 +00:00
* @return {boolean} True if binding is still bound to the signal and has a listener.
2013-10-01 12:54:29 +00:00
*/
isBound: function () {
return (!!this._signal && !!this._listener);
},
/**
2013-10-02 10:22:20 +00:00
* @method Phaser.SignalBinding#isOnce
2013-10-01 15:39:39 +00:00
* @return {boolean} If SignalBinding will only be executed once.
2013-10-01 12:54:29 +00:00
*/
isOnce: function () {
return this._isOnce;
},
/**
2013-10-02 10:22:20 +00:00
* @method Phaser.SignalBinding#getListener
2013-10-01 12:54:29 +00:00
* @return {Function} Handler function bound to the signal.
*/
getListener: function () {
return this._listener;
},
/**
2013-10-02 10:22:20 +00:00
* @method Phaser.SignalBinding#getSignal
2013-10-01 12:54:29 +00:00
* @return {Signal} Signal that listener is currently bound to.
*/
getSignal: function () {
return this._signal;
},
/**
2013-10-02 10:22:20 +00:00
* @method Phaser.SignalBinding#_destroy
2013-10-01 12:54:29 +00:00
* Delete instance properties
* @private
*/
_destroy: function () {
delete this._signal;
delete this._listener;
delete this.context;
},
/**
2013-10-02 10:22:20 +00:00
* @method Phaser.SignalBinding#toString
2013-10-01 12:54:29 +00:00
* @return {string} String representation of the object.
*/
toString: function () {
return '[Phaser.SignalBinding isOnce:' + this._isOnce +', isBound:'+ this.isBound() +', active:' + this.active + ']';
}
};
Phaser.SignalBinding.prototype.constructor = Phaser.SignalBinding;