functions.php 2.7 KB

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