1<?php 2 3namespace Sabre\Event\Loop; 4 5/** 6 * Executes a function after x seconds. 7 * 8 * @param callable $cb 9 * @param float $timeout timeout in seconds 10 * @return void 11 */ 12function setTimeout(callable $cb, $timeout) { 13 14 instance()->setTimeout($cb, $timeout); 15 16} 17 18/** 19 * Executes a function every x seconds. 20 * 21 * The value this function returns can be used to stop the interval with 22 * clearInterval. 23 * 24 * @param callable $cb 25 * @param float $timeout 26 * @return array 27 */ 28function setInterval(callable $cb, $timeout) { 29 30 return instance()->setInterval($cb, $timeout); 31 32} 33 34/** 35 * Stops a running internval. 36 * 37 * @param array $intervalId 38 * @return void 39 */ 40function clearInterval($intervalId) { 41 42 instance()->clearInterval($intervalId); 43 44} 45 46/** 47 * Runs a function immediately at the next iteration of the loop. 48 * 49 * @param callable $cb 50 * @return void 51 */ 52function nextTick(callable $cb) { 53 54 instance()->nextTick($cb); 55 56} 57 58 59/** 60 * Adds a read stream. 61 * 62 * The callback will be called as soon as there is something to read from 63 * the stream. 64 * 65 * You MUST call removeReadStream after you are done with the stream, to 66 * prevent the eventloop from never stopping. 67 * 68 * @param resource $stream 69 * @param callable $cb 70 * @return void 71 */ 72function addReadStream($stream, callable $cb) { 73 74 instance()->addReadStream($stream, $cb); 75 76} 77 78/** 79 * Adds a write stream. 80 * 81 * The callback will be called as soon as the system reports it's ready to 82 * receive writes on the stream. 83 * 84 * You MUST call removeWriteStream after you are done with the stream, to 85 * prevent the eventloop from never stopping. 86 * 87 * @param resource $stream 88 * @param callable $cb 89 * @return void 90 */ 91function addWriteStream($stream, callable $cb) { 92 93 instance()->addWriteStream($stream, $cb); 94 95} 96 97/** 98 * Stop watching a stream for reads. 99 * 100 * @param resource $stream 101 * @return void 102 */ 103function removeReadStream($stream) { 104 105 instance()->removeReadStream($stream); 106 107} 108 109/** 110 * Stop watching a stream for writes. 111 * 112 * @param resource $stream 113 * @return void 114 */ 115function removeWriteStream($stream) { 116 117 instance()->removeWriteStream($stream); 118 119} 120 121 122/** 123 * Runs the loop. 124 * 125 * This function will run continiously, until there's no more events to 126 * handle. 127 * 128 * @return void 129 */ 130function run() { 131 132 instance()->run(); 133 134} 135 136/** 137 * Executes all pending events. 138 * 139 * If $block is turned true, this function will block until any event is 140 * triggered. 141 * 142 * If there are now timeouts, nextTick callbacks or events in the loop at 143 * all, this function will exit immediately. 144 * 145 * This function will return true if there are _any_ events left in the 146 * loop after the tick. 147 * 148 * @param bool $block 149 * @return bool 150 */ 151function tick($block = false) { 152 153 return instance()->tick($block); 154 155} 156 157/** 158 * Stops a running eventloop 159 * 160 * @return void 161 */ 162function stop() { 163 164 instance()->stop(); 165 166} 167 168/** 169 * Retrieves or sets the global Loop object. 170 * 171 * @param Loop $newLoop 172 */ 173function instance(Loop $newLoop = null) { 174 175 static $loop; 176 if ($newLoop) { 177 $loop = $newLoop; 178 } elseif (!$loop) { 179 $loop = new Loop(); 180 } 181 return $loop; 182 183} 184