Skip to content

Commit

Permalink
Created dedicated class Adapter\Tornado\Internal\Task
Browse files Browse the repository at this point in the history
  • Loading branch information
Benoit Viguier committed Oct 24, 2018
1 parent 29f45c2 commit 4d1d819
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 16 deletions.
30 changes: 14 additions & 16 deletions src/Adapter/Tornado/EventLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ class EventLoop implements \M6Web\Tornado\EventLoop
* @var Internal\StreamEventLoop
*/
private $streamLoop;

/**
* @var Internal\Task[]
*/
private $tasks = [];

public function __construct()
Expand Down Expand Up @@ -47,33 +51,33 @@ function (\Throwable $throwable) use (&$finalAction, &$promiseIsPending) {
$this->tasks = [];
foreach ($allTasks as $task) {
try {
if (!$task->generator->valid()) {
$task->promise->resolve($task->generator->getReturn());
if (!$task->getGenerator()->valid()) {
$task->getPromise()->resolve($task->getGenerator()->getReturn());
// This task is finished
continue;
}

$blockingPromise = Internal\PendingPromise::fromGenerator($task->generator);
$blockingPromise = Internal\PendingPromise::fromGenerator($task->getGenerator());
$blockingPromise->addCallbacks(
function ($value) use ($task) {
try {
$task->generator->send($value);
$task->getGenerator()->send($value);
$this->tasks[] = $task;
} catch (\Throwable $exception) {
$task->promise->reject($exception);
$task->getPromise()->reject($exception);
}
},
function (\Throwable $throwable) use ($task) {
try {
$task->generator->throw($throwable);
$task->getGenerator()->throw($throwable);
$this->tasks[] = $task;
} catch (\Throwable $exception) {
$task->promise->reject($exception);
$task->getPromise()->reject($exception);
}
}
);
} catch (\Throwable $exception) {
$task->promise->reject($exception);
$task->getPromise()->reject($exception);
}
}
} while ($promiseIsPending && $somethingToDo());
Expand All @@ -86,15 +90,9 @@ function (\Throwable $throwable) use ($task) {
*/
public function async(\Generator $generator): Promise
{
$task = new class() {
public $generator;
public $promise;
};
$task->generator = $generator;
$task->promise = new Internal\PendingPromise();
$this->tasks[] = $task;
$this->tasks[] = ($task = new Internal\Task($generator));

return $task->promise;
return $task->getPromise();
}

/**
Expand Down
29 changes: 29 additions & 0 deletions src/Adapter/Tornado/Internal/Task.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace M6Web\Tornado\Adapter\Tornado\Internal;

/**
* @internal
* ⚠️ You must NOT rely on this internal implementation
*/
class Task
{
private $generator;
private $promise;

public function __construct(\Generator $generator)
{
$this->generator = $generator;
$this->promise = new PendingPromise();
}

public function getPromise(): PendingPromise
{
return $this->promise;
}

public function getGenerator(): \Generator
{
return $this->generator;
}
}

0 comments on commit 4d1d819

Please sign in to comment.