-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bootstrap Rx scheduler
- Loading branch information
Showing
5 changed files
with
176 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
namespace EventLoop; | ||
|
||
use React\EventLoop\Factory; | ||
use React\EventLoop\LibEventLoop; | ||
use React\EventLoop\LoopInterface; | ||
|
||
class EventLoop | ||
{ | ||
/** @var LibEventLoop */ | ||
static private $loop; | ||
|
||
static public function setLoop(LoopInterface $loop) { | ||
if (static::$loop === null) { | ||
static::$loop = $loop; | ||
} | ||
|
||
if (static::$loop !== $loop) { | ||
throw new \Exception("Two different loop instances created. Something is not right."); | ||
} | ||
|
||
static::registerLoopRunner(); | ||
} | ||
|
||
static public function getLoop() { | ||
if (static::$loop) { | ||
return static::$loop; | ||
} | ||
|
||
static::$loop = Factory::create(); | ||
|
||
static::registerLoopRunner(); | ||
|
||
return static::$loop; | ||
} | ||
|
||
static private function registerLoopRunner() { | ||
$hasBeenRun = false; | ||
|
||
register_shutdown_function(function () use (&$hasBeenRun) { | ||
if (!$hasBeenRun) { | ||
EventLoop::getLoop()->run(); | ||
} | ||
}); | ||
|
||
static::$loop->nextTick(function () use (&$hasBeenRun) { | ||
$hasBeenRun = true; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,7 @@ | ||
<?php | ||
|
||
use Interop\Async\Loop; | ||
use WyriHaximus\React\AsyncInteropLoop\ReactDriverFactory; | ||
|
||
Loop::setFactory(ReactDriverFactory::createFactory()); | ||
|
||
register_shutdown_function(function () { | ||
Loop::execute(function () {}, Loop::get()); | ||
}); | ||
if (class_exists('\Rx\Scheduler\EventLoopScheduler') && class_exists('\Rx\Scheduler')) { | ||
\Rx\Scheduler::setDefaultFactory(function () { | ||
return new \Rx\Scheduler\EventLoopScheduler(\EventLoop\getLoop()); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
namespace EventLoop; | ||
|
||
use React\EventLoop\LoopInterface; | ||
use React\EventLoop\Timer\TimerInterface; | ||
|
||
/** | ||
* @param LoopInterface $loop | ||
* @throws \Exception | ||
*/ | ||
function setLoop(LoopInterface $loop) { | ||
EventLoop::setLoop($loop); | ||
} | ||
|
||
/** | ||
* @return \React\EventLoop\LibEventLoop | ||
*/ | ||
function getLoop() { | ||
return EventLoop::getLoop(); | ||
} | ||
|
||
/** | ||
* 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 | ||
*/ | ||
function addTimer($interval, callable $callback) { | ||
return getLoop()->addTimer($interval, $callback); | ||
} | ||
|
||
/** | ||
* 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 | ||
*/ | ||
function addPeriodicTimer($interval, callable $callback) { | ||
return getLoop()->addPeriodicTimer($interval, $callback); | ||
} | ||
|
||
/** | ||
* Cancel a pending timer. | ||
* | ||
* @param TimerInterface $timer The timer to cancel. | ||
*/ | ||
function cancelTimer(TimerInterface $timer) { | ||
getLoop()->cancelTimer($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. | ||
*/ | ||
function isTimerActive(TimerInterface $timer) { | ||
return getLoop()->isTimerActive($timer); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters