Skip to content

Commit 5595a42

Browse files
committed
Add postpone parameter
1 parent 73f1574 commit 5595a42

3 files changed

Lines changed: 80 additions & 4 deletions

File tree

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,12 @@ abstract class Loop
8080
*
8181
* If resume is called multiple times, and the event loop hasn't resumed the loop yet,
8282
* the loop will be resumed only once, not N times for every call.
83-
*
83+
*
84+
* @param bool $postpone If true, multiple resumes will postpone the resuming to the end of the callback queue instead of leaving its position unchanged.
85+
*
8486
* @return bool Returns false if the loop is not paused.
8587
*/
86-
public function resume(): bool;
88+
public function resume(bool $postpone = false): bool;
8789
/**
8890
* Stops loop.
8991
*

lib/Loop.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,11 +222,21 @@ public function isPaused(): bool
222222
* If resume is called multiple times, and the event loop hasn't resumed the loop yet,
223223
* the loop will be resumed only once, not N times for every call.
224224
*
225+
* @param bool $postpone If true, multiple resumes will postpone the resuming to the end of the callback queue instead of leaving its position unchanged.
226+
*
225227
* @return bool Returns false if the loop is not paused.
226228
*/
227-
public function resume(): bool
229+
public function resume(bool $postpone = false): bool
228230
{
229-
if (!$this->resumeImmediate && $this->running && $this->paused) {
231+
if ($this->running && $this->paused) {
232+
if ($this->resumeImmediate) {
233+
if (!$postpone) {
234+
return true;
235+
}
236+
$resumeImmediate = $this->resumeImmediate;
237+
$this->resumeImmediate = null;
238+
EventLoop::cancel($resumeImmediate);
239+
}
230240
if ($this->resumeTimer) {
231241
$timer = $this->resumeTimer;
232242
$this->resumeTimer = null;

test/GenericTest.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use danog\Loop\Test\Interfaces\LoggingPauseInterface;
1515
use danog\Loop\Test\Traits\Basic;
1616
use danog\Loop\Test\Traits\LoggingPause;
17+
use Revolt\EventLoop;
1718

1819
use function Amp\delay;
1920

@@ -275,6 +276,69 @@ private function fixtureAssertions(callable $closure, int &$runCount, ?float &$p
275276
$this->assertFalse($loop->isRunning());
276277
$this->assertFalse($loop->stop());
277278
$this->assertFalse($loop->resume());
279+
280+
// Restart loop, without postponing resuming
281+
$pauseTime = GenericLoop::PAUSE;
282+
$this->assertTrue($loop->start());
283+
self::waitTick();
284+
$this->fixtureStarted($loop, 5);
285+
$expectedRunCount++;
286+
287+
$this->assertEquals($expectedRunCount, $runCount);
288+
$this->assertEquals(6, $loop->getPauseCount());
289+
$this->assertEquals(0.0, $loop->getLastPause());
290+
$this->assertTrue($loop->isPaused());
291+
292+
$pauseTime = GenericLoop::STOP;
293+
$this->assertTrue($loop->resume(false));
294+
$this->assertTrue($loop->resume(false));
295+
$expectedRunCount++;
296+
self::waitTick();
297+
$this->assertEquals($expectedRunCount, $runCount);
298+
$this->assertEquals(6, $loop->getPauseCount());
299+
$this->assertEquals(0.0, $loop->getLastPause());
300+
$this->assertTrue($loop->isPaused());
301+
302+
$this->assertEquals(5, $loop->startCounter());
303+
$this->assertEquals(5, $loop->endCounter());
304+
305+
$this->assertFalse($loop->isRunning());
306+
$this->assertFalse($loop->stop());
307+
$this->assertFalse($loop->resume());
308+
309+
// Restart loop, postponing resuming
310+
$pauseTime = GenericLoop::PAUSE;
311+
$this->assertTrue($loop->start());
312+
self::waitTick();
313+
$this->fixtureStarted($loop, 6);
314+
$expectedRunCount++;
315+
316+
$this->assertEquals($expectedRunCount, $runCount);
317+
$this->assertEquals(7, $loop->getPauseCount());
318+
$this->assertEquals(0.0, $loop->getLastPause());
319+
$this->assertTrue($loop->isPaused());
320+
321+
$pauseTime = GenericLoop::STOP;
322+
$this->assertTrue($loop->resume(true));
323+
EventLoop::queue(fn () => $this->assertTrue($loop->resume(true)));
324+
self::waitTick();
325+
$this->assertEquals($expectedRunCount, $runCount);
326+
$this->assertEquals(7, $loop->getPauseCount());
327+
$this->assertEquals(0.0, $loop->getLastPause());
328+
$this->assertTrue($loop->isPaused());
329+
330+
$this->assertEquals(6, $loop->startCounter());
331+
$this->assertEquals(5, $loop->endCounter());
332+
333+
self::waitTick();
334+
$expectedRunCount++;
335+
$this->assertEquals($expectedRunCount, $runCount);
336+
$this->assertEquals(7, $loop->getPauseCount());
337+
$this->assertEquals(0.0, $loop->getLastPause());
338+
$this->assertTrue($loop->isPaused());
339+
340+
$this->assertEquals(6, $loop->startCounter());
341+
$this->assertEquals(6, $loop->endCounter());
278342
}
279343

280344
/**

0 commit comments

Comments
 (0)