@ 2015-10-10T00:02:49Z <?php
class Operation
{
protected $routine;
protected $promise;
public function __construct(\Closure $routine, $promise)
{
$this->routine = $routine;
$this->promise = $promise;
}
public function run()
{
$routine = $this->routine();
$routine($this->promise);
}
public function getPromise()
{
return $this->promise;
}
}
class Promise
{
protected $successClosure;
protected $failClosure;
public function fulfill(...$args)
{
$closure = $this->successClosure;
$closure(...$args);
}
public function reject(...$args)
{
$closure = $this->failClosure;
$closure(...$args);
}
public function then(\Closure $success, \Closure $fail)
{
$this->successClosure = $success;
$this->failClosure = $fail;
}
}
class Queue
{
protected $queue = [];
public function append(Operation $operation) {
$this->queue[] = $operation;
}
public function getOperationGenerator()
{
foreach ($this->queue as $operation) {
yield $operation;
}
}
}
class QueueHandler
{
protected $queues = [];
public function addQueue(Queue $queue)
{
$this->queues[] = $queue;
}
protected function getOperationGenerator()
{
$queues = [];
foreach ($this->queues as $queue) {
$queues[] = $queue->getOperationGenerator();
}
foreach ($this->queues as $queue) {
yield $queue;
}
}
protected function getRunGenerator()
{
$operation_generator = $this->getOperationGenerator();
foreach ($operation_generator as $operation) {
yield $operation;
}
}
public function run() {
$generator = $this->getRunGenerator();
while (1) {
foreach ($generator as $operation) {
$operation->run();
}
}
}
}
$main_queue = new Queue;
$secondary_queue = new Queue;
function async(\Closure $routine, Queue $queue) : Promise {
$promise = new Promise();
$operation = new Operation($operation, $promise);
$queue[] = $operation;
return $promise;
}
async(function(Promise $promise) {
var_dump('Main Fired');
$promise->fulfill();
}, $main_queue)->then(function() {
var_dump('Main fulfilled!');
}, function() {
var_dump('Main rejected.');
});
async(function(Promise $promise) {
var_dump('Secondary Fired');
$promise->fulfill();
}, $secondary_queue)->then(function() {
var_dump('Secondary fulfilled!');
}, function() {
var_dump('Main rejected.');
});
$queue_handler = new QueueHandler();
$queue_handler->addQueue($main_queue);
$queue_handler->addQueue($secondary_queue);
$queue->run();
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for git.master , git.master_jit , rfc.property-hooks Warning: Undefined variable $operation in /in/7P8Fm on line 123
Fatal error: Uncaught TypeError: Operation::__construct(): Argument #1 ($routine) must be of type Closure, null given, called in /in/7P8Fm on line 123 and defined in /in/7P8Fm:10
Stack trace:
#0 /in/7P8Fm(123): Operation->__construct(NULL, Object(Promise))
#1 /in/7P8Fm(130): async(Object(Closure), Object(Queue))
#2 {main}
thrown in /in/7P8Fm on line 10
Process exited with code 255 . This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.
Active branches Archived branches Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page
preferences:dark mode live preview
42.86 ms | 401 KiB | 8 Q