Domain events can be used inside MediaWiki core to remove the need for code that updates state to know about other code that needs to be notified about this update (this is just the standard listener pattern).
Knowledge of which part of the code needs to listen to which event should be located in the application's "wiring". This could be done as part of the application's service wiring - however, this approach is not attractive: it would mean that we have to register all listeners for all events in the service instantiator for DomainEventDispatcher.
It seems better to establish a separate wiring system for events, as follows:
- each event wiring file returns an associative array that maps even types to wiring callbacks
- a wiring callback associated with an event type returns a list of listeners that is to be registered for that event type
- DomainEventDispatcher maintains a wiring map, associating event types with lists of callbacks.
- DomainEventDispatcher gets a loadWiringFile() method which merges the map returned by the wiring file with any previously loaded wiring.
- When the first event of a gioven type is triggered, any wiring callbacks registered for that event type will be invoked, and all listeners returned by any of the callbacks are registered for the event type before the event is triggered. The wiring for that event type is then reset.
This way, listener instantiation is deferred until the listeners are first needed. Also, all listeners for a given type of event can be defined in a single place.