Documentation

WildcardEmitter
in package
implements EmitterInterface Uses WildcardEmitterTrait

This class is an EventEmitter with support for wildcard event handlers.

What this means is that you can emit events like this:

emit('change:firstName')

and listen to this event like this:

on('change:*')

A few notes:

  • Wildcards only work at the end of an event name.
  • Currently you can only use 1 wildcard.
  • Using ":" as a separator is optional, but it's highly recommended to use some kind of separator.

The WilcardEmitter is a bit slower than the regular Emitter. If you code must be very high performance, it might be better to try to use the other emitter. For must usage the difference is negligible though.

Tags
copyright

Copyright (C) fruux GmbH (https://fruux.com/)

author

Evert Pot (http://evertpot.com/)

license

http://sabre.io/license/ Modified BSD License

Interfaces, Classes and Traits

EmitterInterface
Event Emitter Interface.

Table of Contents

$listenerIndex  : mixed
An index of listeners for a specific event name. This helps speeding up emitting events after all listeners have been set.
$listeners  : mixed
The list of listeners.
$wildcardListeners  : mixed
The list of "wildcard listeners".
emit()  : bool
Emits an event.
listeners()  : array<string|int, callable>
Returns the list of listeners for an event.
on()  : mixed
Subscribe to an event.
once()  : mixed
Subscribe to an event exactly once.
removeAllListeners()  : mixed
Removes all listeners.
removeListener()  : bool
Removes a specific listener from an event.

Properties

$listenerIndex

An index of listeners for a specific event name. This helps speeding up emitting events after all listeners have been set.

protected mixed $listenerIndex = []

If the list of listeners changes though, the index clears.

$wildcardListeners

The list of "wildcard listeners".

protected mixed $wildcardListeners = []

Methods

emit()

Emits an event.

public emit(string $eventName[, array<string|int, mixed> $arguments = [] ][, callable $continueCallBack = null ]) : bool

This method will return true if 0 or more listeners were successfully handled. false is returned if one of the events broke the event chain.

If the continueCallBack is specified, this callback will be called every time before the next event handler is called.

If the continueCallback returns false, event propagation stops. This allows you to use the eventEmitter as a means for listeners to implement functionality in your application, and break the event loop as soon as some condition is fulfilled.

Note that returning false from an event subscriber breaks propagation and returns false, but if the continue-callback stops propagation, this is still considered a 'successful' operation and returns true.

Lastly, if there are 5 event handlers for an event. The continueCallback will be called at most 4 times.

Parameters
$eventName : string
$arguments : array<string|int, mixed> = []
$continueCallBack : callable = null
Return values
bool

listeners()

Returns the list of listeners for an event.

public listeners(string $eventName) : array<string|int, callable>

The list is returned as an array, and the list of events are sorted by their priority.

Parameters
$eventName : string
Return values
array<string|int, callable>

on()

Subscribe to an event.

public on(string $eventName, callable $callBack[, int $priority = 100 ]) : mixed
Parameters
$eventName : string
$callBack : callable
$priority : int = 100
Return values
mixed

once()

Subscribe to an event exactly once.

public once(string $eventName, callable $callBack[, int $priority = 100 ]) : mixed
Parameters
$eventName : string
$callBack : callable
$priority : int = 100
Return values
mixed

removeAllListeners()

Removes all listeners.

public removeAllListeners([string $eventName = null ]) : mixed

If the eventName argument is specified, all listeners for that event are removed. If it is not specified, every listener for every event is removed.

Parameters
$eventName : string = null
Return values
mixed

removeListener()

Removes a specific listener from an event.

public removeListener(string $eventName, callable $listener) : bool

If the listener could not be found, this method will return false. If it was removed it will return true.

Parameters
$eventName : string
$listener : callable
Return values
bool

Search results