-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
ReactAdapter.php
235 lines (196 loc) · 6.39 KB
/
ReactAdapter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
namespace Amp;
use AsyncInterop\Loop\Driver;
use React\EventLoop\LoopInterface;
use React\EventLoop\Timer\Timer;
use React\EventLoop\Timer\TimerInterface;
class ReactAdapter implements LoopInterface {
private $driver;
private $inNextTick = false;
private $readWatchers = [];
private $writeWatchers = [];
private $timers = [];
public function __construct(Driver $driver) {
$this->driver = $driver;
}
/**
* Register a listener to be notified when a stream is ready to read.
*
* @param resource $stream The PHP stream resource to check.
* @param callable $listener Invoked when the stream is ready.
*/
public function addReadStream($stream, callable $listener) {
$watcher = $this->driver->onReadable($stream, function () use ($stream, $listener) {
$listener($stream, $this);
});
$this->readWatchers[(int) $stream][] = $watcher;
}
/**
* Register a listener to be notified when a stream is ready to write.
*
* @param resource $stream The PHP stream resource to check.
* @param callable $listener Invoked when the stream is ready.
*/
public function addWriteStream($stream, callable $listener) {
$watcher = $this->driver->onWritable($stream, function () use ($stream, $listener) {
$listener($stream, $this);
});
$this->writeWatchers[(int) $stream][] = $watcher;
}
/**
* Remove the read event listener for the given stream.
*
* @param resource $stream The PHP stream resource.
*/
public function removeReadStream($stream) {
$key = (int) $stream;
if (!isset($this->readWatchers[$key])) {
return;
}
foreach ($this->readWatchers[$key] as $watcher) {
$this->driver->cancel($watcher);
}
unset($this->readWatchers[$key]);
}
/**
* Remove the write event listener for the given stream.
*
* @param resource $stream The PHP stream resource.
*/
public function removeWriteStream($stream) {
$key = (int) $stream;
if (!isset($this->writeWatchers[$key])) {
return;
}
foreach ($this->writeWatchers[$key] as $watcher) {
$this->driver->cancel($watcher);
}
unset($this->writeWatchers[$key]);
}
/**
* Remove all listeners for the given stream.
*
* @param resource $stream The PHP stream resource.
*/
public function removeStream($stream) {
$this->removeReadStream($stream);
$this->removeWriteStream($stream);
}
/**
* Enqueue a callback to be invoked once after the given interval.
*
* The execution order of timers scheduled to execute at the same time is
* not guaranteed.
*
* @param int|float $interval The number of seconds to wait before execution.
* @param callable $callback The callback to invoke.
*
* @return TimerInterface
*/
public function addTimer($interval, callable $callback) {
$timer = new Timer($this, $interval, $callback, false);
$this->timers[spl_object_hash($timer)] = $timer;
$this->driver->delay((int) (1000 * $interval), function () use ($timer, $callback) {
$this->cancelTimer($timer);
$callback($timer);
});
return $timer;
}
/**
* Enqueue a callback to be invoked repeatedly after the given interval.
*
* The execution order of timers scheduled to execute at the same time is
* not guaranteed.
*
* @param int|float $interval The number of seconds to wait before execution.
* @param callable $callback The callback to invoke.
*
* @return TimerInterface
*/
public function addPeriodicTimer($interval, callable $callback) {
$timer = new Timer($this, $interval, $callback, true);
$watcher = $this->driver->repeat((int) (1000 * $interval), function () use ($timer, $callback) {
$callback($timer);
});
$this->timers[spl_object_hash($timer)] = $watcher;
return $timer;
}
/**
* Cancel a pending timer.
*
* @param TimerInterface $timer The timer to cancel.
*/
public function cancelTimer(TimerInterface $timer) {
if (!isset($this->timers[spl_object_hash($timer)])) {
return;
}
$this->driver->cancel($this->timers[spl_object_hash($timer)]);
unset($this->timers[spl_object_hash($timer)]);
}
/**
* Check if a given timer is active.
*
* @param TimerInterface $timer The timer to check.
*
* @return boolean True if the timer is still enqueued for execution.
*/
public function isTimerActive(TimerInterface $timer) {
return isset($this->timers[spl_object_hash($timer)]);
}
/**
* Schedule a callback to be invoked on the next tick of the event loop.
*
* Callbacks are guaranteed to be executed in the order they are enqueued,
* before any timer or stream events.
*
* @param callable $listener The callback to invoke.
*/
public function nextTick(callable $listener) {
if ($this->inNextTick) {
$listener($this);
return;
}
$this->driver->defer(function () use ($listener) {
$previousValue = $this->inNextTick;
$this->inNextTick = true;
try {
$listener($this);
} finally {
$this->inNextTick = $previousValue;
}
});
}
/**
* Schedule a callback to be invoked on a future tick of the event loop.
*
* Callbacks are guaranteed to be executed in the order they are enqueued.
*
* @param callable $listener The callback to invoke.
*/
public function futureTick(callable $listener) {
$this->driver->defer(function () use ($listener) {
$listener($this);
});
}
/**
* Perform a single iteration of the event loop.
*/
public function tick() {
$this->driver->defer(function () {
$this->driver->stop();
});
$this->run();
}
/**
* Run the event loop until there are no more tasks to perform.
*/
public function run() {
$this->driver->run();
}
/**
* Instruct a running event loop to stop.
*/
public function stop() {
$this->driver->stop();
}
}