Adding types to the messages published with event aggregator (TypeScript)

Hi all!

Has anyone here experimented with adding types to the event aggregator events?

I was thinking of making the pub/sub system more transparent by declaring the different messages and the data that they can/must include with the messages, but I dont know if this would even end up being beneficial in the long run or just unnecessary overhead.

One idea I had was using enums: Message.SessionCreated, Message.SessionExpired etc, but then I was thinking of how to also declare the data that might accompany these messages…

Any thoughts/examples? :slightly_smiling_face:

1 Like

Like this?

ea.publish(new SessionCreatedEvent())

1 Like

Pretty much like your example, yeah! I was thinking that the channel could be perhaps a static enum value of a class while the object of the class contains the data we might want to

Something like
ea.publish(SessionCreatedEvent.Channel, new SessionCreatedEvent(params))

But then it got me thinking that this all depends on the style of pub/sub that you implement, as its possible to do it atleast 2 different ways:

  1. Generic channels, where the additional data has a format and the subscribers filter out the messages based on the additional data

  2. Specific channels, where the subscribers know that when something is published it is meant for them

With the ea supporting subscribeOnce, it seemed to me that 2. is being used more.

(For now, I dont think I am gonna create a complicated system when I only need to subscribe to a few events.)

You don’t need the enum. If you pass an object, the constructor will be used as the key. Same result.

1 Like

Thanks, didn’t know that. This makes it really easy :slight_smile:

Created a proof of concept: