mirror of
https://github.com/danog/amp.git
synced 2024-12-02 17:37:50 +01:00
Add Deferred::isResolved() and StreamSource::isComplete()
This commit is contained in:
parent
1577906fc1
commit
0ddf9a656e
@ -23,6 +23,7 @@ final class Deferred
|
|||||||
use Internal\Placeholder {
|
use Internal\Placeholder {
|
||||||
resolve as public;
|
resolve as public;
|
||||||
fail as public;
|
fail as public;
|
||||||
|
isResolved as public;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -37,6 +38,14 @@ final class Deferred
|
|||||||
return $this->promise;
|
return $this->promise;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return bool True if the contained promise has been resolved.
|
||||||
|
*/
|
||||||
|
public function isResolved(): bool
|
||||||
|
{
|
||||||
|
return $this->resolver->isResolved();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fulfill the promise with the given value.
|
* Fulfill the promise with the given value.
|
||||||
*
|
*
|
||||||
|
@ -218,6 +218,14 @@ final class EmitSource
|
|||||||
return $deferred->promise();
|
return $deferred->promise();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return bool True if the stream has been completed or failed.
|
||||||
|
*/
|
||||||
|
public function isComplete(): bool
|
||||||
|
{
|
||||||
|
return $this->completed;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Completes the stream.
|
* Completes the stream.
|
||||||
**
|
**
|
||||||
|
@ -89,6 +89,11 @@ trait Placeholder
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function isResolved(): bool
|
||||||
|
{
|
||||||
|
return $this->resolved;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
*
|
*
|
||||||
|
@ -10,7 +10,7 @@ namespace Amp;
|
|||||||
interface Stream
|
interface Stream
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Succeeds with the yielded value if the stream has yielded a value or null if the stream has completed.
|
* Succeeds with the emitted value if the stream has emitted a value or null if the stream has completed.
|
||||||
* If the stream fails, the returned promise will fail with the same exception.
|
* If the stream fails, the returned promise will fail with the same exception.
|
||||||
*
|
*
|
||||||
* @return Promise<mixed|null> Resolves with null if the stream has completed.
|
* @return Promise<mixed|null> Resolves with null if the stream has completed.
|
||||||
|
@ -48,6 +48,14 @@ final class StreamSource
|
|||||||
return $this->source->emit($value);
|
return $this->source->emit($value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return bool True if the stream has been completed or failed.
|
||||||
|
*/
|
||||||
|
public function isComplete(): bool
|
||||||
|
{
|
||||||
|
return $this->source->isComplete();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Completes the stream.
|
* Completes the stream.
|
||||||
*
|
*
|
||||||
|
@ -29,6 +29,8 @@ class DeferredTest extends BaseTest
|
|||||||
$value = "Resolution value";
|
$value = "Resolution value";
|
||||||
$promise = $this->deferred->promise();
|
$promise = $this->deferred->promise();
|
||||||
|
|
||||||
|
$this->assertFalse($this->deferred->isResolved());
|
||||||
|
|
||||||
$invoked = false;
|
$invoked = false;
|
||||||
$promise->onResolve(function ($exception, $value) use (&$invoked, &$result) {
|
$promise->onResolve(function ($exception, $value) use (&$invoked, &$result) {
|
||||||
$invoked = true;
|
$invoked = true;
|
||||||
@ -37,6 +39,7 @@ class DeferredTest extends BaseTest
|
|||||||
|
|
||||||
$this->deferred->resolve($value);
|
$this->deferred->resolve($value);
|
||||||
|
|
||||||
|
$this->assertTrue($this->deferred->isResolved());
|
||||||
$this->assertTrue($invoked);
|
$this->assertTrue($invoked);
|
||||||
$this->assertSame($value, $result);
|
$this->assertSame($value, $result);
|
||||||
}
|
}
|
||||||
|
@ -3,20 +3,20 @@
|
|||||||
namespace Amp\Test;
|
namespace Amp\Test;
|
||||||
|
|
||||||
use Amp\DisposedException;
|
use Amp\DisposedException;
|
||||||
use Amp\Internal\EmitSource;
|
|
||||||
use Amp\PHPUnit\AsyncTestCase;
|
use Amp\PHPUnit\AsyncTestCase;
|
||||||
use Amp\Promise;
|
use Amp\Promise;
|
||||||
|
use Amp\StreamSource;
|
||||||
use Amp\Success;
|
use Amp\Success;
|
||||||
|
|
||||||
class EmitSourceTest extends AsyncTestCase
|
class StreamSourceTest extends AsyncTestCase
|
||||||
{
|
{
|
||||||
/** @var EmitSource */
|
/** @var StreamSource */
|
||||||
private $source;
|
private $source;
|
||||||
|
|
||||||
public function setUp()
|
public function setUp()
|
||||||
{
|
{
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
$this->source = new EmitSource;
|
$this->source = new StreamSource;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testEmit()
|
public function testEmit()
|
||||||
@ -33,11 +33,30 @@ class EmitSourceTest extends AsyncTestCase
|
|||||||
$this->assertInstanceOf(Promise::class, $promise);
|
$this->assertInstanceOf(Promise::class, $promise);
|
||||||
$this->assertNull(yield $promise);
|
$this->assertNull(yield $promise);
|
||||||
|
|
||||||
|
$this->assertFalse($this->source->isComplete());
|
||||||
|
|
||||||
$this->source->complete();
|
$this->source->complete();
|
||||||
|
|
||||||
|
$this->assertTrue($this->source->isComplete());
|
||||||
|
|
||||||
$this->assertNull(yield $continue);
|
$this->assertNull(yield $continue);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testFail()
|
||||||
|
{
|
||||||
|
$this->assertFalse($this->source->isComplete());
|
||||||
|
$this->source->fail($exception = new \Exception);
|
||||||
|
$this->assertTrue($this->source->isComplete());
|
||||||
|
|
||||||
|
$stream = $this->source->stream();
|
||||||
|
|
||||||
|
try {
|
||||||
|
yield $stream->continue();
|
||||||
|
} catch (\Exception $caught) {
|
||||||
|
$this->assertSame($exception, $caught);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @depends testEmit
|
* @depends testEmit
|
||||||
*/
|
*/
|
||||||
@ -53,7 +72,7 @@ class EmitSourceTest extends AsyncTestCase
|
|||||||
/**
|
/**
|
||||||
* @depends testEmit
|
* @depends testEmit
|
||||||
*/
|
*/
|
||||||
public function testEmitingNull()
|
public function testEmittingNull()
|
||||||
{
|
{
|
||||||
$this->expectException(\TypeError::class);
|
$this->expectException(\TypeError::class);
|
||||||
$this->expectExceptionMessage('Streams cannot emit NULL');
|
$this->expectExceptionMessage('Streams cannot emit NULL');
|
||||||
@ -64,7 +83,7 @@ class EmitSourceTest extends AsyncTestCase
|
|||||||
/**
|
/**
|
||||||
* @depends testEmit
|
* @depends testEmit
|
||||||
*/
|
*/
|
||||||
public function testEmitingPromise()
|
public function testEmittingPromise()
|
||||||
{
|
{
|
||||||
$this->expectException(\TypeError::class);
|
$this->expectException(\TypeError::class);
|
||||||
$this->expectExceptionMessage('Streams cannot emit promises');
|
$this->expectExceptionMessage('Streams cannot emit promises');
|
Loading…
Reference in New Issue
Block a user