Skip to content

Commit

Permalink
Remove work from construct method
Browse files Browse the repository at this point in the history
  • Loading branch information
quentin.schmick committed Aug 30, 2022
1 parent 7a91e9b commit 792dd3a
Showing 1 changed file with 17 additions and 18 deletions.
35 changes: 17 additions & 18 deletions src/Jobs/DebouncedJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,39 +22,38 @@ class DebouncedJob implements ShouldQueue
protected string $_cacheKey;
protected bool $_debounced = false;
private static int $MICROSECONDS_SLEEP = 100000;
public ShouldQueue $_jobToDebounce;
public int $_minimumMillisecondsToWait = 0;
public int|null $_maximumMillisecondsToWait = null;

public static function dispatchAndDebounce(
ShouldQueue $jobToDebounce,
int $minimumMillisecondsToWait,
int|null $maximumMillisecondsToWait = null,
) : PendingClosureDispatch|PendingDispatch {
return dispatch(new self(
$jobToDebounce,
$minimumMillisecondsToWait,
$maximumMillisecondsToWait
));
}
$debouncer = new self();

$debouncer->_jobToDebounce = $jobToDebounce;
$debouncer->_minimumMillisecondsToWait = $minimumMillisecondsToWait;
$debouncer->_maximumMillisecondsToWait = $maximumMillisecondsToWait;
$debouncer->calculateCacheKey();

public function __construct(
public ShouldQueue $_jobToDebounce,
public int $_minimumMillisecondsToWait,
public int|null $_maximumMillisecondsToWait = null,
) {
$this->calculateCacheKey();
$lock = tap(Cache::lock($this->getCacheKey(), 5))->block(3);
$lock = tap(Cache::lock($debouncer->getCacheKey(), 5))->block(3);

try {
if ($this->debounceExists()) {
$this->_debounced = true;
if ($debouncer->debounceExists()) {
$debouncer->_debounced = true;
} else {
$this->setDebounce();
$this->persistMaximumWaitTime();
$debouncer->setDebounce();
$debouncer->persistMaximumWaitTime();
}

$this->persistMinimumWaitTime();
$debouncer->persistMinimumWaitTime();
} finally {
$lock->release();
}

return dispatch($debouncer);
}

public function handle()
Expand Down

0 comments on commit 792dd3a

Please sign in to comment.