Skip to content

Support promise cancellation #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 15, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Calling cancel() on resulting promise should cancel all pending tasks
  • Loading branch information
clue committed Nov 15, 2021
commit 36eb448496a8530a6be643b92d46a1e65cc7883d
31 changes: 26 additions & 5 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,14 @@ function ($error) use (&$exception, &$rejected, &$wait) {
function parallel(array $tasks)
{
$pending = array();
$deferred = new Deferred();
$deferred = new Deferred(function () use (&$pending) {
foreach ($pending as $promise) {
if ($promise instanceof CancellablePromiseInterface) {
$promise->cancel();
}
}
$pending = array();
});
$results = array();
$errored = false;

Expand Down Expand Up @@ -148,7 +155,13 @@ function parallel(array $tasks)
*/
function series(array $tasks)
{
$deferred = new Deferred();
$pending = null;
$deferred = new Deferred(function () use (&$pending) {
if ($pending instanceof CancellablePromiseInterface) {
$pending->cancel();
}
$pending = null;
});
$results = array();

/** @var callable():void $next */
Expand All @@ -157,7 +170,7 @@ function series(array $tasks)
$next();
};

$next = function () use (&$tasks, $taskCallback, $deferred, &$results) {
$next = function () use (&$tasks, $taskCallback, $deferred, &$results, &$pending) {
if (0 === count($tasks)) {
$deferred->resolve($results);
return;
Expand All @@ -166,6 +179,7 @@ function series(array $tasks)
$task = array_shift($tasks);
$promise = call_user_func($task);
assert($promise instanceof PromiseInterface);
$pending = $promise;

$promise->then($taskCallback, array($deferred, 'reject'));
};
Expand All @@ -181,10 +195,16 @@ function series(array $tasks)
*/
function waterfall(array $tasks)
{
$deferred = new Deferred();
$pending = null;
$deferred = new Deferred(function () use (&$pending) {
if ($pending instanceof CancellablePromiseInterface) {
$pending->cancel();
}
$pending = null;
});

/** @var callable $next */
$next = function ($value = null) use (&$tasks, &$next, $deferred) {
$next = function ($value = null) use (&$tasks, &$next, $deferred, &$pending) {
if (0 === count($tasks)) {
$deferred->resolve($value);
return;
Expand All @@ -193,6 +213,7 @@ function waterfall(array $tasks)
$task = array_shift($tasks);
$promise = call_user_func_array($task, func_get_args());
assert($promise instanceof PromiseInterface);
$pending = $promise;

$promise->then($next, array($deferred, 'reject'));
};
Expand Down
23 changes: 23 additions & 0 deletions tests/ParallelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,29 @@ function () use (&$cancelled) {
$this->assertSame(1, $cancelled);
}

public function testParallelWillCancelPendingPromisesWhenCallingCancelOnResultingPromise()
{
$cancelled = 0;

$tasks = array(
function () use (&$cancelled) {
return new Promise(function () { }, function () use (&$cancelled) {
$cancelled++;
});
},
function () use (&$cancelled) {
return new Promise(function () { }, function () use (&$cancelled) {
$cancelled++;
});
}
);

$promise = React\Async\parallel($tasks);
$promise->cancel();

$this->assertSame(2, $cancelled);
}

public function testParallelWithDelayedErrorReturnsPromiseRejectedWithExceptionFromTask()
{
$called = 0;
Expand Down
23 changes: 23 additions & 0 deletions tests/SeriesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,27 @@ function () use (&$called) {

$this->assertSame(1, $called);
}

public function testSeriesWillCancelFirstPendingPromiseWhenCallingCancelOnResultingPromise()
{
$cancelled = 0;

$tasks = array(
function () {
return new Promise(function ($resolve) {
$resolve();
});
},
function () use (&$cancelled) {
return new Promise(function () { }, function () use (&$cancelled) {
$cancelled++;
});
}
);

$promise = React\Async\series($tasks);
$promise->cancel();

$this->assertSame(1, $cancelled);
}
}
23 changes: 23 additions & 0 deletions tests/WaterfallTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,27 @@ function () use (&$called) {

$this->assertSame(1, $called);
}

public function testWaterfallWillCancelFirstPendingPromiseWhenCallingCancelOnResultingPromise()
{
$cancelled = 0;

$tasks = array(
function () {
return new Promise(function ($resolve) {
$resolve();
});
},
function () use (&$cancelled) {
return new Promise(function () { }, function () use (&$cancelled) {
$cancelled++;
});
}
);

$promise = React\Async\waterfall($tasks);
$promise->cancel();

$this->assertSame(1, $cancelled);
}
}
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy