diff --git a/StronglyTypesEvents-0.2.0.ts b/StronglyTypedEvents-0.2.0.ts similarity index 95% rename from StronglyTypesEvents-0.2.0.ts rename to StronglyTypedEvents-0.2.0.ts index 64a91c90..c763310e 100644 --- a/StronglyTypesEvents-0.2.0.ts +++ b/StronglyTypedEvents-0.2.0.ts @@ -1,278 +1,278 @@ -/// - -/*! - * Strongly Typed Events for TypeScript - * https://github.com/KeesCBakker/StronlyTypedEvents/ - * http://keestalkstech.com - * - * Copyright Kees C. Bakker / KeesTalksTech - * Released under the MIT license - */ - -/** - * Base class for implementation of the dispatcher. It facilitates the subscribe - * and unsubscribe methods based on generic handlers. The TEventType specifies - * the type of event that should be exposed. Use the asEvent to expose the - * dispatcher as event. - */ -abstract class DispatcherBase implements ISubscribable { - - private _wrap = new DispatcherWrapper(this); - protected _subscriptions: Array = new Array(); - - /** - * Subscribe to the event dispatcher. - * @param fn The event handler that is called when the event is dispatched. - */ - subscribe(fn: TEventHandler): void { - if (fn) { - this._subscriptions.push(fn); - } - } - - /** - * Unsubscribes the handler from the dispatcher. - * @param fn The event handler. - */ - unsubscribe(fn: TEventHandler): void { - let i = this._subscriptions.indexOf(fn); - if (i > -1) { - this._subscriptions.splice(i, 1); - } - } - - /** - * Creates an event from the dispatcher. Will return the dispatcher - * in a wrapper. This will prevent exposure of any dispatcher methods. - */ - public asEvent(): ISubscribable { - return this._wrap; - } -} - -/** - * Dispatcher implementation for events. Can be used to subscribe, unsubscribe - * or dispatch events. Use the ToEvent() method to expose the event. - */ -class EventDispatcher extends DispatcherBase> implements IEvent -{ - /** - * Dispatches the event. - * @param sender The sender. - * @param args The arguments object. - */ - dispatch(sender: TSender, args: TArgs): void { - for (let handler of this._subscriptions) { - handler(sender, args); - } - } - - /** - * Dispatches the events thread. - * @param sender The sender. - * @param args The arguments object. - */ - dispatchAsync(sender: TSender, args: TArgs): void { - - for (let handler of this._subscriptions) { - this.excuteAsync(sender, args, handler); - } - } - - private excuteAsync(sender: TSender, args: TArgs, handler: IEventHandler): void { - window.setTimeout(() => handler(sender, args), 0); - } -} - -/** The dispatcher handles the storage of subsciptions and facilitates - * subscription, unsubscription and dispatching of a simple event */ -class SimpleEventDispatcher extends DispatcherBase> implements ISimpleEvent -{ - /** - * Dispatches the event. - * @param args The arguments object. - */ - dispatch(args: TArgs): void { - for (let handler of this._subscriptions) { - handler(args); - } - } - - /** - * Dispatches the events thread. - * @param args The arguments object. - */ - dispatchAsync(args: TArgs): void { - - for (let handler of this._subscriptions) { - this.excuteAsync( args, handler); - } - } - - private excuteAsync(args: TArgs, handler: ISimpleEventHandler): void { - window.setTimeout(() => handler(args), 0); - } -} - - -/** - * Hides the implementation of the event dispatcher. Will expose methods that - * are relevent to the event. - */ -class DispatcherWrapper implements ISubscribable -{ - private _subscribe: (fn: THandlerType) => void; - private _unsubscribe: (fn: THandlerType) => void; - - /** - * Creates a new EventDispatcherWrapper instance. - * @param dispatcher The dispatcher. - */ - constructor(dispatcher: ISubscribable) { - this._subscribe = (fn: THandlerType) => dispatcher.subscribe(fn); - this._unsubscribe = (fn: THandlerType) => dispatcher.unsubscribe(fn); - } - - /** - * Subscribe to the event dispatcher. - * @param fn The event handler that is called when the event is dispatched. - */ - public subscribe(fn: THandlerType): void { - this._subscribe(fn); - } - - /** - * Unsubscribe from the event dispatcher. - * @param fn The event handler that is called when the event is dispatched. - */ - public unsubscribe(fn: THandlerType): void { - this._unsubscribe(fn); - } -} - -/** Base class for event lists classes. Implements the get and remove. */ -abstract class EventListBase { - - private _events: { [name: string]: TEventDispatcher; } = {}; - - /** - * Gets the dispatcher associated with the name. - * @param name The name of the event. - */ - get(name: string): TEventDispatcher { - - let event = this._events[name]; - - if (event) { - return event; - } - - event = this.createDispatcher(); - this._events[name] = event; - return event; - } - - /** - * Removes the dispatcher associated with the name. - * @param name The name of the event. - */ - remove(name: string): void { - this._events[name] = null; - } - - /** - * Creates a new dispatcher instance. - */ - protected abstract createDispatcher(): TEventDispatcher; -} - -/** - * Storage class for multiple events that are accessible by name. - * Events dispatchers are automatically created. - */ -class EventList extends EventListBase> { - - /** - * Creates a new dispatcher instance. - */ - protected createDispatcher(): EventDispatcher { - return new EventDispatcher(); - } -} - -/** - * Storage class for multiple simple events that are accessible by name. - * Events dispatchers are automatically created. - */ -class SimpleEventList extends EventListBase> { - - /** - * Creates a new dispatcher instance. - */ - protected createDispatcher(): SimpleEventDispatcher { - return new SimpleEventDispatcher(); - } -} - -/** - * Extends objects with event handling capabilities. - */ -abstract class EventHandlingBase implements IEventHandling { - - private _events = new EventList(); - - /** - * Gets the list with all the event dispatchers. - */ - protected get events(): EventList { - return this._events; - } - - /** - * Subscribes to the event with the specified name. - * @param name The name of the event. - * @param fn The event handler. - */ - subscribe(name: string, fn: IEventHandler): void { - this._events.get(name).subscribe(fn); - } - - /** - * Unsubscribes from the event with the specified name. - * @param name The name of the event. - * @param fn The event handler. - */ - unsubscribe(name: string, fn: IEventHandler): void { - this._events.get(name).unsubscribe(fn); - } -} - -/** - * Extends objects with simple event handling capabilities. - */ -abstract class SimpleEventHandlingBase implements ISimpleEventHandling { - - private _events = new SimpleEventList(); - - protected get events(): SimpleEventList { - return this._events; - } - - /** - * Subscribes to the event with the specified name. - * @param name The name of the event. - * @param fn The event handler. - */ - subscribe(name: string, fn: ISimpleEventHandler): void { - this._events.get(name).subscribe(fn); - } - - /** - * Unsubscribes from the event with the specified name. - * @param name The name of the event. - * @param fn The event handler. - */ - unsubscribe(name: string, fn: ISimpleEventHandler): void { - this._events.get(name).unsubscribe(fn); - } +/// + +/*! + * Strongly Typed Events for TypeScript + * https://github.com/KeesCBakker/StronlyTypedEvents/ + * http://keestalkstech.com + * + * Copyright Kees C. Bakker / KeesTalksTech + * Released under the MIT license + */ + +/** + * Base class for implementation of the dispatcher. It facilitates the subscribe + * and unsubscribe methods based on generic handlers. The TEventType specifies + * the type of event that should be exposed. Use the asEvent to expose the + * dispatcher as event. + */ +abstract class DispatcherBase implements ISubscribable { + + private _wrap = new DispatcherWrapper(this); + protected _subscriptions: Array = new Array(); + + /** + * Subscribe to the event dispatcher. + * @param fn The event handler that is called when the event is dispatched. + */ + subscribe(fn: TEventHandler): void { + if (fn) { + this._subscriptions.push(fn); + } + } + + /** + * Unsubscribes the handler from the dispatcher. + * @param fn The event handler. + */ + unsubscribe(fn: TEventHandler): void { + let i = this._subscriptions.indexOf(fn); + if (i > -1) { + this._subscriptions.splice(i, 1); + } + } + + /** + * Creates an event from the dispatcher. Will return the dispatcher + * in a wrapper. This will prevent exposure of any dispatcher methods. + */ + public asEvent(): ISubscribable { + return this._wrap; + } +} + +/** + * Dispatcher implementation for events. Can be used to subscribe, unsubscribe + * or dispatch events. Use the ToEvent() method to expose the event. + */ +class EventDispatcher extends DispatcherBase> implements IEvent +{ + /** + * Dispatches the event. + * @param sender The sender. + * @param args The arguments object. + */ + dispatch(sender: TSender, args: TArgs): void { + for (let handler of this._subscriptions) { + handler(sender, args); + } + } + + /** + * Dispatches the events thread. + * @param sender The sender. + * @param args The arguments object. + */ + dispatchAsync(sender: TSender, args: TArgs): void { + + for (let handler of this._subscriptions) { + this.excuteAsync(sender, args, handler); + } + } + + private excuteAsync(sender: TSender, args: TArgs, handler: IEventHandler): void { + window.setTimeout(() => handler(sender, args), 0); + } +} + +/** The dispatcher handles the storage of subsciptions and facilitates + * subscription, unsubscription and dispatching of a simple event */ +class SimpleEventDispatcher extends DispatcherBase> implements ISimpleEvent +{ + /** + * Dispatches the event. + * @param args The arguments object. + */ + dispatch(args: TArgs): void { + for (let handler of this._subscriptions) { + handler(args); + } + } + + /** + * Dispatches the events thread. + * @param args The arguments object. + */ + dispatchAsync(args: TArgs): void { + + for (let handler of this._subscriptions) { + this.excuteAsync( args, handler); + } + } + + private excuteAsync(args: TArgs, handler: ISimpleEventHandler): void { + window.setTimeout(() => handler(args), 0); + } +} + + +/** + * Hides the implementation of the event dispatcher. Will expose methods that + * are relevent to the event. + */ +class DispatcherWrapper implements ISubscribable +{ + private _subscribe: (fn: THandlerType) => void; + private _unsubscribe: (fn: THandlerType) => void; + + /** + * Creates a new EventDispatcherWrapper instance. + * @param dispatcher The dispatcher. + */ + constructor(dispatcher: ISubscribable) { + this._subscribe = (fn: THandlerType) => dispatcher.subscribe(fn); + this._unsubscribe = (fn: THandlerType) => dispatcher.unsubscribe(fn); + } + + /** + * Subscribe to the event dispatcher. + * @param fn The event handler that is called when the event is dispatched. + */ + public subscribe(fn: THandlerType): void { + this._subscribe(fn); + } + + /** + * Unsubscribe from the event dispatcher. + * @param fn The event handler that is called when the event is dispatched. + */ + public unsubscribe(fn: THandlerType): void { + this._unsubscribe(fn); + } +} + +/** Base class for event lists classes. Implements the get and remove. */ +abstract class EventListBase { + + private _events: { [name: string]: TEventDispatcher; } = {}; + + /** + * Gets the dispatcher associated with the name. + * @param name The name of the event. + */ + get(name: string): TEventDispatcher { + + let event = this._events[name]; + + if (event) { + return event; + } + + event = this.createDispatcher(); + this._events[name] = event; + return event; + } + + /** + * Removes the dispatcher associated with the name. + * @param name The name of the event. + */ + remove(name: string): void { + this._events[name] = null; + } + + /** + * Creates a new dispatcher instance. + */ + protected abstract createDispatcher(): TEventDispatcher; +} + +/** + * Storage class for multiple events that are accessible by name. + * Events dispatchers are automatically created. + */ +class EventList extends EventListBase> { + + /** + * Creates a new dispatcher instance. + */ + protected createDispatcher(): EventDispatcher { + return new EventDispatcher(); + } +} + +/** + * Storage class for multiple simple events that are accessible by name. + * Events dispatchers are automatically created. + */ +class SimpleEventList extends EventListBase> { + + /** + * Creates a new dispatcher instance. + */ + protected createDispatcher(): SimpleEventDispatcher { + return new SimpleEventDispatcher(); + } +} + +/** + * Extends objects with event handling capabilities. + */ +abstract class EventHandlingBase implements IEventHandling { + + private _events = new EventList(); + + /** + * Gets the list with all the event dispatchers. + */ + protected get events(): EventList { + return this._events; + } + + /** + * Subscribes to the event with the specified name. + * @param name The name of the event. + * @param fn The event handler. + */ + subscribe(name: string, fn: IEventHandler): void { + this._events.get(name).subscribe(fn); + } + + /** + * Unsubscribes from the event with the specified name. + * @param name The name of the event. + * @param fn The event handler. + */ + unsubscribe(name: string, fn: IEventHandler): void { + this._events.get(name).unsubscribe(fn); + } +} + +/** + * Extends objects with simple event handling capabilities. + */ +abstract class SimpleEventHandlingBase implements ISimpleEventHandling { + + private _events = new SimpleEventList(); + + protected get events(): SimpleEventList { + return this._events; + } + + /** + * Subscribes to the event with the specified name. + * @param name The name of the event. + * @param fn The event handler. + */ + subscribe(name: string, fn: ISimpleEventHandler): void { + this._events.get(name).subscribe(fn); + } + + /** + * Unsubscribes from the event with the specified name. + * @param name The name of the event. + * @param fn The event handler. + */ + unsubscribe(name: string, fn: ISimpleEventHandler): void { + this._events.get(name).unsubscribe(fn); + } } \ No newline at end of file diff --git a/examples/example.clock.ts b/examples/example.clock.ts index c8717f70..5776e8cb 100644 --- a/examples/example.clock.ts +++ b/examples/example.clock.ts @@ -1,5 +1,5 @@ -/// -/// +/// +/// /* * This clock example shows how to use Strongly Typed Events diff --git a/examples/example.imageloader.ts b/examples/example.imageloader.ts index 5038bd60..bb3abd5c 100644 --- a/examples/example.imageloader.ts +++ b/examples/example.imageloader.ts @@ -1,5 +1,5 @@ -/// -/// +/// +/// window.onload = () => { diff --git a/examples/example.pulse-generator.ts b/examples/example.pulse-generator.ts index 957a5147..996be240 100644 --- a/examples/example.pulse-generator.ts +++ b/examples/example.pulse-generator.ts @@ -1,5 +1,5 @@ -/// -/// +/// +/// window.onload = function () { diff --git a/tests/StronglyTypesEvents-0.2.0.tests.html b/tests/StronglyTypedEvents-0.2.0.tests.html similarity index 96% rename from tests/StronglyTypesEvents-0.2.0.tests.html rename to tests/StronglyTypedEvents-0.2.0.tests.html index 6cb083cd..0b8e832e 100644 --- a/tests/StronglyTypesEvents-0.2.0.tests.html +++ b/tests/StronglyTypedEvents-0.2.0.tests.html @@ -1,16 +1,16 @@ - - - - - - Strongly Typed Events for TypeScript - - - -
-
- - - - - + + + + + + Strongly Typed Events for TypeScript + + + +
+
+ + + + + diff --git a/tests/StronglyTypesEvents-0.2.0.tests.ts b/tests/StronglyTypedEvents-0.2.0.tests.ts similarity index 94% rename from tests/StronglyTypesEvents-0.2.0.tests.ts rename to tests/StronglyTypedEvents-0.2.0.tests.ts index 0edd9469..67c05970 100644 --- a/tests/StronglyTypesEvents-0.2.0.tests.ts +++ b/tests/StronglyTypedEvents-0.2.0.tests.ts @@ -1,302 +1,302 @@ -/// -/// -/// - -QUnit.test("Testing subscribe / unsubsribe - event as property", (assert) => { - - class MyEventTester { - private _myEvent: EventDispatcher = new EventDispatcher(); - - get myEvent(): IEvent { - return this._myEvent.asEvent(); - } - - signal(str: string): void { - this._myEvent.dispatch(this, str); - } - } - - var s = new MyEventTester(); - var r = null; - - var handler = (sender: MyEventTester, args: string) => { - r = args; - }; - - s.myEvent.subscribe(handler); - s.signal('Test1'); - assert.equal(r, 'Test1'); - - s.myEvent.unsubscribe(handler); - s.signal('Test2'); - assert.equal(r, 'Test1'); -}); - -QUnit.test("Testing subscribe / unsubsribe - event on interface" , (assert) => { - - interface IMyEventTester { - myEvent(): IEvent; - - signal(str: string); - } - - class MyEventTester implements IMyEventTester { - private _myEvent: EventDispatcher = new EventDispatcher(); - - myEvent(): IEvent { - return this._myEvent.asEvent(); - } - - signal(str: string): void { - this._myEvent.dispatch(this, str); - } - } - - var s: IMyEventTester = new MyEventTester(); - var r = null; - - var handler = (sender: IMyEventTester, args: string) => { - r = args; - }; - - s.myEvent().subscribe(handler); - s.signal('Test1'); - assert.equal(r, 'Test1'); - - s.myEvent().unsubscribe(handler); - s.signal('Test2'); - assert.equal(r, 'Test1'); -}); - -QUnit.test("Testing event list", (assert) => { - - var events = new EventList(); - var result: string; - - events.get('Test1').subscribe((sender, args) => result = args); - events.get('Test1').dispatch(this, 'Testing 123'); - assert.equal(result, 'Testing 123'); - - events.get('Test2').dispatch(this, 'Testing 456'); - assert.equal(result, 'Testing 123'); - - events.get('Test2').subscribe((sender, args) => result = args); - events.get('Test2').dispatch(this, 'Testing 789'); - assert.equal(result, 'Testing 789'); - - events.get('Test3').asEvent().subscribe((sender, args) => result = args); - events.get('Test3').dispatch(this, 'Testing 42'); - assert.equal(result, 'Testing 42'); - -}); - -QUnit.test('Testing EventHandlingBase', (assert) => { - - class MyTester extends EventHandlingBase { - - signal(name: string, str: string): void { - this.events.get(name).dispatch(this, str); - } - } - - var t = new MyTester(); - var result: string; - - t.subscribe('Test1', (sender, args) => result = args); - t.signal('Test1', 'Testing 123'); - assert.equal(result, 'Testing 123'); - - t.signal('Test2', 'Testing 456'); - assert.equal(result, 'Testing 123'); - - t.subscribe('Test2', (sender, args) => result = args); - t.signal('Test2', 'Testing 789'); - assert.equal(result, 'Testing 789'); -}); - -QUnit.test("Testing subscribe / unsubsribe - simple event as property", (assert) => { - - class MyEventTester { - private _myEvent: SimpleEventDispatcher = new SimpleEventDispatcher(); - - get myEvent(): ISimpleEvent { - return this._myEvent; - } - - signal(str: string): void { - this._myEvent.dispatch(str); - } - } - - var s = new MyEventTester(); - var r = null; - - var handler = (args: string) => { - r = args; - }; - - s.myEvent.subscribe(handler); - s.signal('Test1'); - assert.equal(r, 'Test1'); - - s.myEvent.unsubscribe(handler); - s.signal('Test2'); - assert.equal(r, 'Test1'); -}); - -QUnit.test("Testing subscribe / unsubsribe - simple event on interface", (assert) => { - - interface IMyEventTester { - myEvent(): ISimpleEvent; - - signal(str: string); - } - - class MyEventTester implements IMyEventTester { - private _myEvent: SimpleEventDispatcher = new SimpleEventDispatcher(); - - myEvent(): ISimpleEvent { - return this._myEvent; - } - - signal(str: string): void { - this._myEvent.dispatch(str); - } - } - - var s: IMyEventTester = new MyEventTester(); - var r = null; - - var handler = (args: string) => { - r = args; - }; - - s.myEvent().subscribe(handler); - s.signal('Test1'); - assert.equal(r, 'Test1'); - - s.myEvent().unsubscribe(handler); - s.signal('Test2'); - assert.equal(r, 'Test1'); -}); - -QUnit.test("Testing simple event list", (assert) => { - - var events = new SimpleEventList(); - var result: string; - - events.get('Test1').subscribe((args) => result = args); - events.get('Test1').dispatch('Testing 123'); - assert.equal(result, 'Testing 123'); - - events.get('Test2').dispatch('Testing 456'); - assert.equal(result, 'Testing 123'); - - events.get('Test2').subscribe((args) => result = args); - events.get('Test2').dispatch('Testing 789'); - assert.equal(result, 'Testing 789'); - - events.get('Test3').asEvent().subscribe((args) => result = args); - events.get('Test3').dispatch('Testing 42'); - assert.equal(result, 'Testing 42'); - -}); - -QUnit.test('Testing SimpleEventHandlingBase', (assert) => { - - class MyTester extends SimpleEventHandlingBase { - - signal(name: string, str: string): void { - this.events.get(name).dispatch(str); - } - } - - var t = new MyTester(); - var result: string; - - t.subscribe('Test1', (args) => result = args); - t.signal('Test1', 'Testing 123'); - assert.equal(result, 'Testing 123'); - - t.signal('Test2', 'Testing 456'); - assert.equal(result, 'Testing 123'); - - t.subscribe('Test2', (args) => result = args); - t.signal('Test2', 'Testing 789'); - assert.equal(result, 'Testing 789'); -}); - -QUnit.test('Testing event dispatcher', (assert) => { - - class Source { } - class Argument { } - - let dispatcher = new EventDispatcher(); - - var s1 = new Source(); - var s2 = new Source(); - var a1 = new Argument(); - var a2 = new Argument(); - - dispatcher.subscribe((sender: Source, argument: Argument) => { - assert.equal(sender === s1, true); - assert.equal(sender === s2 ,false); - - assert.equal(argument === a1, true); - assert.equal(argument === a2, false); - }); - - dispatcher.dispatch(s1, a1); -}); - - -QUnit.test('Testing simple event dispatcher', (assert) => { - - class Argument { } - - let dispatcher = new SimpleEventDispatcher< Argument>(); - - var a1 = new Argument(); - var a2 = new Argument(); - - dispatcher.subscribe((argument: Argument) => { - assert.equal(argument === a1, true); - assert.equal(argument === a2, false); - }); - - dispatcher.dispatch(a1); -}); - -QUnit.test('Testing event async dispatch', (assert) => { - - let dispatcher = new EventDispatcher(); - - let i = 0; - - dispatcher.subscribe((s, a) => { - i = a; - - assert.equal(i, 1); - }); - - dispatcher.dispatchAsync(null, 1); - - assert.equal(i, 0); -}); - -QUnit.test('Testing simple event async dispatch', (assert) => { - - let dispatcher = new SimpleEventDispatcher(); - - let i = 0; - - dispatcher.subscribe((a) => { - i = a; - - assert.equal(i, 1); - }); - - dispatcher.dispatchAsync(1); - - assert.equal(i, 0); +/// +/// +/// + +QUnit.test("Testing subscribe / unsubsribe - event as property", (assert) => { + + class MyEventTester { + private _myEvent: EventDispatcher = new EventDispatcher(); + + get myEvent(): IEvent { + return this._myEvent.asEvent(); + } + + signal(str: string): void { + this._myEvent.dispatch(this, str); + } + } + + var s = new MyEventTester(); + var r = null; + + var handler = (sender: MyEventTester, args: string) => { + r = args; + }; + + s.myEvent.subscribe(handler); + s.signal('Test1'); + assert.equal(r, 'Test1'); + + s.myEvent.unsubscribe(handler); + s.signal('Test2'); + assert.equal(r, 'Test1'); +}); + +QUnit.test("Testing subscribe / unsubsribe - event on interface" , (assert) => { + + interface IMyEventTester { + myEvent(): IEvent; + + signal(str: string); + } + + class MyEventTester implements IMyEventTester { + private _myEvent: EventDispatcher = new EventDispatcher(); + + myEvent(): IEvent { + return this._myEvent.asEvent(); + } + + signal(str: string): void { + this._myEvent.dispatch(this, str); + } + } + + var s: IMyEventTester = new MyEventTester(); + var r = null; + + var handler = (sender: IMyEventTester, args: string) => { + r = args; + }; + + s.myEvent().subscribe(handler); + s.signal('Test1'); + assert.equal(r, 'Test1'); + + s.myEvent().unsubscribe(handler); + s.signal('Test2'); + assert.equal(r, 'Test1'); +}); + +QUnit.test("Testing event list", (assert) => { + + var events = new EventList(); + var result: string; + + events.get('Test1').subscribe((sender, args) => result = args); + events.get('Test1').dispatch(this, 'Testing 123'); + assert.equal(result, 'Testing 123'); + + events.get('Test2').dispatch(this, 'Testing 456'); + assert.equal(result, 'Testing 123'); + + events.get('Test2').subscribe((sender, args) => result = args); + events.get('Test2').dispatch(this, 'Testing 789'); + assert.equal(result, 'Testing 789'); + + events.get('Test3').asEvent().subscribe((sender, args) => result = args); + events.get('Test3').dispatch(this, 'Testing 42'); + assert.equal(result, 'Testing 42'); + +}); + +QUnit.test('Testing EventHandlingBase', (assert) => { + + class MyTester extends EventHandlingBase { + + signal(name: string, str: string): void { + this.events.get(name).dispatch(this, str); + } + } + + var t = new MyTester(); + var result: string; + + t.subscribe('Test1', (sender, args) => result = args); + t.signal('Test1', 'Testing 123'); + assert.equal(result, 'Testing 123'); + + t.signal('Test2', 'Testing 456'); + assert.equal(result, 'Testing 123'); + + t.subscribe('Test2', (sender, args) => result = args); + t.signal('Test2', 'Testing 789'); + assert.equal(result, 'Testing 789'); +}); + +QUnit.test("Testing subscribe / unsubsribe - simple event as property", (assert) => { + + class MyEventTester { + private _myEvent: SimpleEventDispatcher = new SimpleEventDispatcher(); + + get myEvent(): ISimpleEvent { + return this._myEvent; + } + + signal(str: string): void { + this._myEvent.dispatch(str); + } + } + + var s = new MyEventTester(); + var r = null; + + var handler = (args: string) => { + r = args; + }; + + s.myEvent.subscribe(handler); + s.signal('Test1'); + assert.equal(r, 'Test1'); + + s.myEvent.unsubscribe(handler); + s.signal('Test2'); + assert.equal(r, 'Test1'); +}); + +QUnit.test("Testing subscribe / unsubsribe - simple event on interface", (assert) => { + + interface IMyEventTester { + myEvent(): ISimpleEvent; + + signal(str: string); + } + + class MyEventTester implements IMyEventTester { + private _myEvent: SimpleEventDispatcher = new SimpleEventDispatcher(); + + myEvent(): ISimpleEvent { + return this._myEvent; + } + + signal(str: string): void { + this._myEvent.dispatch(str); + } + } + + var s: IMyEventTester = new MyEventTester(); + var r = null; + + var handler = (args: string) => { + r = args; + }; + + s.myEvent().subscribe(handler); + s.signal('Test1'); + assert.equal(r, 'Test1'); + + s.myEvent().unsubscribe(handler); + s.signal('Test2'); + assert.equal(r, 'Test1'); +}); + +QUnit.test("Testing simple event list", (assert) => { + + var events = new SimpleEventList(); + var result: string; + + events.get('Test1').subscribe((args) => result = args); + events.get('Test1').dispatch('Testing 123'); + assert.equal(result, 'Testing 123'); + + events.get('Test2').dispatch('Testing 456'); + assert.equal(result, 'Testing 123'); + + events.get('Test2').subscribe((args) => result = args); + events.get('Test2').dispatch('Testing 789'); + assert.equal(result, 'Testing 789'); + + events.get('Test3').asEvent().subscribe((args) => result = args); + events.get('Test3').dispatch('Testing 42'); + assert.equal(result, 'Testing 42'); + +}); + +QUnit.test('Testing SimpleEventHandlingBase', (assert) => { + + class MyTester extends SimpleEventHandlingBase { + + signal(name: string, str: string): void { + this.events.get(name).dispatch(str); + } + } + + var t = new MyTester(); + var result: string; + + t.subscribe('Test1', (args) => result = args); + t.signal('Test1', 'Testing 123'); + assert.equal(result, 'Testing 123'); + + t.signal('Test2', 'Testing 456'); + assert.equal(result, 'Testing 123'); + + t.subscribe('Test2', (args) => result = args); + t.signal('Test2', 'Testing 789'); + assert.equal(result, 'Testing 789'); +}); + +QUnit.test('Testing event dispatcher', (assert) => { + + class Source { } + class Argument { } + + let dispatcher = new EventDispatcher(); + + var s1 = new Source(); + var s2 = new Source(); + var a1 = new Argument(); + var a2 = new Argument(); + + dispatcher.subscribe((sender: Source, argument: Argument) => { + assert.equal(sender === s1, true); + assert.equal(sender === s2 ,false); + + assert.equal(argument === a1, true); + assert.equal(argument === a2, false); + }); + + dispatcher.dispatch(s1, a1); +}); + + +QUnit.test('Testing simple event dispatcher', (assert) => { + + class Argument { } + + let dispatcher = new SimpleEventDispatcher< Argument>(); + + var a1 = new Argument(); + var a2 = new Argument(); + + dispatcher.subscribe((argument: Argument) => { + assert.equal(argument === a1, true); + assert.equal(argument === a2, false); + }); + + dispatcher.dispatch(a1); +}); + +QUnit.test('Testing event async dispatch', (assert) => { + + let dispatcher = new EventDispatcher(); + + let i = 0; + + dispatcher.subscribe((s, a) => { + i = a; + + assert.equal(i, 1); + }); + + dispatcher.dispatchAsync(null, 1); + + assert.equal(i, 0); +}); + +QUnit.test('Testing simple event async dispatch', (assert) => { + + let dispatcher = new SimpleEventDispatcher(); + + let i = 0; + + dispatcher.subscribe((a) => { + i = a; + + assert.equal(i, 1); + }); + + dispatcher.dispatchAsync(1); + + assert.equal(i, 0); }); \ No newline at end of file diff --git a/typings/StronglyTypesEvents-0.2.0.d.ts b/typings/StronglyTypedEvents-0.2.0.d.ts similarity index 100% rename from typings/StronglyTypesEvents-0.2.0.d.ts rename to typings/StronglyTypedEvents-0.2.0.d.ts