Skip to content

Commit

Permalink
Drop support for promises
Browse files Browse the repository at this point in the history
We have Awaitable now and no longer need promises. Emitter is now compatible with \Iterator. Removed many things, because they're no longer needed.
  • Loading branch information
kelunik committed Jul 16, 2018
1 parent 967cb38 commit b242929
Show file tree
Hide file tree
Showing 67 changed files with 703 additions and 6,489 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"concurrent-php/async-api": "dev-master"
},
"require-dev": {
"amphp/phpunit-util": "^1",
"amphp/phpunit-util": "dev-ext-task",
"react/promise": "^2",
"friendsofphp/php-cs-fixer": "^2.3",
"phpunit/phpunit": "^6.0.9",
Expand Down Expand Up @@ -69,7 +69,7 @@
},
"config": {
"platform": {
"php": "7.0.0"
"php": "7.2.0"
}
},
"scripts": {
Expand Down
74 changes: 0 additions & 74 deletions lib/CallableMaker.php

This file was deleted.

17 changes: 17 additions & 0 deletions lib/Cancellation/CancelledException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Amp\Cancellation;

/**
* Will be thrown in case an operation is cancelled.
*
* @see Token
* @see CancellationTokenSource
*/
class CancelledException extends \Exception
{
public function __construct(string $message = "The operation was cancelled", \Throwable $previous = null)
{
parent::__construct($message, 0, $previous);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Amp;
namespace Amp\Cancellation;

/**
* A NullCancellationToken can be used to avoid conditionals to check whether a token has been provided.
Expand All @@ -25,7 +25,7 @@
*
* instead.
*/
final class NullCancellationToken implements CancellationToken
final class NullToken implements Token
{
/** @inheritdoc */
public function subscribe(callable $callback): string
Expand All @@ -34,7 +34,7 @@ public function subscribe(callable $callback): string
}

/** @inheritdoc */
public function unsubscribe(string $id)
public function unsubscribe(string $id): void
{
// nothing to do
}
Expand All @@ -46,7 +46,7 @@ public function isRequested(): bool
}

/** @inheritdoc */
public function throwIfRequested()
public function throwIfRequested(): void
{
// nothing to do
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
<?php

namespace Amp;
namespace Amp\Cancellation;

use Amp\Loop;
use Amp\TimeoutException;

/**
* A TimeoutCancellationToken automatically requests cancellation after the timeout has elapsed.
*/
final class TimeoutCancellationToken implements CancellationToken
final class TimeoutToken implements Token
{
/** @var string */
private $watcher;

/** @var \Amp\CancellationToken */
/** @var Token */
private $token;

/**
* @param int $timeout Milliseconds until cancellation is requested.
*/
public function __construct(int $timeout)
{
$source = new CancellationTokenSource;
$source = new TokenSource;
$this->token = $source->getToken();

$this->watcher = Loop::delay($timeout, static function () use ($source) {
Expand Down Expand Up @@ -46,7 +49,7 @@ public function subscribe(callable $callback): string
/**
* {@inheritdoc}
*/
public function unsubscribe(string $id)
public function unsubscribe(string $id): void
{
$this->token->unsubscribe($id);
}
Expand All @@ -62,7 +65,7 @@ public function isRequested(): bool
/**
* {@inheritdoc}
*/
public function throwIfRequested()
public function throwIfRequested(): void
{
$this->token->throwIfRequested();
}
Expand Down
10 changes: 6 additions & 4 deletions lib/CancellationToken.php → lib/Cancellation/Token.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php

namespace Amp;
namespace Amp\Cancellation;

/**
* Cancellation tokens are simple objects that allow registering handlers to subscribe to cancellation requests.
*/
interface CancellationToken
interface Token
{
/**
* Subscribes a new handler to be invoked on a cancellation request.
Expand All @@ -29,7 +29,7 @@ public function subscribe(callable $callback): string;
*
* @return void
*/
public function unsubscribe(string $id);
public function unsubscribe(string $id): void;

/**
* Returns whether cancellation has been requested yet.
Expand All @@ -42,6 +42,8 @@ public function isRequested(): bool;
* Throws the `CancelledException` if cancellation has been requested, otherwise does nothing.
*
* @return void
*
* @throws CancelledException
*/
public function throwIfRequested();
public function throwIfRequested(): void;
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
<?php

namespace Amp;

use React\Promise\PromiseInterface as ReactPromise;
use function Amp\Promise\rethrow;
namespace Amp\Cancellation;

/**
* A cancellation token source provides a mechanism to cancel operations.
Expand Down Expand Up @@ -35,25 +32,26 @@
* }
* ```
*
* @see CancellationToken
* @see Token
* @see CancelledException
*/
final class CancellationTokenSource
final class TokenSource
{
private $token;
private $onCancel;

public function __construct()
{
$this->token = new class($this->onCancel) implements CancellationToken {
$this->token = new class($this->onCancel) implements Token
{
/** @var string */
private $nextId = "a";

/** @var callable[] */
private $callbacks = [];

/** @var \Throwable|null */
private $exception = null;
private $exception;

public function __construct(&$onCancel)
{
Expand All @@ -64,79 +62,59 @@ public function __construct(&$onCancel)
$this->callbacks = [];

foreach ($callbacks as $callback) {
$this->invokeCallback($callback);
$callback($this->exception);
}
};
}

private function invokeCallback($callback)
{
// No type declaration to prevent exception outside the try!
try {
$result = $callback($this->exception);

if ($result instanceof \Generator) {
$result = new Coroutine($result);
}

if ($result instanceof Promise || $result instanceof ReactPromise) {
rethrow($result);
}
} catch (\Throwable $exception) {
Loop::defer(static function () use ($exception) {
throw $exception;
});
}
}

public function subscribe(callable $callback): string
{
$id = $this->nextId++;

if ($this->exception) {
$this->invokeCallback($callback);
$callback($this->exception);
} else {
$this->callbacks[$id] = $callback;
}

return $id;
}

public function unsubscribe(string $id)
public function unsubscribe(string $id): void
{
unset($this->callbacks[$id]);
}

public function isRequested(): bool
{
return isset($this->exception);
return $this->exception !== null;
}

public function throwIfRequested()
public function throwIfRequested(): void
{
if (isset($this->exception)) {
if ($this->exception !== null) {
throw $this->exception;
}
}
};
}

public function getToken(): CancellationToken
public function getToken(): Token
{
return $this->token;
}

/**
* @param \Throwable|null $previous Exception to be used as the previous exception to CancelledException.
*/
public function cancel(\Throwable $previous = null)
public function cancel(\Throwable $previous = null): void
{
if ($this->onCancel === null) {
return;
}

$onCancel = $this->onCancel;
$this->onCancel = null;
$onCancel(new CancelledException($previous));
$onCancel(new CancelledException("The operation was cancelled", $previous));
}
}
17 changes: 0 additions & 17 deletions lib/CancelledException.php

This file was deleted.

Loading

0 comments on commit b242929

Please sign in to comment.