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
|
<?php
namespace Sabre\Event\Loop;
/**
* Executes a function after x seconds.
*
* @param callable $cb
* @param float $timeout timeout in seconds
* @return void
*/
function setTimeout(callable $cb, $timeout) {
instance()->setTimeout($cb, $timeout);
}
/**
* Executes a function every x seconds.
*
* The value this function returns can be used to stop the interval with
* clearInterval.
*
* @param callable $cb
* @param float $timeout
* @return array
*/
function setInterval(callable $cb, $timeout) {
return instance()->setInterval($cb, $timeout);
}
/**
* Stops a running internval.
*
* @param array $intervalId
* @return void
*/
function clearInterval($intervalId) {
instance()->clearInterval($intervalId);
}
/**
* Runs a function immediately at the next iteration of the loop.
*
* @param callable $cb
* @return void
*/
function nextTick(callable $cb) {
instance()->nextTick($cb);
}
/**
* Adds a read stream.
*
* The callback will be called as soon as there is something to read from
* the stream.
*
* You MUST call removeReadStream after you are done with the stream, to
* prevent the eventloop from never stopping.
*
* @param resource $stream
* @param callable $cb
* @return void
*/
function addReadStream($stream, callable $cb) {
instance()->addReadStream($stream, $cb);
}
/**
* Adds a write stream.
*
* The callback will be called as soon as the system reports it's ready to
* receive writes on the stream.
*
* You MUST call removeWriteStream after you are done with the stream, to
* prevent the eventloop from never stopping.
*
* @param resource $stream
* @param callable $cb
* @return void
*/
function addWriteStream($stream, callable $cb) {
instance()->addWriteStream($stream, $cb);
}
/**
* Stop watching a stream for reads.
*
* @param resource $stream
* @return void
*/
function removeReadStream($stream) {
instance()->removeReadStream($stream);
}
/**
* Stop watching a stream for writes.
*
* @param resource $stream
* @return void
*/
function removeWriteStream($stream) {
instance()->removeWriteStream($stream);
}
/**
* Runs the loop.
*
* This function will run continiously, until there's no more events to
* handle.
*
* @return void
*/
function run() {
instance()->run();
}
/**
* Executes all pending events.
*
* If $block is turned true, this function will block until any event is
* triggered.
*
* If there are now timeouts, nextTick callbacks or events in the loop at
* all, this function will exit immediately.
*
* This function will return true if there are _any_ events left in the
* loop after the tick.
*
* @param bool $block
* @return bool
*/
function tick($block = false) {
return instance()->tick($block);
}
/**
* Stops a running eventloop
*
* @return void
*/
function stop() {
instance()->stop();
}
/**
* Retrieves or sets the global Loop object.
*
* @param Loop $newLoop
*/
function instance(Loop $newLoop = null) {
static $loop;
if ($newLoop) {
$loop = $newLoop;
} elseif (!$loop) {
$loop = new Loop();
}
return $loop;
}
|