PHP code example of reyesoft / reactive-laravel-jobs

1. Go to this page and download the library: Download reyesoft/reactive-laravel-jobs library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

reyesoft / reactive-laravel-jobs example snippets


use Reyesoft\ReactiveLaravelJobs\Debounce\Debounceable;
use Reyesoft\ReactiveLaravelJobs\Debounce\ShouldDebounce;

final class DebouncedNotificationJob implements ShouldDebounce
{
    use Debounceable;
    use Dispatchable;
    use Queueable;

    public $param1 = '';

    public function __construct($param1)
    {
        $this->param1 = $param1;
    }

    public function uniqueId()
    {
        return $this->param1;
    }

    public function debouncedHandle(): void
    {
        echo PHP_EOL . $this->param1;
    }
}

DebouncedNotificationJob::dispatchDebounced('You have 1 item.')->delay(5);
DebouncedNotificationJob::dispatchDebounced('You have 2 items.')->delay(5);
DebouncedNotificationJob::dispatchDebounced('You have 3 items.')->delay(5);
sleep(10);
DebouncedNotificationJob::dispatchDebounced('You have 4 items.')->delay(5);
sleep(1);
DebouncedNotificationJob::dispatchDebounced('You have 5 items.')->delay(5);

// Do
You have 3 items.
You have 5 items.

use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Contracts\Queue\ShouldBeUnique;
 
class ThrottledNotificationJob implements ShouldQueue, ShouldBeUnique
{
    public $param1 = '';

    public function __construct($param1)
    {
        $this->param1 = $param1;
    }

    /**
     * The number of seconds after which the job's unique lock
     * will be released.
     */
    public $uniqueFor = 3600;
 
    public function uniqueId()
    {
        return $this->param1;
    }

    public function handle(): void
    {
        echo PHP_EOL . $this->param1;
    }
}

ThrottledNotificationJob::dispatch('You have 1 item.')->delay(5);
ThrottledNotificationJob::dispatch('You have 2 items.')->delay(5);
ThrottledNotificationJob::dispatch('You have 3 items.')->delay(5);
sleep(10);
ThrottledNotificationJob::dispatch('You have 4 items.')->delay(5);
sleep(1);
ThrottledNotificationJob::dispatch('You have 5 items.')->delay(5);

// Do
You have 1 item.
You have 4 items.