schwern/test-more — Issue #155: Rework the event handlers to have individual methods per event
Author: schwern
Created: 2011-10-27 01:22:53+00:00
State: open
Labels: Events, Test-Builder2

Right now, EventWatchers have accept_event() and accept_result().

`accept_event()` has to look at the event type and do its own dispatching. This is a pain for event watcher authors. This was done for performance reasons, so the event coordinator doesn't have to do a `can` for each event and watcher to see if it has a method for the event.

Turns out the cost of that `can()` isn't much, so let's put it back in.

EventWatchers will either define `accept_$type()` to handle a specific event type, or leave it to `accept_event`.

The EventCoordinator will do something like:

my $method = "accept_".$event->event_type;
$method =~ s{\s}{_}g;
$watcher->can($method) ? $watcher->$method($event) : $watcher->accept_event($event);

Leaving `accept_event` as a fallback allows a watcher to see every event without having to write an AUTOLOAD.

This also eliminates the special case for results. `accept_result` is just another event handler and `post_result` is no longer necessary.

The translation from event_type to method name can be cached and does significantly improve dispatch performance.