Just wondering how I can retrieve the data passed into an event (for example the randomNameAsTestData from cfg.OnEvent("eventName", "randomNameAsTestData").
I’m listening to events like this:
caddy.RegisterEventHook("eventName", HookFunc);
Where the HookFun looks something like this
func HookFunc(event caddy.EventName, info interface{}) error {
}
But how can I get the data passed? The info interface has keys like onshutdown (or similar) so I have no idea what to do…
Your func needs to be an EventHook, which means you receive an EventName (which is a string) and an interface which can literally be anything.
What you want to do is search the code for event emitters (look for EmitEvent calls) and find out what it’s putting in the interface.
To demonstrate, if you’re looking for a ShutdownEvent (see: caddy package - github.com/mholt/caddy - pkg.go.dev) you can check whether the EventName is equal to caddy.ShutdownEvent before proceeding. Then you can type assert the info interface to a string, which - going by the usage of EmitEvent here - should be the name of the signal we’re shutting down for.
So as I said, if I emitted an event (it’s custom, by the way) like cfg.OnEvent("eventName", "randomNameAsTestData") how would I get the data ("randomTestData") when listening to the event?