Typed events instead of plain Hash?

I am jumping with joy into the dry ecosystem.

I am exploring with dry-events, and saw the payload seems to be “just” a Hash.

Is there any idiomatic way to make it a Dry::Struct for example, or at least validate the hash when publishing an event?

I’m about to write a small wrapper to do that, but maybe it has already been done/proofed before?

1 Like

There is this sentence in the docs, but I can’t make sense of it:

Publishers are created using Dry::Events::Publisher extension, which is created by providing a unique identifier. An extended class can be used to define the type of events it can publish

YAY! Welcome :slight_smile:

There’s no public API to do that but it is possible. The reason why it’s a plain Hash is because performance is a great concern here. Creating plain hashes will be multiple times faster than Structs. We could consider adding an API that would allow you to specify payload constructors (as in, something that would build a payload however you want), but it’s a very, very low priority right now. This is a post-1.0.0 type of a feature.

I hope this will make it more clear:

  • include Dry::Events::Publisher[:my_publisher] returns a module object which is identified by :my_publisher symbol, then it gets included in the Application class and provides register_event class method
  • register_event defines an event type that you will be able to use with this publisher class