An event queue lets you fire events with multiple payloads at a delayed time. Using queue's is a great way to organize events. Where events are only fired with one payload, queue's can hold multiple payloads and listeners, and run through them in one go.
$queue = new Fuel\Event\Queue();
$queue->queue('name', array('param 1 - a', 'param 2 - a'));
// Add another one.
$queue->queue('name', array('param 1 - b', 'param 2 - b'));
You've now got 2 payloads in the queue, ready to get cracking.
Where in events callbacks are often referred to as Listeners, in Queue they're commonly known as Flushers or Workers. Registering a Flusher works just like adding a Listener to a Container:
$queue->on('name', $callable_flusher, $context, $priority);
As you can see in the code above, the queue on
method can also take a $priority
param. Like in events emitted from a Container, this will sort the Flushers descending.
$queue->queue('name', array('Hello', 'World'));
$queue->on('name', function($event, $one, $two){
echo $two.'!';
}, 1);
$queue->on('name', function($event, $one, $two){
echo $one.' ';
}, 2);
When flushed this will echo out Hello World!
.
In order to run the queued payloads by the Flushers you'll have to flush()
.
$result = $queue->flush('name');
When only using one Flusher, you can also define it in the flush
call.
$result = $queue->flush('name', function(){
// Flusher logic.
}, $optional_context);
The result of the flushing is a multidimentional array:
result_array(
payload_one_array(
flusher_one_payload_one_result(),
flusher_two_payload_one_result(),
),
payload_two_array(
flusher_one_payload_two_result(),
flusher_two_payload_two_result(),
)
)