Event queues with ReactiveX

An event queue would simply ensure that things that are to be done in a synchronous manner or in a first-in first-out manner. Let's define first what a queue is.

A queue is basically a list of things to do, which will get executed one by one until all the things in the queue have been finished.

In Laravel, for example, there is already a concept of queues, where we go through the elements of the queue. You can find the documentation at https://laravel.com/docs/5.0/queues .

Queues are usually used in systems that need to do some tasks in order and not in an asynchronous function. In PHP, there is already the SplQueue class, which provides the main functionalities of a queue implemented using a doubly linked list.

In general, queues are executed in the order that they come in. In ReactiveX, things are more of an asynchronous nature. In this scenario, we will implement a priority queue, where each task has corresponding levels of priority.

This is what a simple PriorityQueue code in ReactiveX would look like:

use RxSchedulerPriorityQueue; 
 
Var $firstItem = new ScheduledItem(null, null, null, 1, null); 
 
var $secondtItem = new ScheduledItem(null, null, null, 2, null); 
$queue          = new PriorityQueue(); 
$queue->enqueue($firstItem); 
$queue->enqueue($secondItem); 
//remove firstItem if not needed in queue 
$queue->remove($firstItem); 

In the preceding code, we used the PriorityQueue library of RxPHP. We set some schedulers and queued them in a PriorityQueue. We gave each scheduled item a priority or time to spend in execution with 1 and 2. In the preceding scenario, the first item will execute first because it is the first priority and has the shortest time (1). Finally, we remove the ScheduledItem just to show what's possible with PriorityQueue in the RxPHP library.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.176.99