diff --git a/lib/Diagram.spec.ts b/lib/Diagram.spec.ts index c80a138cb..099014d4f 100644 --- a/lib/Diagram.spec.ts +++ b/lib/Diagram.spec.ts @@ -35,12 +35,21 @@ const foo = diagram.invoke((modeling: Modeling, eventBus: EventBus) => { foo.bar = false; +type NoneEvent = {}; + +type EventMap = { + 'diagram.init': NoneEvent +}; + type ServiceMap = { - 'eventBus': EventBus + 'eventBus': EventBus }; const typedDiagram = new Diagram(); const eventBus = typedDiagram.get('eventBus'); -eventBus.on('a', (event: any) => console.log('a')); +eventBus.on('diagram.init', (event) => { + + // go forth and react to init (!) +}); diff --git a/lib/core/EventBus.js b/lib/core/EventBus.js index 9b764759a..f949f0eb1 100644 --- a/lib/core/EventBus.js +++ b/lib/core/EventBus.js @@ -118,6 +118,8 @@ var slice = Array.prototype.slice; * var sum = eventBus.fire('sum', 1, 2); * console.log(sum); // 3 * ``` + * + * @template [EventMap=null] */ export default function EventBus() { @@ -131,8 +133,9 @@ export default function EventBus() { this.on('diagram.destroy', 1, this._destroy, this); } - /** + * @overlord + * * Register an event listener for events with the given name. * * The callback will be invoked with `event, ...additionalArguments` @@ -151,6 +154,25 @@ export default function EventBus() { * @param {EventBusEventCallback} callback * @param {any} [that] callback context */ +/** + * Register an event listener for events with the given name. + * + * The callback will be invoked with `event, ...additionalArguments` + * that have been passed to {@link EventBus#fire}. + * + * Returning false from a listener will prevent the events default action + * (if any is specified). To stop an event from being processed further in + * other listeners execute {@link Event#stopPropagation}. + * + * Returning anything but `undefined` from a listener will stop the listener propagation. + * + * @template {keyof EventMap} EventName + * + * @param {EventName} events to subscribe to + * @param {number} [priority=1000] listen priority + * @param {EventBusEventCallback} callback + * @param {any} [that] callback context + */ EventBus.prototype.on = function(events, priority, callback, that) { events = isArray(events) ? events : [ events ]; @@ -188,6 +210,8 @@ EventBus.prototype.on = function(events, priority, callback, that) { }; /** + * @overlord + * * Register an event listener that is called only once. * * @template T @@ -197,6 +221,16 @@ EventBus.prototype.on = function(events, priority, callback, that) { * @param {EventBusEventCallback} callback * @param {any} [that] callback context */ +/** + * Register an event listener that is called only once. + * + * @template {keyof EventMap} EventName + * + * @param {EventName} events to subscribe to + * @param {number} [priority=1000] listen priority + * @param {EventBusEventCallback} callback + * @param {any} [that] callback context + */ EventBus.prototype.once = function(events, priority, callback, that) { var self = this; diff --git a/lib/core/EventBus.spec.ts b/lib/core/EventBus.spec.ts index a2415d1f1..14b13d99d 100644 --- a/lib/core/EventBus.spec.ts +++ b/lib/core/EventBus.spec.ts @@ -59,3 +59,22 @@ eventBus.once('foo', callback, this); eventBus.once('foo', (event, additional1, additional2) => { console.log('foo', event, additional1, additional2); }); + + +type EventMap = { + foo: FooEvent +}; + +const typedEventBus = new EventBus(); + +typedEventBus.on('foo', (event: FooEvent) => { + const { foo } = event; + + console.log(foo); +}); + +typedEventBus.on('foo', (event) => { + const { foo } = event; + + console.log(foo); +}); \ No newline at end of file