Skip to content

Commit 431f6f5

Browse files
committed
[EventLoop] Add a timer interface and class.
They will be used to provide a much nicer abstraction for timers shared among the various event loop implementations.
1 parent 134b94d commit 431f6f5

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

Timer/Timer.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
namespace React\EventLoop\Timer;
4+
5+
use InvalidArgumentException;
6+
use React\EventLoop\LoopInterface;
7+
8+
class Timer implements TimerInterface
9+
{
10+
protected $loop;
11+
protected $interval;
12+
protected $callback;
13+
protected $periodic;
14+
protected $data;
15+
16+
public function __construct(LoopInterface $loop, $interval, $callback, $periodic = false, $data = null)
17+
{
18+
if (false === is_callable($callback)) {
19+
throw new InvalidArgumentException('The callback argument must be a valid callable object');
20+
}
21+
22+
$this->loop = $loop;
23+
$this->interval = (float) $interval;
24+
$this->callback = $callback;
25+
$this->periodic = (bool) $periodic;
26+
$this->data = null;
27+
}
28+
29+
public function getLoop()
30+
{
31+
return $this->loop;
32+
}
33+
34+
public function getInterval()
35+
{
36+
return $this->interval;
37+
}
38+
39+
public function getCallback()
40+
{
41+
return $this->callback;
42+
}
43+
44+
public function setData($data)
45+
{
46+
$this->data = $data;
47+
}
48+
49+
public function getData()
50+
{
51+
return $this->data;
52+
}
53+
54+
public function isPeriodic()
55+
{
56+
return $this->periodic;
57+
}
58+
59+
public function cancel()
60+
{
61+
$this->loop->cancelTimer($this);
62+
}
63+
}

Timer/TimerInterface.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace React\EventLoop\Timer;
4+
5+
interface TimerInterface
6+
{
7+
public function getLoop();
8+
public function getInterval();
9+
public function getCallback();
10+
public function setData($data);
11+
public function getData();
12+
public function isPeriodic();
13+
public function cancel();
14+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy