2013-08-08 18:16:47 +00:00
|
|
|
/// <reference path="../_definitions.ts" />
|
2013-04-13 03:25:03 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
/**
|
2013-04-18 15:49:08 +00:00
|
|
|
* Phaser - Signal
|
|
|
|
*
|
|
|
|
* A Signal is used for object communication via a custom broadcaster instead of Events.
|
|
|
|
* Based on JS Signals by Miller Medeiros. Converted by TypeScript by Richard Davey.
|
|
|
|
* Released under the MIT license
|
|
|
|
* http://millermedeiros.github.com/js-signals/
|
2013-04-18 13:16:18 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
module Phaser {
|
|
|
|
|
|
|
|
export class Signal {
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @property _bindings
|
|
|
|
* @type Array
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
private _bindings: SignalBinding[] = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @property _prevParams
|
|
|
|
* @type Any
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
private _prevParams = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Signals Version Number
|
|
|
|
* @property VERSION
|
|
|
|
* @type String
|
|
|
|
* @const
|
|
|
|
*/
|
|
|
|
public static VERSION: string = '1.0.0';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If Signal should keep record of previously dispatched parameters and
|
|
|
|
* automatically execute listener during `add()`/`addOnce()` if Signal was
|
|
|
|
* already dispatched before.
|
|
|
|
* @type boolean
|
|
|
|
*/
|
2013-08-08 10:34:33 +00:00
|
|
|
public memorize: boolean = false;
|
2013-04-18 13:16:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @type boolean
|
|
|
|
* @private
|
|
|
|
*/
|
2013-08-08 10:34:33 +00:00
|
|
|
private _shouldPropagate: boolean = true;
|
2013-04-18 13:16:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* If Signal is active and should broadcast events.
|
|
|
|
* <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>
|
|
|
|
* @type boolean
|
|
|
|
*/
|
2013-08-08 10:34:33 +00:00
|
|
|
public active: boolean = true;
|
2013-04-18 13:16:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @method validateListener
|
|
|
|
* @param {Any} listener
|
|
|
|
* @param {Any} fnName
|
|
|
|
*/
|
|
|
|
public validateListener(listener, fnName) {
|
|
|
|
|
|
|
|
if (typeof listener !== 'function')
|
|
|
|
{
|
|
|
|
throw new Error('listener is a required param of {fn}() and should be a Function.'.replace('{fn}', fnName));
|
|
|
|
}
|
|
|
|
|
2013-04-13 03:25:03 +00:00
|
|
|
}
|
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
/**
|
|
|
|
* @param {Function} listener
|
|
|
|
* @param {boolean} isOnce
|
|
|
|
* @param {Object} [listenerContext]
|
|
|
|
* @param {Number} [priority]
|
|
|
|
* @return {SignalBinding}
|
|
|
|
* @private
|
|
|
|
*/
|
2013-08-08 10:34:33 +00:00
|
|
|
private _registerListener(listener, isOnce: boolean, listenerContext, priority: number): SignalBinding {
|
2013-04-13 03:25:03 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
var prevIndex: number = this._indexOfListener(listener, listenerContext);
|
|
|
|
var binding: SignalBinding;
|
2013-04-13 03:25:03 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
if (prevIndex !== -1)
|
|
|
|
{
|
|
|
|
binding = this._bindings[prevIndex];
|
2013-04-13 03:25:03 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
if (binding.isOnce() !== isOnce)
|
|
|
|
{
|
|
|
|
throw new Error('You cannot add' + (isOnce ? '' : 'Once') + '() then add' + (!isOnce ? '' : 'Once') + '() the same listener without removing the relationship first.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
binding = new SignalBinding(this, listener, isOnce, listenerContext, priority);
|
2013-04-13 03:25:03 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
this._addBinding(binding);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.memorize && this._prevParams)
|
2013-04-13 03:25:03 +00:00
|
|
|
{
|
2013-04-18 13:16:18 +00:00
|
|
|
binding.execute(this._prevParams);
|
2013-04-13 03:25:03 +00:00
|
|
|
}
|
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
return binding;
|
2013-04-13 03:25:03 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @method _addBinding
|
|
|
|
* @param {SignalBinding} binding
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
private _addBinding(binding: SignalBinding) {
|
2013-04-13 03:25:03 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
//simplified insertion sort
|
2013-04-13 03:25:03 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
var n: number = this._bindings.length;
|
2013-04-13 03:25:03 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
do { --n; } while (this._bindings[n] && binding.priority <= this._bindings[n].priority);
|
2013-04-13 03:25:03 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
this._bindings.splice(n + 1, 0, binding);
|
2013-04-13 03:25:03 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
2013-04-13 03:25:03 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @method _indexOfListener
|
|
|
|
* @param {Function} listener
|
|
|
|
* @return {number}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
private _indexOfListener(listener, context): number {
|
2013-04-13 03:25:03 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
var n: number = this._bindings.length;
|
|
|
|
var cur: SignalBinding;
|
2013-04-13 03:25:03 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
while (n--)
|
|
|
|
{
|
|
|
|
cur = this._bindings[n];
|
2013-04-13 03:25:03 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
if (cur.getListener() === listener && cur.context === context)
|
|
|
|
{
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
}
|
2013-04-13 03:25:03 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
return -1;
|
2013-04-13 03:25:03 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
/**
|
|
|
|
* Check if listener was attached to Signal.
|
|
|
|
* @param {Function} listener
|
|
|
|
* @param {Object} [context]
|
|
|
|
* @return {boolean} if Signal has the specified listener.
|
|
|
|
*/
|
2013-08-08 10:34:33 +00:00
|
|
|
public has(listener, context: any = null): boolean {
|
2013-04-13 03:25:03 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
return this._indexOfListener(listener, context) !== -1;
|
2013-04-13 03:25:03 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
2013-04-13 03:25:03 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
/**
|
|
|
|
* Add a listener to the signal.
|
|
|
|
* @param {Function} listener Signal handler function.
|
|
|
|
* @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. 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)
|
|
|
|
* @return {SignalBinding} An Object representing the binding between the Signal and listener.
|
|
|
|
*/
|
2013-08-08 10:34:33 +00:00
|
|
|
public add(listener, listenerContext: any = null, priority: number = 0): SignalBinding {
|
2013-04-13 03:25:03 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
this.validateListener(listener, 'add');
|
2013-04-13 03:25:03 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
return this._registerListener(listener, false, listenerContext, priority);
|
2013-04-13 03:25:03 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
2013-04-13 03:25:03 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
/**
|
|
|
|
* Add listener to the signal that should be removed after first execution (will be executed only once).
|
|
|
|
* @param {Function} listener Signal handler function.
|
|
|
|
* @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. 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)
|
|
|
|
* @return {SignalBinding} An Object representing the binding between the Signal and listener.
|
|
|
|
*/
|
2013-08-08 10:34:33 +00:00
|
|
|
public addOnce(listener, listenerContext: any = null, priority: number = 0): SignalBinding {
|
2013-04-13 03:25:03 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
this.validateListener(listener, 'addOnce');
|
2013-04-13 03:25:03 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
return this._registerListener(listener, true, listenerContext, priority);
|
2013-04-13 03:25:03 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
2013-04-13 03:25:03 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
/**
|
|
|
|
* Remove a single listener from the dispatch queue.
|
|
|
|
* @param {Function} listener Handler function that should be removed.
|
|
|
|
* @param {Object} [context] Execution context (since you can add the same handler multiple times if executing in a different context).
|
|
|
|
* @return {Function} Listener handler function.
|
|
|
|
*/
|
2013-08-08 10:34:33 +00:00
|
|
|
public remove(listener, context: any = null) {
|
2013-04-13 03:25:03 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
this.validateListener(listener, 'remove');
|
2013-04-13 03:25:03 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
var i: number = this._indexOfListener(listener, context);
|
2013-04-13 03:25:03 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
if (i !== -1)
|
|
|
|
{
|
2013-04-29 01:41:19 +00:00
|
|
|
this._bindings[i]._destroy();
|
2013-04-18 13:16:18 +00:00
|
|
|
this._bindings.splice(i, 1);
|
|
|
|
}
|
2013-04-13 03:25:03 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
return listener;
|
2013-04-13 03:25:03 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
/**
|
|
|
|
* Remove all listeners from the Signal.
|
|
|
|
*/
|
|
|
|
public removeAll() {
|
2013-04-13 03:25:03 +00:00
|
|
|
|
2013-04-29 01:41:19 +00:00
|
|
|
if (this._bindings)
|
2013-04-18 13:16:18 +00:00
|
|
|
{
|
2013-04-29 01:41:19 +00:00
|
|
|
var n: number = this._bindings.length;
|
2013-04-13 03:25:03 +00:00
|
|
|
|
2013-04-29 01:41:19 +00:00
|
|
|
while (n--)
|
|
|
|
{
|
|
|
|
this._bindings[n]._destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
this._bindings.length = 0;
|
|
|
|
}
|
2013-04-13 03:25:03 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
/**
|
|
|
|
* @return {number} Number of listeners attached to the Signal.
|
|
|
|
*/
|
|
|
|
public getNumListeners(): number {
|
2013-04-13 03:25:03 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
return this._bindings.length;
|
2013-04-13 03:25:03 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
2013-04-13 03:25:03 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
/**
|
|
|
|
* Stop propagation of the event, blocking the dispatch to next listeners on the queue.
|
|
|
|
* <p><strong>IMPORTANT:</strong> should be called only during signal dispatch, calling it before/after dispatch won't affect signal broadcast.</p>
|
|
|
|
* @see Signal.prototype.disable
|
|
|
|
*/
|
|
|
|
public halt() {
|
2013-04-13 03:25:03 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
this._shouldPropagate = false;
|
2013-04-13 03:25:03 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
2013-04-13 03:25:03 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
/**
|
|
|
|
* Dispatch/Broadcast Signal to all listeners added to the queue.
|
|
|
|
* @param {...*} [params] Parameters that should be passed to each handler.
|
|
|
|
*/
|
|
|
|
public dispatch(...paramsArr: any[]) {
|
2013-04-13 03:25:03 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
if (!this.active)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-04-13 03:25:03 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
var n: number = this._bindings.length;
|
|
|
|
var bindings: SignalBinding[];
|
2013-04-13 03:25:03 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
if (this.memorize)
|
|
|
|
{
|
|
|
|
this._prevParams = paramsArr;
|
|
|
|
}
|
2013-04-13 03:25:03 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
if (!n)
|
|
|
|
{
|
|
|
|
//should come after memorize
|
|
|
|
return;
|
|
|
|
}
|
2013-04-13 03:25:03 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
bindings = this._bindings.slice(0); //clone array in case add/remove items during dispatch
|
2013-04-13 03:25:03 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
this._shouldPropagate = true; //in case `halt` was called before dispatch or during the previous dispatch.
|
2013-04-13 03:25:03 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
//execute all callbacks until end of the list or until a callback returns `false` or stops propagation
|
|
|
|
//reverse loop since listeners with higher priority will be added at the end of the list
|
|
|
|
do { n--; } while (bindings[n] && this._shouldPropagate && bindings[n].execute(paramsArr) !== false);
|
2013-04-13 03:25:03 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
2013-04-13 03:25:03 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
/**
|
|
|
|
* Forget memorized arguments.
|
|
|
|
* @see Signal.memorize
|
|
|
|
*/
|
|
|
|
public forget() {
|
2013-04-13 03:25:03 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
this._prevParams = null;
|
2013-04-13 03:25:03 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
2013-04-13 03:25:03 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
/**
|
|
|
|
* Remove all bindings from signal and destroy any reference to external objects (destroy Signal object).
|
|
|
|
* <p><strong>IMPORTANT:</strong> calling any method on the signal instance after calling dispose will throw errors.</p>
|
|
|
|
*/
|
|
|
|
public dispose() {
|
2013-04-13 03:25:03 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
this.removeAll();
|
2013-04-13 03:25:03 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
delete this._bindings;
|
|
|
|
delete this._prevParams;
|
2013-04-13 03:25:03 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
2013-04-13 03:25:03 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
/**
|
|
|
|
* @return {string} String representation of the object.
|
|
|
|
*/
|
|
|
|
public toString(): string {
|
2013-04-13 03:25:03 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
return '[Signal active:' + this.active + ' numListeners:' + this.getNumListeners() + ']';
|
2013-04-13 03:25:03 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
2013-04-13 03:25:03 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|