Skip to content

Commit

Permalink
Merge pull request #8 from tioncico/patch-1
Browse files Browse the repository at this point in the history
修复当定时器name为数字时无法clear的bug
  • Loading branch information
kiss291323003 authored Feb 24, 2019
2 parents 3e4897a + 2e7135b commit ad3deff
Showing 1 changed file with 28 additions and 27 deletions.
55 changes: 28 additions & 27 deletions src/Timer.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,52 +16,53 @@ class Timer
protected $timerList = [];
protected $timerMap = [];

function loop(int $ms,callable $callback,$name = null):int
function loop(int $ms, callable $callback, $name = null): int
{
$id = swoole_timer_tick($ms, $callback);
$id = swoole_timer_tick($ms, $callback);
$this->timerList[$id] = $id;
if($name !== null){
if ($name !== null) {
$this->timerMap[md5($name)] = $id;
}
return $id;
}

function clear($timerIdOrName):bool
function clear($timerIdOrName): bool
{
if(is_numeric($timerIdOrName)){
if(!isset($this->timerList[$timerIdOrName])){
return false;
}
swoole_timer_clear($timerIdOrName);
$key = array_search($timerIdOrName,$this->timerMap);
if($key !== null){
unset($this->timerMap[$key]);
}
return true;
}else{
$timerIdOrName = md5($timerIdOrName);
if(!isset($this->timerMap[$timerIdOrName])){
return false;
if (!isset($this->timerMap[md5($timerIdOrName)]) && !isset($this->timerList[$timerIdOrName])) {
return false;
}
if (is_numeric($timerIdOrName)) {
if (isset($this->timerList[$timerIdOrName])) {
swoole_timer_clear($timerIdOrName);
$key = array_search($timerIdOrName, $this->timerMap);
if ($key !== null) {
unset($this->timerMap[$key]);
}
return true;
}
$id = $this->timerMap[$timerIdOrName];
swoole_timer_clear($id);
unset($this->timerList[$id]);
unset($this->timerMap[$timerIdOrName]);
return true;
}
$timerIdOrName = md5($timerIdOrName);
if (!isset($this->timerMap[$timerIdOrName])) {
return false;
}
$id = $this->timerMap[$timerIdOrName];
swoole_timer_clear($id);
unset($this->timerList[$id]);
unset($this->timerMap[$timerIdOrName]);
return true;
}

function clearAll():bool
function clearAll(): bool
{
foreach ($this->timerList as $id){
foreach ($this->timerList as $id) {
swoole_timer_clear($id);
}
$this->timerList = [];
return true;
}

function after(int $ms,callable $callback):int
function after(int $ms, callable $callback): int
{
return swoole_timer_after($ms, $callback);
}
}
}

0 comments on commit ad3deff

Please sign in to comment.