Skip to content

Remove unneeded isTimerActive() to reduce API surface #133

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 1 addition & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ For the code of the current stable 0.4.x release, checkout the
* [addTimer()](#addtimer)
* [addPeriodicTimer()](#addperiodictimer)
* [cancelTimer()](#canceltimer)
* [isTimerActive()](#istimeractive)
* [futureTick()](#futuretick)
* [addSignal()](#addsignal)
* [removeSignal()](#removesignal)
Expand Down Expand Up @@ -357,23 +356,8 @@ cancel a pending timer.

See also [`addPeriodicTimer()`](#addperiodictimer) and [example #2](examples).

You can use the [`isTimerActive()`](#istimeractive) method to check if
this timer is still "active". After a timer is successfully cancelled,
it is no longer considered "active".

Calling this method on a timer instance that has not been added to this
loop instance or on a timer that is not "active" (or has already been
cancelled) has no effect.

#### isTimerActive()

The `isTimerActive(TimerInterface $timer): bool` method can be used to
check if a given timer is active.

A timer is considered "active" if it has been added to this loop instance
via [`addTimer()`](#addtimer) or [`addPeriodicTimer()`](#addperiodictimer)
and has not been cancelled via [`cancelTimer()`](#canceltimer) and is not
a non-periodic timer that has already been triggered after its interval.
loop instance or on a timer that has already been cancelled has no effect.

#### futureTick()

Expand Down
9 changes: 2 additions & 7 deletions src/ExtEventLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,17 +144,12 @@ public function addPeriodicTimer($interval, callable $callback)

public function cancelTimer(TimerInterface $timer)
{
if ($this->isTimerActive($timer)) {
if ($this->timerEvents->contains($timer)) {
$this->timerEvents[$timer]->free();
$this->timerEvents->detach($timer);
}
}

public function isTimerActive(TimerInterface $timer)
{
return $this->timerEvents->contains($timer);
}

public function futureTick(callable $listener)
{
$this->futureTickQueue->add($listener);
Expand Down Expand Up @@ -282,7 +277,7 @@ private function createTimerCallback()
$this->timerCallback = function ($_, $__, $timer) {
call_user_func($timer->getCallback(), $timer);

if (!$timer->isPeriodic() && $this->isTimerActive($timer)) {
if (!$timer->isPeriodic() && $this->timerEvents->contains($timer)) {
$this->cancelTimer($timer);
}
};
Expand Down
7 changes: 1 addition & 6 deletions src/ExtLibevLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public function addTimer($interval, callable $callback)
$callback = function () use ($timer) {
call_user_func($timer->getCallback(), $timer);

if ($this->isTimerActive($timer)) {
if ($this->timerEvents->contains($timer)) {
$this->cancelTimer($timer);
}
};
Expand Down Expand Up @@ -157,11 +157,6 @@ public function cancelTimer(TimerInterface $timer)
}
}

public function isTimerActive(TimerInterface $timer)
{
return $this->timerEvents->contains($timer);
}

public function futureTick(callable $listener)
{
$this->futureTickQueue->add($listener);
Expand Down
9 changes: 2 additions & 7 deletions src/ExtLibeventLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public function addPeriodicTimer($interval, callable $callback)

public function cancelTimer(TimerInterface $timer)
{
if ($this->isTimerActive($timer)) {
if ($this->timerEvents->contains($timer)) {
$event = $this->timerEvents[$timer];

event_del($event);
Expand All @@ -173,11 +173,6 @@ public function cancelTimer(TimerInterface $timer)
}
}

public function isTimerActive(TimerInterface $timer)
{
return $this->timerEvents->contains($timer);
}

public function futureTick(callable $listener)
{
$this->futureTickQueue->add($listener);
Expand Down Expand Up @@ -298,7 +293,7 @@ private function createTimerCallback()
call_user_func($timer->getCallback(), $timer);

// Timer already cancelled ...
if (!$this->isTimerActive($timer)) {
if (!$this->timerEvents->contains($timer)) {
return;

// Reschedule periodic timers ...
Expand Down
21 changes: 1 addition & 20 deletions src/LoopInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,34 +255,15 @@ public function addPeriodicTimer($interval, callable $callback);
*
* See also [`addPeriodicTimer()`](#addperiodictimer) and [example #2](examples).
*
* You can use the [`isTimerActive()`](#istimeractive) method to check if
* this timer is still "active". After a timer is successfully cancelled,
* it is no longer considered "active".
*
* Calling this method on a timer instance that has not been added to this
* loop instance or on a timer that is not "active" (or has already been
* cancelled) has no effect.
* loop instance or on a timer that has already been cancelled has no effect.
*
* @param TimerInterface $timer The timer to cancel.
*
* @return void
*/
public function cancelTimer(TimerInterface $timer);

/**
* Check if a given timer is active.
*
* A timer is considered "active" if it has been added to this loop instance
* via [`addTimer()`](#addtimer) or [`addPeriodicTimer()`](#addperiodictimer)
* and has not been cancelled via [`cancelTimer()`](#canceltimer) and is not
* a non-periodic timer that has already been triggered after its interval.
*
* @param TimerInterface $timer The timer to check.
*
* @return boolean True if the timer is still enqueued for execution.
*/
public function isTimerActive(TimerInterface $timer);

/**
* Schedule a callback to be invoked on a future tick of the event loop.
*
Expand Down
5 changes: 0 additions & 5 deletions src/StreamSelectLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,6 @@ public function cancelTimer(TimerInterface $timer)
$this->timers->cancel($timer);
}

public function isTimerActive(TimerInterface $timer)
{
return $this->timers->contains($timer);
}

public function futureTick(callable $listener)
{
$this->futureTickQueue->add($listener);
Expand Down
13 changes: 0 additions & 13 deletions tests/Timer/AbstractTimerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,6 @@ public function testAddPeriodicTimerCancelsItself()
$this->assertSame(2, $i);
}

public function testIsTimerActive()
{
$loop = $this->createLoop();

$timer = $loop->addPeriodicTimer(0.001, function () {});

$this->assertTrue($loop->isTimerActive($timer));

$loop->cancelTimer($timer);

$this->assertFalse($loop->isTimerActive($timer));
}

public function testMinimumIntervalOneMicrosecond()
{
$loop = $this->createLoop();
Expand Down