1
0
mirror of https://github.com/danog/amp.git synced 2024-12-13 18:07:30 +01:00
amp/lib/Stream/ToArrayOperator.php

42 lines
735 B
PHP
Raw Normal View History

2020-05-16 17:39:34 +02:00
<?php
namespace Amp\Stream;
use Amp\Promise;
use Amp\Stream;
use function Amp\call;
/**
* @template TValue
*/
final class ToArrayOperator
{
/** @var Promise<list<TValue>> */
private $promise;
/**
* @param Stream<TValue> $stream
*/
public function __construct(Stream $stream)
{
$this->promise = call(function () use ($stream) {
/** @psalm-var list $array */
$array = [];
while (list($value) = yield $stream->continue()) {
$array[] = $value;
}
return $array;
});
}
/**
* @return Promise<list<TValue>>
*/
public function promise(): Promise
{
return $this->promise;
}
}