broadcast method
Broadcast this Event to subscribers, with an optional EventArgs derived argument.
Ignored if no handlers (subscribers). Calls each handler (callback) that was previously added to this Event.
Returns true if there are associated subscribers, or else false if there are no subscribers and the broadcast has no effect.
// Example
// Without an <EventArgs> argument
var e = Event();
e.broadcast();
// Note: above is equivalent to...
var e = Event<EventArgs>();
e.broadcast(EventArgs());
// With an <EventArgs> argument
var e = Event<ChangedValue>();
e.broadcast(ChangedValue(3.14159));
If the broadcast argument does not match the
Event generic type, then an ArgsError
will be thrown.
Implementation
bool broadcast([args]) {
if (_handlers.isEmpty) return false;
// if no EventArgs or derived specified, then create one
args ??= EventArgs();
args.eventName = this.eventName;
args.whenOccurred = DateTime.now().toUtc();
try {
for (var i = 0; i < _handlers.length; i++) {
log('Broadcast Event "$this"', source: "Event", level: Severity.debug);
_handlers[i].call(args);
}
} on TypeError {
throw ArgsError("Incorrect args being broadcast - args should be a $genericType");
}
return true;
}