phaser/TS Source/core/SignalBinding.js
2013-08-28 07:02:57 +01:00

113 lines
4.7 KiB
JavaScript

/// <reference path="../_definitions.ts" />
/**
* Phaser - SignalBinding
*
* An object that represents a binding between a Signal and a listener function.
* Based on JS Signals by Miller Medeiros. Converted by TypeScript by Richard Davey.
* Released under the MIT license
* http://millermedeiros.github.com/js-signals/
*/
var Phaser;
(function (Phaser) {
var SignalBinding = (function () {
/**
* Object that represents a binding between a Signal and a listener function.
* <br />- <strong>This is an internal constructor and shouldn't be called by regular users.</strong>
* <br />- inspired by Joa Ebert AS3 SignalBinding and Robert Penner's Slot classes.
* @author Miller Medeiros
* @constructor
* @internal
* @name SignalBinding
* @param {Signal} signal Reference to Signal object that listener is currently bound to.
* @param {Function} listener Handler function bound to the signal.
* @param {bool} isOnce If binding should be executed just once.
* @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).
*/
function SignalBinding(signal, listener, isOnce, listenerContext, priority) {
if (typeof priority === "undefined") { priority = 0; }
/**
* If binding is active and should be executed.
* @type bool
*/
this.active = true;
/**
* Default parameters passed to listener during `Signal.dispatch` and `SignalBinding.execute`. (curried parameters)
* @type Array|null
*/
this.params = null;
this._listener = listener;
this._isOnce = isOnce;
this.context = listenerContext;
this._signal = signal;
this.priority = priority || 0;
}
SignalBinding.prototype.execute = /**
* Call listener passing arbitrary parameters.
* <p>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.</p>
* @param {Array} [paramsArr] Array of parameters that should be passed to the listener
* @return {*} Value returned by the listener.
*/
function (paramsArr) {
var handlerReturn;
var 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;
};
SignalBinding.prototype.detach = /**
* Detach binding from signal.
* - alias to: mySignal.remove(myBinding.getListener());
* @return {Function|null} Handler function bound to the signal or `null` if binding was previously detached.
*/
function () {
return this.isBound() ? this._signal.remove(this._listener, this.context) : null;
};
SignalBinding.prototype.isBound = /**
* @return {bool} `true` if binding is still bound to the signal and have a listener.
*/
function () {
return (!!this._signal && !!this._listener);
};
SignalBinding.prototype.isOnce = /**
* @return {bool} If SignalBinding will only be executed once.
*/
function () {
return this._isOnce;
};
SignalBinding.prototype.getListener = /**
* @return {Function} Handler function bound to the signal.
*/
function () {
return this._listener;
};
SignalBinding.prototype.getSignal = /**
* @return {Signal} Signal that listener is currently bound to.
*/
function () {
return this._signal;
};
SignalBinding.prototype._destroy = /**
* Delete instance properties
* @private
*/
function () {
delete this._signal;
delete this._listener;
delete this.context;
};
SignalBinding.prototype.toString = /**
* @return {string} String representation of the object.
*/
function () {
return '[SignalBinding isOnce:' + this._isOnce + ', isBound:' + this.isBound() + ', active:' + this.active + ']';
};
return SignalBinding;
})();
Phaser.SignalBinding = SignalBinding;
})(Phaser || (Phaser = {}));