diff --git a/dist/signals.min.js b/dist/signals.min.js index 248be67..55f4115 100644 --- a/dist/signals.min.js +++ b/dist/signals.min.js @@ -1 +1 @@ -var SignalsJS=function(t){function e(n){if(r[n])return r[n].exports;var i=r[n]={i:n,l:!1,exports:{}};return t[n].call(i.exports,i,i.exports,e),i.l=!0,i.exports}var r={};return e.m=t,e.c=r,e.d=function(t,r,n){e.o(t,r)||Object.defineProperty(t,r,{configurable:!1,enumerable:!0,get:n})},e.n=function(t){var r=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(r,"a",r),r},e.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},e.p="",e(e.s=5)}([function(t,e,r){"use strict";Object.defineProperty(e,"__esModule",{value:!0});var n=function(){function t(t,e,r,n){void 0===r&&(r=!1),void 0===n&&(n=0),this._enabled=!0,this._once=!1,this._priority=0,this._listener=t,this._once=r,this._signal=e,this._priority=n,this.verifyListener(t)}return t.prototype.execute0=function(){if(this._enabled){if(this._once&&this.remove(),this._params&&this._params.length)return void this._listener.apply(null,this._params);this._listener()}},t.prototype.execute1=function(t){if(this._enabled){if(this._once&&this.remove(),this._params&&this._params.length)return void this._listener.apply(null,[t].concat(this._params));this._listener(t)}},t.prototype.execute=function(t){if(this._enabled){this._once&&this.remove(),this._params&&this._params.length&&(t=t.concat(this._params));var e=t.length;0==e?this._listener():1==e?this._listener(t[0]):2==e?this._listener(t[0],t[1]):3==e?this._listener(t[0],t[1],t[2]):this._listener.apply(null,t)}},Object.defineProperty(t.prototype,"listener",{get:function(){return this._listener},set:function(t){if(null==t)throw new Error("Given listener is null.\nDid you want to set enabled to false instead?");this.verifyListener(t),this._listener=t},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"once",{get:function(){return this._once},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"priority",{get:function(){return this._priority},enumerable:!0,configurable:!0}),t.prototype.toString=function(){return"[Slot listener: "+this._listener+", once: "+this._once+", priority: "+this._priority+", enabled: "+this._enabled+"]"},Object.defineProperty(t.prototype,"enabled",{get:function(){return this._enabled},set:function(t){this._enabled=t},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"params",{get:function(){return this._params},set:function(t){this._params=t},enumerable:!0,configurable:!0}),t.prototype.remove=function(){this._signal.remove(this._listener)},t.prototype.verifyListener=function(t){if(null==t)throw new Error("Given listener is null.");if(null==this._signal)throw new Error("Internal signal reference has not been set yet.")},t}();e.Slot=n},function(t,e,r){"use strict";Object.defineProperty(e,"__esModule",{value:!0});var n=r(4),i=r(0),o=function(){function t(){for(var t=[],e=0;e."+this._valueClasses[e])},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"numListeners",{get:function(){return this.slots.length},enumerable:!0,configurable:!0}),t.prototype.addOnce=function(t){return this.registerListener(t,!0)},t.prototype.remove=function(t){var e=this.slots.find(t);return e?(this.slots=this.slots.filterNot(t),e):null},t.prototype.removeAll=function(){this.slots=n.SlotList.NIL},t.prototype.dispatch=function(){for(var t=[],e=0;e is not an instance of <"+this._valueClasses[i]+">.");var o=this.slots;if(o.nonEmpty)for(;o.nonEmpty;)o.head.execute(t),o=o.tail},t.prototype.registerListener=function(t,e){if(void 0===e&&(e=!1),this.registrationPossible(t,e)){var r=new i.Slot(t,this,e);return this.slots=this.slots.prepend(r),r}return this.slots.find(t)},t.prototype.registrationPossible=function(t,e){if(!this.slots.nonEmpty)return!0;var r=this.slots.find(t);if(!r)return!0;if(r.once!=e)throw new Error("You cannot addOnce() then add() the same listener without removing the relationship first.");return!1},t}();e.OnceSignal=o},function(t,e,r){"use strict";var n=this&&this.__extends||function(){var t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var r in e)e.hasOwnProperty(r)&&(t[r]=e[r])};return function(e,r){function n(){this.constructor=e}t(e,r),e.prototype=null===r?Object.create(r):(n.prototype=r.prototype,new n)}}();Object.defineProperty(e,"__esModule",{value:!0});var i=r(3),o=r(0),s=function(t){function e(){for(var e=[],r=0;rthis.head.priority)return this.prepend(e);for(var n=new t(this.head),i=n,o=this.tail;o.nonEmpty;){if(r>o.head.priority)return i.tail=o.prepend(e),n;i=i.tail=new t(o.head),o=o.tail}return i.tail=new t(e),n},t.prototype.filterNot=function(e){if(!this.nonEmpty||null==e)return this;if(e==this.head.listener)return this.tail;for(var r=new t(this.head),n=r,i=this.tail;i.nonEmpty;){if(i.head.listener==e)return n.tail=i.tail,r;n=n.tail=new t(i.head),i=i.tail}return this},t.prototype.contains=function(t){if(!this.nonEmpty)return!1;for(var e=this;e.nonEmpty;){if(e.head.listener==t)return!0;e=e.tail}return!1},t.prototype.find=function(t){if(!this.nonEmpty)return null;for(var e=this;e.nonEmpty;){if(e.head.listener==t)return e.head;e=e.tail}return null},t.prototype.toString=function(){for(var t="",e=this;e.nonEmpty;)t+=e.head+" -> ",e=e.tail;return"[List "+(t+="NIL")+"]"},t.NIL=new t(null,null),t}();e.SlotList=n},function(t,e,r){"use strict";Object.defineProperty(e,"__esModule",{value:!0});var n=r(6);e.DeluxeSignal=n.DeluxeSignal;var i=r(7);e.GenericEvent=i.GenericEvent;var o=r(8);e.IOnceSignal=o.IOnceSignal;var s=r(9);e.IPrioritySignal=s.IPrioritySignal;var a=r(10);e.ISignal=a.ISignal;var l=r(11);e.ISlot=l.ISlot;var u=r(12);e.MonoSignal=u.MonoSignal;var c=r(1);e.OnceSignal=c.OnceSignal;var h=r(2);e.PrioritySignal=h.PrioritySignal;var f=r(13);e.Promise=f.Promise;var p=r(3);e.Signal=p.Signal;var y=r(0);e.Slot=y.Slot;var v=r(4);e.SlotList=v.SlotList},function(t,e,r){"use strict";var n=this&&this.__extends||function(){var t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var r in e)e.hasOwnProperty(r)&&(t[r]=e[r])};return function(e,r){function n(){this.constructor=e}t(e,r),e.prototype=null===r?Object.create(r):(n.prototype=r.prototype,new n)}}();Object.defineProperty(e,"__esModule",{value:!0});var i=r(2),o=function(t){function e(e){void 0===e&&(e=null);for(var r=[],n=1;n is not an instance of <"+this._valueClasses[i]+">.");var o=t[0];o&&(o.target&&(o=o.clone(),t[0]=o),o.target=this.target,o.currentTarget=this.target,o.signal=this);for(var s=this.slots;s.nonEmpty;)s.head.execute(t),s=s.tail;if(o&&o.bubbles)for(var a=this.target;a&&a.hasOwnProperty("parent")&&(a=a.parent,null===a.onEventBubbled||(o.currentTarget=a,a.onEventBubbled(o))););},e}(i.PrioritySignal);e.DeluxeSignal=o},function(t,e,r){"use strict";Object.defineProperty(e,"__esModule",{value:!0});var n=function(){function t(t){void 0===t&&(t=!1),this._bubbles=t}return Object.defineProperty(t.prototype,"signal",{get:function(){return this._signal},set:function(t){this._signal=t},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"target",{get:function(){return this._target},set:function(t){this._target=t},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"currentTarget",{get:function(){return this._currentTarget},set:function(t){this._currentTarget=t},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"bubbles",{get:function(){return this._bubbles},set:function(t){this._bubbles=t},enumerable:!0,configurable:!0}),t.prototype.clone=function(){return new t(this._bubbles)},t}();e.GenericEvent=n},function(t,e,r){"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.IOnceSignal=Symbol("IOnceSignal")},function(t,e,r){"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.IPrioritySignal=Symbol("IPrioritySignal")},function(t,e,r){"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.ISignal=Symbol("ISignal")},function(t,e,r){"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.ISlot=Symbol("ISlot")},function(t,e,r){"use strict";Object.defineProperty(e,"__esModule",{value:!0});var n=r(0),i=function(){function t(){for(var t=[],e=0;e."+this._valueClasses[e])},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"numListeners",{get:function(){return this.slot?1:0},enumerable:!0,configurable:!0}),t.prototype.add=function(t){return this.registerListener(t)},t.prototype.addOnce=function(t){return this.registerListener(t,!0)},t.prototype.remove=function(t){if(this.slot&&this.slot.listener==t){var e=this.slot;return this.slot=null,e}return null},t.prototype.removeAll=function(){this.slot&&this.slot.remove()},t.prototype.dispatch=function(){for(var t=[],e=0;e is not an instance of <"+this._valueClasses[i]+">.");this.slot&&this.slot.execute(t)},t.prototype.registerListener=function(t,e){if(void 0===e&&(e=!1),this.slot)throw new Error("You cannot add or addOnce with a listener already added, remove the current listener first.");return this.slot=new n.Slot(t,this,e)},t}();e.MonoSignal=i},function(t,e,r){"use strict";var n=this&&this.__extends||function(){var t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var r in e)e.hasOwnProperty(r)&&(t[r]=e[r])};return function(e,r){function n(){this.constructor=e}t(e,r),e.prototype=null===r?Object.create(r):(n.prototype=r.prototype,new n)}}();Object.defineProperty(e,"__esModule",{value:!0});var i=r(1),o=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return n(e,t),e.prototype.addOnce=function(e){var r=t.prototype.addOnce.call(this,e);return this.isDispatched&&(r.execute(this.valueObjects),r.remove()),r},e.prototype.dispatch=function(){for(var e=[],r=0;r."+this._valueClasses[e])},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"numListeners",{get:function(){return this.slots.length},enumerable:!0,configurable:!0}),t.prototype.addOnce=function(t){return this.registerListener(t,!0)},t.prototype.remove=function(t){var e=this.slots.find(t);return e?(this.slots=this.slots.filterNot(t),e):null},t.prototype.removeAll=function(){this.slots=n.SlotList.NIL},t.prototype.dispatch=function(){for(var t=[],e=0;e is not an instance of <"+this._valueClasses[i]+">.");var o=this.slots;if(o.nonEmpty)for(;o.nonEmpty;)o.head.execute(t),o=o.tail},t.prototype.registerListener=function(t,e){if(void 0===e&&(e=!1),this.registrationPossible(t,e)){var r=new i.Slot(t,this,e);return this.slots=this.slots.prepend(r),r}return this.slots.find(t)},t.prototype.registrationPossible=function(t,e){if(!this.slots.nonEmpty)return!0;var r=this.slots.find(t);if(!r)return!0;if(r.once!==e)throw new Error("You cannot addOnce() then add() the same listener without removing the relationship first.");return!1},t}();e.OnceSignal=o},function(t,e,r){"use strict";var n=this&&this.__extends||function(){var t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var r in e)e.hasOwnProperty(r)&&(t[r]=e[r])};return function(e,r){function n(){this.constructor=e}t(e,r),e.prototype=null===r?Object.create(r):(n.prototype=r.prototype,new n)}}();Object.defineProperty(e,"__esModule",{value:!0});var i=r(3),o=r(0),s=function(t){function e(){for(var e=[],r=0;rthis.head.priority)return this.prepend(e);for(var n=new t(this.head),i=n,o=this.tail;o.nonEmpty;){if(r>o.head.priority)return i.tail=o.prepend(e),n;i=i.tail=new t(o.head),o=o.tail}return i.tail=new t(e),n},t.prototype.filterNot=function(e){if(!this.nonEmpty||null==e)return this;if(e===this.head.listener)return this.tail;for(var r=new t(this.head),n=r,i=this.tail;i.nonEmpty;){if(i.head.listener===e)return n.tail=i.tail,r;n=n.tail=new t(i.head),i=i.tail}return this},t.prototype.contains=function(t){if(!this.nonEmpty)return!1;for(var e=this;e.nonEmpty;){if(e.head.listener===t)return!0;e=e.tail}return!1},t.prototype.find=function(t){if(!this.nonEmpty)return null;for(var e=this;e.nonEmpty;){if(e.head.listener===t)return e.head;e=e.tail}return null},t.prototype.toString=function(){for(var t="",e=this;e.nonEmpty;)t+=e.head+" -> ",e=e.tail;return"[List "+(t+="NIL")+"]"},t.NIL=new t(null,null),t}();e.SlotList=n},function(t,e,r){"use strict";Object.defineProperty(e,"__esModule",{value:!0});var n=r(6);e.DeluxeSignal=n.DeluxeSignal;var i=r(7);e.GenericEvent=i.GenericEvent;var o=r(8);e.IOnceSignal=o.IOnceSignal;var s=r(9);e.IPrioritySignal=s.IPrioritySignal;var a=r(10);e.ISignal=a.ISignal;var l=r(11);e.ISlot=l.ISlot;var u=r(12);e.MonoSignal=u.MonoSignal;var c=r(1);e.OnceSignal=c.OnceSignal;var h=r(2);e.PrioritySignal=h.PrioritySignal;var f=r(13);e.Promise=f.Promise;var p=r(3);e.Signal=p.Signal;var y=r(0);e.Slot=y.Slot;var v=r(4);e.SlotList=v.SlotList},function(t,e,r){"use strict";var n=this&&this.__extends||function(){var t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var r in e)e.hasOwnProperty(r)&&(t[r]=e[r])};return function(e,r){function n(){this.constructor=e}t(e,r),e.prototype=null===r?Object.create(r):(n.prototype=r.prototype,new n)}}();Object.defineProperty(e,"__esModule",{value:!0});var i=r(2),o=function(t){function e(e){void 0===e&&(e=null);for(var r=[],n=1;n is not an instance of <"+this._valueClasses[i]+">.");var o=t[0];o&&(o.target&&(o=o.clone(),t[0]=o),o.target=this.target,o.currentTarget=this.target,o.signal=this);for(var s=this.slots;s.nonEmpty;)s.head.execute(t),s=s.tail;if(o&&o.bubbles)for(var a=this.target;a&&a.hasOwnProperty("parent")&&(a=a.parent,null===a.onEventBubbled||(o.currentTarget=a,a.onEventBubbled(o))););},e}(i.PrioritySignal);e.DeluxeSignal=o},function(t,e,r){"use strict";Object.defineProperty(e,"__esModule",{value:!0});var n=function(){function t(t){void 0===t&&(t=!1),this._bubbles=t}return Object.defineProperty(t.prototype,"signal",{get:function(){return this._signal},set:function(t){this._signal=t},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"target",{get:function(){return this._target},set:function(t){this._target=t},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"currentTarget",{get:function(){return this._currentTarget},set:function(t){this._currentTarget=t},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"bubbles",{get:function(){return this._bubbles},set:function(t){this._bubbles=t},enumerable:!0,configurable:!0}),t.prototype.clone=function(){return new t(this._bubbles)},t}();e.GenericEvent=n},function(t,e,r){"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.IOnceSignal=Symbol("IOnceSignal")},function(t,e,r){"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.IPrioritySignal=Symbol("IPrioritySignal")},function(t,e,r){"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.ISignal=Symbol("ISignal")},function(t,e,r){"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.ISlot=Symbol("ISlot")},function(t,e,r){"use strict";Object.defineProperty(e,"__esModule",{value:!0});var n=r(0),i=function(){function t(){for(var t=[],e=0;e."+this._valueClasses[e])},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"numListeners",{get:function(){return this.slot?1:0},enumerable:!0,configurable:!0}),t.prototype.add=function(t){return this.registerListener(t)},t.prototype.addOnce=function(t){return this.registerListener(t,!0)},t.prototype.remove=function(t){if(this.slot&&this.slot.listener===t){var e=this.slot;return this.slot=null,e}return null},t.prototype.removeAll=function(){this.slot&&this.slot.remove()},t.prototype.dispatch=function(){for(var t=[],e=0;e is not an instance of <"+this._valueClasses[i]+">.");this.slot&&this.slot.execute(t)},t.prototype.registerListener=function(t,e){if(void 0===e&&(e=!1),this.slot)throw new Error("You cannot add or addOnce with a listener already added, remove the current listener first.");return this.slot=new n.Slot(t,this,e)},t}();e.MonoSignal=i},function(t,e,r){"use strict";var n=this&&this.__extends||function(){var t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var r in e)e.hasOwnProperty(r)&&(t[r]=e[r])};return function(e,r){function n(){this.constructor=e}t(e,r),e.prototype=null===r?Object.create(r):(n.prototype=r.prototype,new n)}}();Object.defineProperty(e,"__esModule",{value:!0});var i=r(1),o=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return n(e,t),e.prototype.addOnce=function(e){var r=t.prototype.addOnce.call(this,e);return this.isDispatched&&(r.execute(this.valueObjects),r.remove()),r},e.prototype.dispatch=function(){for(var e=[],r=0;r.' + this._valueClasses[i]); //@CHANGED - temp replacement for getQualifiedClassByName() + throw new Error("Invalid valueClasses argument: " + + "item at index " + i + " should be a Class but was:<" + + this._valueClasses[i] + "'>." + this._valueClasses[i]); // @CHANGED - temp replacement for getQualifiedClassByName() } } } @@ -78,8 +78,8 @@ export class MonoSignal implements ISignal { /** @inheritDoc */ public remove(listener: Function): ISlot { - if (this.slot && this.slot.listener == listener) { - var theSlot: ISlot = this.slot; + if (this.slot && this.slot.listener === listener) { + let theSlot: ISlot = this.slot; this.slot = null; return theSlot; } @@ -89,7 +89,9 @@ export class MonoSignal implements ISignal { /** @inheritDoc */ public removeAll(): void { - if (this.slot) this.slot.remove(); + if (this.slot) { + this.slot.remove(); + } } /** @@ -99,18 +101,18 @@ export class MonoSignal implements ISignal { */ public dispatch(...valueObjects): void { // If valueClasses is empty, value objects are not type-checked. - var numValueClasses: number = this._valueClasses.length; - var numValueObjects: number = valueObjects.length; + let numValueClasses: number = this._valueClasses.length; + let numValueObjects: number = valueObjects.length; // Cannot dispatch fewer objects than declared classes. if (numValueObjects < numValueClasses) { - throw new Error('Incorrect number of arguments. ' + - 'Expected at least ' + numValueClasses + ' but received ' + - numValueObjects + '.'); + throw new Error("Incorrect number of arguments. " + + "Expected at least " + numValueClasses + " but received " + + numValueObjects + "."); } // Cannot dispatch differently typed objects than declared classes. - for (var i: number = 0; i < numValueClasses; i++) { + for (let i: number = 0; i < numValueClasses; i++) { // Optimized for the optimistic case that values are correct. if ( valueObjects[i] === null || @@ -119,8 +121,8 @@ export class MonoSignal implements ISignal { continue; } - throw new Error('Value object <' + valueObjects[i] - + '> is not an instance of <' + this._valueClasses[i] + '>.'); + throw new Error("Value object <" + valueObjects[i] + + "> is not an instance of <" + this._valueClasses[i] + ">."); } // Broadcast to the one listener. @@ -132,11 +134,10 @@ export class MonoSignal implements ISignal { protected registerListener(listener: Function, once: boolean = false): ISlot { if (this.slot) { // If the listener exits previously added, definitely don't add it. - throw new Error('You cannot add or addOnce with a listener already added, remove the current listener first.'); + throw new Error("You cannot add or addOnce with a listener already added, remove the current listener first."); } return (this.slot = new Slot(listener, this, once)); } } - diff --git a/src/org/osflash/signals/OnceSignal.ts b/src/org/osflash/signals/OnceSignal.ts index 9e558fb..5f92df1 100644 --- a/src/org/osflash/signals/OnceSignal.ts +++ b/src/org/osflash/signals/OnceSignal.ts @@ -36,7 +36,7 @@ export class OnceSignal implements IOnceSignal { */ constructor(...valueClasses) { // Cannot use super.apply(null, valueClasses), so allow the subclass to call super(valueClasses). - this.valueClasses = (valueClasses.length == 1 && valueClasses[0] instanceof Array) ? valueClasses[0] : valueClasses; + this.valueClasses = (valueClasses.length === 1 && valueClasses[0] instanceof Array) ? valueClasses[0] : valueClasses; } /** @@ -51,11 +51,11 @@ export class OnceSignal implements IOnceSignal { public set valueClasses(value: any[]) { // Clone so the Array cannot be affected from outside. this._valueClasses = value ? value.slice() : []; - for (var i: number = this._valueClasses.length; i--;) { + for (let i: number = this._valueClasses.length; i--;) { if (!(this._valueClasses[i] instanceof Object)) { - throw new Error('Invalid valueClasses argument: ' + - 'item at index ' + i + ' should be a Class but was:<' + - this._valueClasses[i] + '>.' + this._valueClasses[i]); //@CHANGED - temp replacement for getQualifiedClassByName() + throw new Error("Invalid valueClasses argument: " + + "item at index " + i + " should be a Class but was:<" + + this._valueClasses[i] + ">." + this._valueClasses[i]); // @CHANGED - temp replacement for getQualifiedClassByName() } } } @@ -76,8 +76,10 @@ export class OnceSignal implements IOnceSignal { /** @inheritDoc */ public remove(listener: Function): ISlot { - var slot: ISlot = this.slots.find(listener); - if (!slot) return null; + let slot: ISlot = this.slots.find(listener); + if (!slot) { + return null; + } this.slots = this.slots.filterNot(listener); return slot; @@ -96,18 +98,18 @@ export class OnceSignal implements IOnceSignal { public dispatch(...valueObjects): void { // If valueClasses is empty, value objects are not type-checked. - var numValueClasses: number = this._valueClasses.length; - var numValueObjects: number = valueObjects.length; + let numValueClasses: number = this._valueClasses.length; + let numValueObjects: number = valueObjects.length; // Cannot dispatch fewer objects than declared classes. if (numValueObjects < numValueClasses) { - throw new Error('Incorrect number of arguments. ' + - 'Expected at least ' + numValueClasses + ' but received ' + - numValueObjects + '.'); + throw new Error("Incorrect number of arguments. " + + "Expected at least " + numValueClasses + " but received " + + numValueObjects + "."); } // Cannot dispatch differently typed objects than declared classes. - for (var i: number = 0; i < numValueClasses; i++) { + for (let i: number = 0; i < numValueClasses; i++) { // Optimized for the optimistic case that values are correct. if ( valueObjects[i] === null || @@ -116,12 +118,12 @@ export class OnceSignal implements IOnceSignal { continue; } - throw new Error('Value object <' + valueObjects[i] - + '> is not an instance of <' + this._valueClasses[i] + '>.'); + throw new Error("Value object <" + valueObjects[i] + + "> is not an instance of <" + this._valueClasses[i] + ">."); } // Broadcast to listeners. - var slotsToProcess: SlotList = this.slots; + let slotsToProcess: SlotList = this.slots; if (slotsToProcess.nonEmpty) { while (slotsToProcess.nonEmpty) { slotsToProcess.head.execute(valueObjects); @@ -132,7 +134,7 @@ export class OnceSignal implements IOnceSignal { protected registerListener(listener: Function, once: boolean = false): ISlot { if (this.registrationPossible(listener, once)) { - var newSlot: ISlot = new Slot(listener, this, once); + let newSlot: ISlot = new Slot(listener, this, once); this.slots = this.slots.prepend(newSlot); return newSlot; } @@ -141,18 +143,21 @@ export class OnceSignal implements IOnceSignal { } protected registrationPossible(listener: Function, once: boolean): boolean { - if (!this.slots.nonEmpty) return true; + if (!this.slots.nonEmpty) { + return true; + } - var existingSlot: ISlot = this.slots.find(listener); - if (!existingSlot) return true; + let existingSlot: ISlot = this.slots.find(listener); + if (!existingSlot) { + return true; + } - if (existingSlot.once != once) { + if (existingSlot.once !== once) { // If the listener was previously added, definitely don't add it again. // But throw an exception if their once values differ. - throw new Error('You cannot addOnce() then add() the same listener without removing the relationship first.'); + throw new Error("You cannot addOnce() then add() the same listener without removing the relationship first."); } return false; // Listener was already registered. } } - diff --git a/src/org/osflash/signals/PrioritySignal.ts b/src/org/osflash/signals/PrioritySignal.ts index 117dd52..98333ea 100644 --- a/src/org/osflash/signals/PrioritySignal.ts +++ b/src/org/osflash/signals/PrioritySignal.ts @@ -7,7 +7,7 @@ export class PrioritySignal extends Signal implements IPrioritySignal { constructor(...valueClasses) { // Cannot use super.apply(null, valueClasses), so allow the subclass to call super(valueClasses). - valueClasses = (valueClasses.length == 1 && valueClasses[0] instanceof Array) ? valueClasses[0] : valueClasses; + valueClasses = (valueClasses.length === 1 && valueClasses[0] instanceof Array) ? valueClasses[0] : valueClasses; super(valueClasses); } @@ -37,13 +37,11 @@ export class PrioritySignal extends Signal implements IPrioritySignal { protected registerListenerWithPriority(listener: Function, once: boolean = false, priority: number = 0): ISlot { if (this.registrationPossible(listener, once)) { - var slot: ISlot = new Slot(listener, this, once, priority); + let slot: ISlot = new Slot(listener, this, once, priority); this.slots = this.slots.insertWithPriority(slot); return slot; } return this.slots.find(listener); } - } - diff --git a/src/org/osflash/signals/Promise.ts b/src/org/osflash/signals/Promise.ts index 7201c5d..2e61d20 100644 --- a/src/org/osflash/signals/Promise.ts +++ b/src/org/osflash/signals/Promise.ts @@ -8,7 +8,7 @@ export class Promise extends OnceSignal { /** @inheritDoc */ /*override*/ public addOnce(listener: Function): ISlot { - var slot: ISlot = super.addOnce(listener); + let slot: ISlot = super.addOnce(listener); if (this.isDispatched) { slot.execute(this.valueObjects); slot.remove(); @@ -25,12 +25,10 @@ export class Promise extends OnceSignal { public dispatch(...valueObjects): void { if (this.isDispatched) { throw new Error("You cannot dispatch() a Promise more than once"); - } - else { + } else { this.isDispatched = true; this.valueObjects = valueObjects; super.dispatch.apply(this, valueObjects); } } } - diff --git a/src/org/osflash/signals/Signal.ts b/src/org/osflash/signals/Signal.ts index 8cb2a69..d7ba897 100644 --- a/src/org/osflash/signals/Signal.ts +++ b/src/org/osflash/signals/Signal.ts @@ -32,7 +32,7 @@ export class Signal extends OnceSignal implements ISignal { */ constructor(...valueClasses) { // Cannot use super.apply(null, valueClasses), so allow the subclass to call super(valueClasses). - valueClasses = (valueClasses.length == 1 && valueClasses[0] instanceof Array) ? valueClasses[0] : valueClasses; + valueClasses = (valueClasses.length === 1 && valueClasses[0] instanceof Array) ? valueClasses[0] : valueClasses; super(valueClasses); } @@ -46,4 +46,3 @@ export class Signal extends OnceSignal implements ISignal { return this.registerListener(listener); } } - diff --git a/src/org/osflash/signals/Slot.ts b/src/org/osflash/signals/Slot.ts index 2cd803f..9edebdf 100644 --- a/src/org/osflash/signals/Slot.ts +++ b/src/org/osflash/signals/Slot.ts @@ -39,8 +39,14 @@ export class Slot implements ISlot { * @inheritDoc */ public execute0(): void { - if (!this._enabled) return; - if (this._once) this.remove(); + if (!this._enabled) { + return; + } + + if (this._once) { + this.remove(); + } + if (this._params && this._params.length) { this._listener.apply(null, this._params); return; @@ -52,8 +58,14 @@ export class Slot implements ISlot { * @inheritDoc */ public execute1(value: Object): void { - if (!this._enabled) return; - if (this._once) this.remove(); + if (!this._enabled) { + return; + } + + if (this._once) { + this.remove(); + } + if (this._params && this._params.length) { this._listener.apply(null, [value].concat(this._params)); return; @@ -65,8 +77,13 @@ export class Slot implements ISlot { * @inheritDoc */ public execute(valueObjects: any[]): void { - if (!this._enabled) return; - if (this._once) this.remove(); + if (!this._enabled) { + return; + } + + if (this._once) { + this.remove(); + } // If we have parameters, add them to the valueObject // Note: This could be expensive if we're after the fastest dispatch possible. @@ -75,20 +92,16 @@ export class Slot implements ISlot { } // NOTE: simple ifs are faster than switch: http://jacksondunstan.com/articles/1007 - var numValueObjects: number = valueObjects.length; - if (numValueObjects == 0) { + let numValueObjects: number = valueObjects.length; + if (numValueObjects === 0) { this._listener(); - } - else if (numValueObjects == 1) { + } else if (numValueObjects === 1) { this._listener(valueObjects[0]); - } - else if (numValueObjects == 2) { + } else if (numValueObjects === 2) { this._listener(valueObjects[0], valueObjects[1]); - } - else if (numValueObjects == 3) { + } else if (numValueObjects === 3) { this._listener(valueObjects[0], valueObjects[1], valueObjects[2]); - } - else { + } else { this._listener.apply(null, valueObjects); } } @@ -103,8 +116,9 @@ export class Slot implements ISlot { } public set listener(value: Function) { - if (null == value) throw new Error( - 'Given listener is null.\nDid you want to set enabled to false instead?'); + if (null == value) { + throw new Error("Given listener is null.\nDid you want to set enabled to false instead?"); + } this.verifyListener(value); this._listener = value; @@ -165,12 +179,11 @@ export class Slot implements ISlot { protected verifyListener(listener: Function): void { if (null == listener) { - throw new Error('Given listener is null.'); + throw new Error("Given listener is null."); } if (null == this._signal) { - throw new Error('Internal signal reference has not been set yet.'); + throw new Error("Internal signal reference has not been set yet."); } - } } diff --git a/src/org/osflash/signals/SlotList.ts b/src/org/osflash/signals/SlotList.ts index 47009a0..991993c 100644 --- a/src/org/osflash/signals/SlotList.ts +++ b/src/org/osflash/signals/SlotList.ts @@ -1,4 +1,5 @@ import { ISlot } from "./ISlot"; + /** * The SlotList class represents an immutable list of Slot objects. * @@ -11,6 +12,11 @@ export class SlotList { */ public static NIL: SlotList = new SlotList(null, null); + // Although those variables are not const, they would be if AS3 would handle it correctly. + public head: ISlot; + public tail: SlotList; + public nonEmpty: boolean = false; + /** * Creates and returns a new SlotList object. * @@ -26,40 +32,39 @@ export class SlotList { */ constructor(head: ISlot, tail: SlotList = null) { if (!head && !tail) { - if (SlotList.NIL) - throw new Error('Parameters head and tail are null. Use the NIL element instead.'); + if (SlotList.NIL) { + throw new Error("Parameters head and tail are null. Use the NIL element instead."); + } - //this is the NIL element as per definition + // this is the NIL element as per definition this.nonEmpty = false; - } - else if (!head) { - throw new Error('Parameter head cannot be null.'); - } - else { + } else if (!head) { + throw new Error("Parameter head cannot be null."); + } else { this.head = head; this.tail = tail || SlotList.NIL; this.nonEmpty = true; } } - // Although those variables are not const, they would be if AS3 would handle it correctly. - public head: ISlot; - public tail: SlotList; - public nonEmpty: boolean = false; - /** * The number of slots in the list. */ public get length(): number { - if (!this.nonEmpty) return 0; - if (this.tail == SlotList.NIL) return 1; + if (!this.nonEmpty) { + return 0; + } + + if (this.tail === SlotList.NIL) { + return 1; + } // We could cache the length, but it would make methods like filterNot unnecessarily complicated. // Instead we assume that O(n) is okay since the length property is used in rare cases. // We could also cache the length lazy, but that is a waste of another 8b per list node (at least). - var result: number = 0; - var p: SlotList = this; + let result: number = 0; + let p: SlotList = this; while (p.nonEmpty) { ++result; @@ -89,22 +94,30 @@ export class SlotList { * @return A list consisting of all elements of this list followed by slot. */ public append(slot: ISlot): SlotList { - if (!slot) return this; - if (!this.nonEmpty) return new SlotList(slot); + if (!slot) { + return this; + } + + if (!this.nonEmpty) { + return new SlotList(slot); + } + // Special case: just one slot currently in the list. - if (this.tail == SlotList.NIL) + if (this.tail === SlotList.NIL) { return new SlotList(slot).prepend(this.head); + } // The list already has two or more slots. // We have to build a new list with cloned items because they are immutable. - var wholeClone: SlotList = new SlotList(this.head); - var subClone: SlotList = wholeClone; - var current: SlotList = this.tail; + let wholeClone: SlotList = new SlotList(this.head); + let subClone: SlotList = wholeClone; + let current: SlotList = this.tail; while (current.nonEmpty) { subClone = subClone.tail = new SlotList(current.head); current = current.tail; } + // Append the new slot last. subClone.tail = new SlotList(slot); return wholeClone; @@ -119,15 +132,20 @@ export class SlotList { * @throws ArgumentError ArgumentError: Parameter head cannot be null. */ public insertWithPriority(slot: ISlot): SlotList { - if (!this.nonEmpty) return new SlotList(slot); + if (!this.nonEmpty) { + return new SlotList(slot); + } + + let priority: number = slot.priority; - var priority: number = slot.priority; // Special case: new slot has the highest priority. - if (priority > this.head.priority) return this.prepend(slot); + if (priority > this.head.priority) { + return this.prepend(slot); + } - var wholeClone: SlotList = new SlotList(this.head); - var subClone: SlotList = wholeClone; - var current: SlotList = this.tail; + let wholeClone: SlotList = new SlotList(this.head); + let subClone: SlotList = wholeClone; + let current: SlotList = this.tail; // Find a slot with lower priority and go in front of it. while (current.nonEmpty) { @@ -151,17 +169,21 @@ export class SlotList { * @return A list consisting of all elements of this list that do not have listener. */ public filterNot(listener: Function): SlotList { - if (!this.nonEmpty || listener == null) return this; + if (!this.nonEmpty || listener == null) { + return this; + } - if (listener == this.head.listener) return this.tail; + if (listener === this.head.listener) { + return this.tail; + } // The first item wasn't a match so the filtered list will contain it. - var wholeClone: SlotList = new SlotList(this.head); - var subClone: SlotList = wholeClone; - var current: SlotList = this.tail; + let wholeClone: SlotList = new SlotList(this.head); + let subClone: SlotList = wholeClone; + let current: SlotList = this.tail; while (current.nonEmpty) { - if (current.head.listener == listener) { + if (current.head.listener === listener) { // Splice out the current head. subClone.tail = current.tail; return wholeClone; @@ -179,11 +201,17 @@ export class SlotList { * Determines whether the supplied listener Function is contained within this list */ public contains(listener: Function): boolean { - if (!this.nonEmpty) return false; + if (!this.nonEmpty) { + return false; + } + + let p: SlotList = this; - var p: SlotList = this; while (p.nonEmpty) { - if (p.head.listener == listener) return true; + if (p.head.listener === listener) { + return true; + } + p = p.tail; } @@ -197,11 +225,17 @@ export class SlotList { * Returns null if no such ISlot instance exists or the list is empty. */ public find(listener: Function): ISlot { - if (!this.nonEmpty) return null; + if (!this.nonEmpty) { + return null; + } + + let p: SlotList = this; - var p: SlotList = this; while (p.nonEmpty) { - if (p.head.listener == listener) return p.head; + if (p.head.listener === listener) { + return p.head; + } + p = p.tail; } @@ -209,8 +243,8 @@ export class SlotList { } public toString(): string { - var buffer: string = ''; - var p: SlotList = this; + let buffer: string = ""; + let p: SlotList = this; while (p.nonEmpty) { buffer += p.head + " -> "; @@ -222,4 +256,3 @@ export class SlotList { return "[List " + buffer + "]"; } } - diff --git a/src/org/osflash/signals/events/GenericEvent.ts b/src/org/osflash/signals/events/GenericEvent.ts index 6480249..f02a6c3 100644 --- a/src/org/osflash/signals/events/GenericEvent.ts +++ b/src/org/osflash/signals/events/GenericEvent.ts @@ -57,4 +57,3 @@ export class GenericEvent implements IEvent { return new GenericEvent(this._bubbles); } } - diff --git a/src/org/osflash/signals/events/IEvent.ts b/src/org/osflash/signals/events/IEvent.ts index 598a1fe..e39dbba 100644 --- a/src/org/osflash/signals/events/IEvent.ts +++ b/src/org/osflash/signals/events/IEvent.ts @@ -21,4 +21,3 @@ export interface IEvent { /** Returns a new copy of the instance. */ clone(): IEvent; } - diff --git a/test/entry.ts b/test/entry.ts index 91d1b46..680ee33 100644 --- a/test/entry.ts +++ b/test/entry.ts @@ -1,4 +1,4 @@ -/// +/// import "bluebird/js/browser/bluebird"; import "es6-symbol/implement"; diff --git a/test/org/osflash/signals/AmbiguousRelationshipTest.test.ts b/test/org/osflash/signals/AmbiguousRelationshipTest.test.ts index cf2f93e..795a5ad 100644 --- a/test/org/osflash/signals/AmbiguousRelationshipTest.test.ts +++ b/test/org/osflash/signals/AmbiguousRelationshipTest.test.ts @@ -44,5 +44,4 @@ describe("AmbiguousRelationshipTest", () => { instance.addOnce(failIfCalled); assert.equal(1, instance.numListeners); }); - }); diff --git a/test/org/osflash/signals/MonoSignalDispatchArgsTest.test.ts b/test/org/osflash/signals/MonoSignalDispatchArgsTest.test.ts index ef9d488..2b51678 100644 --- a/test/org/osflash/signals/MonoSignalDispatchArgsTest.test.ts +++ b/test/org/osflash/signals/MonoSignalDispatchArgsTest.test.ts @@ -58,5 +58,4 @@ describe("MonoSignalDispatchArgsTest", () => { signal.dispatch(new Inherited1()); signal.dispatch(new Inherited2()); }); - }); diff --git a/tslint.json b/tslint.json index 80daf3f..3c951e2 100644 --- a/tslint.json +++ b/tslint.json @@ -1,20 +1,34 @@ { "rules": { "class-name": true, - "comment-format": [true, "check-space"], + "comment-format": [ + true, + "check-space" + ], "curly": true, "eofline": true, "forin": true, - "indent": [true, "spaces"], + "indent": [ + true, + "spaces" + ], "label-position": true, - "label-undefined": true, - "max-line-length": [true, 140], + "max-line-length": [ + true, + 140 + ], "member-access": true, "member-ordering": [ - true, - "public-before-private", - "static-before-instance", - "variables-before-functions" + true, { + "order": [ + "static-field", + "instance-field", + "constructor", + "public-instance-method", + "protected-instance-method", + "private-instance-method" + ] + } ], "no-arg": true, "no-bitwise": true, @@ -28,12 +42,10 @@ ], "no-construct": true, "no-debugger": true, - "no-duplicate-key": true, "no-duplicate-variable": true, "no-empty": true, "no-eval": true, - "no-inferrable-types": true, - "no-internal-module": true, + "no-inferrable-types": false, "no-shadowed-variable": true, "no-string-literal": true, "no-switch-case-fall-through": false, @@ -51,26 +63,33 @@ "check-else", "check-whitespace" ], - "quotemark": [true, "double", "avoid-escape"], - "radix": true, - "semicolon": true, - "trailing-comma": false, - "triple-equals": [true, "allow-null-check"], + "quotemark": [ + true, + "double", + "avoid-escape" + ], + "radix":true, + "semicolon": [ + true, + "always" + ], + "trailing-comma": [ + false + ], + "triple-equals": [ + true, + "allow-null-check" + ], "typedef-whitespace": [ true, { - "call-signature": "nospace", - "index-signature": "nospace", - "parameter": "nospace", - "property-declaration": "nospace", - "variable-declaration": "nospace" - }], - "variable-name": [ - true, - "ban-keywords", - "check-format", - "allow-leading-underscore", - "allow-trailing-underscore" + "call-signature":"nospace", + "index-signature":"nospace", + "parameter":"nospace", + "property-declaration":"nospace", + "variable-declaration":"nospace" + } ], + "variable-name": false, "whitespace": [ true, "check-branch",