Skip to content

Commit

Permalink
feat(types): allow to define event map
Browse files Browse the repository at this point in the history
  • Loading branch information
nikku committed Feb 29, 2024
1 parent c8d3fb7 commit 7cfc91d
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 3 deletions.
13 changes: 11 additions & 2 deletions lib/Diagram.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<EventMap>
};

const typedDiagram = new Diagram<ServiceMap>();

const eventBus = typedDiagram.get('eventBus');

eventBus.on('a', (event: any) => console.log('a'));
eventBus.on('diagram.init', (event) => {

// go forth and react to init (!)
});
36 changes: 35 additions & 1 deletion lib/core/EventBus.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {

Expand All @@ -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`
Expand All @@ -151,6 +154,25 @@ export default function EventBus() {
* @param {EventBusEventCallback<T>} 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<EventMap[EventName]>} callback
* @param {any} [that] callback context
*/
EventBus.prototype.on = function(events, priority, callback, that) {

events = isArray(events) ? events : [ events ];
Expand Down Expand Up @@ -188,6 +210,8 @@ EventBus.prototype.on = function(events, priority, callback, that) {
};

/**
* @overlord
*
* Register an event listener that is called only once.
*
* @template T
Expand All @@ -197,6 +221,16 @@ EventBus.prototype.on = function(events, priority, callback, that) {
* @param {EventBusEventCallback<T>} 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<EventMap[EventName]>} callback
* @param {any} [that] callback context
*/
EventBus.prototype.once = function(events, priority, callback, that) {
var self = this;

Expand Down
19 changes: 19 additions & 0 deletions lib/core/EventBus.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<EventMap>();

typedEventBus.on('foo', (event: FooEvent) => {
const { foo } = event;

console.log(foo);
});

typedEventBus.on('foo', (event) => {
const { foo } = event;

console.log(foo);
});

0 comments on commit 7cfc91d

Please sign in to comment.