Skip to content

Commit

Permalink
Mark repeat watcher as disabled then re-enable when executed (#325)
Browse files Browse the repository at this point in the history
Fixes #324.
  • Loading branch information
trowski committed Jul 13, 2020
1 parent a04eda3 commit 0b802a5
Showing 1 changed file with 21 additions and 31 deletions.
52 changes: 21 additions & 31 deletions lib/Loop/NativeDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,43 +104,33 @@ protected function dispatch(bool $blocking)
$blocking ? $this->getTimeout() : 0
);

$scheduleQueue = [];
$now = $this->now();

try {
$now = $this->now();

while ($watcher = $this->timerQueue->extract($now)) {
if ($watcher->type & Watcher::REPEAT) {
$expiration = $now + $watcher->value;
$scheduleQueue[] = [$watcher, $expiration];
} else {
$this->cancel($watcher->id);
}

try {
// Execute the timer.
$result = ($watcher->callback)($watcher->id, $watcher->data);
while ($watcher = $this->timerQueue->extract($now)) {
if ($watcher->type & Watcher::REPEAT) {
$watcher->enabled = false; // Trick base class into adding to enable queue when calling enable()
$this->enable($watcher->id);
} else {
$this->cancel($watcher->id);
}

if ($result === null) {
continue;
}
try {
// Execute the timer.
$result = ($watcher->callback)($watcher->id, $watcher->data);

if ($result instanceof \Generator) {
$result = new Coroutine($result);
}
if ($result === null) {
continue;
}

if ($result instanceof Promise || $result instanceof ReactPromise) {
rethrow($result);
}
} catch (\Throwable $exception) {
$this->error($exception);
if ($result instanceof \Generator) {
$result = new Coroutine($result);
}
}
} finally {
foreach ($scheduleQueue as list($watcher, $expiration)) {
if ($watcher->enabled) {
$this->timerQueue->insert($watcher, $expiration);

if ($result instanceof Promise || $result instanceof ReactPromise) {
rethrow($result);
}
} catch (\Throwable $exception) {
$this->error($exception);
}
}

Expand Down

0 comments on commit 0b802a5

Please sign in to comment.