Skip to content

Commit

Permalink
feat(cancellation): csfix
Browse files Browse the repository at this point in the history
  • Loading branch information
c-moncy committed Dec 23, 2019
1 parent 6c662ff commit 9265417
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 22 deletions.
11 changes: 5 additions & 6 deletions examples/05-cancellable-promise.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@
//$eventLoop = new Adapter\Amp\EventLoop();
//$eventLoop = new Adapter\ReactPhp\EventLoop(new React\EventLoop\StreamSelectLoop());


function timer(EventLoop $eventLoop, string $id, int $time, \M6Web\Tornado\Promise &$promise = null)
{
$result = "not resolved";
$result = 'not resolved';
try {
echo "[$id] Starting to wait $time\n";
yield $eventLoop->delay($time);
Expand All @@ -32,27 +31,27 @@ function timer(EventLoop $eventLoop, string $id, int $time, \M6Web\Tornado\Promi
} catch (\Exception $e) {
echo "[$id] XXX Cancelled …\n";
}

return $result;
}

echo "Let's start!\n";

try {

$result = $eventLoop->wait(
$eventLoop->promiseAll(
$p1 = $eventLoop->async(timer($eventLoop, 'A', 100)),
$eventLoop->async(timer($eventLoop, 'B', 10, $p1))
)
);
} catch (\M6Web\Tornado\CancelledException $e) {
$result = "cancelled promise";
$result = 'cancelled promise';
} catch (\Exception $e) {
$result = "other exception";
$result = 'other exception';
}

echo "async cancellation result :\n";

var_dump($result);

echo "Finished!\n";
echo "Finished!\n";
15 changes: 6 additions & 9 deletions examples/06-advanced-cancellable-promise.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@
$eventLoop = new Adapter\Amp\EventLoop();
//$eventLoop = new Adapter\ReactPhp\EventLoop(new React\EventLoop\StreamSelectLoop());


function timer(EventLoop $eventLoop, string $id, int $time, \M6Web\Tornado\Promise &$promise = null)
{
$result = "not resolved";
$result = 'not resolved';
try {
echo "[$id] Starting to wait $time\n";
yield $eventLoop->delay($time);
Expand Down Expand Up @@ -45,32 +44,30 @@ function timer(EventLoop $eventLoop, string $id, int $time, \M6Web\Tornado\Promi
} catch (\Exception $e) {
echo "[$id] Cancelled … : clean workspace and other stuff\n";
}

return $result;
}

echo "Let's start!\n";

try {

$result = $eventLoop->wait(
$eventLoop->promiseAll(
$promise = $eventLoop->promiseAll(
$eventLoop->async(timer($eventLoop, ' Timer A ', 100)),
$eventLoop->async(timer($eventLoop, ' Timer B ', 100))
)
,
),
$eventLoop->async(timer($eventLoop, 'Canceller', 100, $promise))
)
);

} catch (\M6Web\Tornado\CancelledException $e) {
$result = "cancelled promise";
$result = 'cancelled promise';
} catch (\Exception $e) {
$result = "other exception";
$result = 'other exception';
}

echo "async cancellation with result concervation:\n";

var_dump($result);

echo "Finished!\n";
echo "Finished!\n";
2 changes: 1 addition & 1 deletion src/Adapter/Amp/EventLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function async(\Generator $generator): Promise
while ($generator->valid()) {
$blockingPromise = $generator->current();
if (!$blockingPromise instanceof Promise) {
throw new \Error('Asynchronous function is yielding a [' . gettype($blockingPromise) . '] instead of a Promise.');
throw new \Error('Asynchronous function is yielding a ['.gettype($blockingPromise).'] instead of a Promise.');
}
$currentPromise = $blockingPromise;
$blockingPromise = Internal\PromiseWrapper::toHandledPromise(
Expand Down
8 changes: 4 additions & 4 deletions src/Adapter/Tornado/EventLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,9 @@ public function async(\Generator $generator): Promise
function () use (&$task, &$promise) {
if ($task->getGenerator()->current()) {
$promise->reject(new CancelledException('async cancellation'));
/** @var PendingPromise $currentPromise */
$currentPromise = $task->getGenerator()->current();
if (method_exists($currentPromise, 'isCanceled') && !$currentPromise->isCanceled()) {
if (!$currentPromise->isCancelled()) {
$currentPromise->cancel();
}
}
Expand Down Expand Up @@ -201,7 +202,6 @@ public function promiseRace(Promise ...$promises): Promise
return $this->promiseFulfilled(null);
}


$promisesCancellation = function () use (&$wrappedPromise) {
foreach ($wrappedPromise as $index => $promise) {
$promise->cancel();
Expand Down Expand Up @@ -287,10 +287,10 @@ public function delay(int $milliseconds): Promise
yield $this->idle();
} catch (\Throwable $throwable) {
$promise->reject($throwable);
return new CancelledException('cancelled kiki');

return new CancelledException('cancelled promise');
}
}

})());

return $promise;
Expand Down
4 changes: 2 additions & 2 deletions src/Adapter/Tornado/Internal/PendingPromise.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ public static function createUnhandled(FailingPromiseCollection $failingPromiseC
{
$promiseWrapper = new self();
$promiseWrapper->failingPromiseCollection = $failingPromiseCollection;
$promiseWrapper->cancellation = $cancellation ?? function() {};
$promiseWrapper->cancellation = $cancellation ?? function () {};

return $promiseWrapper;
}

public static function createHandled(callable $cancellation = null)
{
$promiseWrapper = new self();
$promiseWrapper->cancellation = $cancellation ?? function() {};
$promiseWrapper->cancellation = $cancellation ?? function () {};
$promiseWrapper->failingPromiseCollection = null;

return $promiseWrapper;
Expand Down

0 comments on commit 9265417

Please sign in to comment.