-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathWritableIterableStream.php
95 lines (75 loc) · 2.13 KB
/
WritableIterableStream.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php declare(strict_types=1);
namespace Amp\ByteStream;
use Amp\DeferredFuture;
use Amp\ForbidCloning;
use Amp\ForbidSerialization;
use Amp\Pipeline\Queue;
/**
* @template-implements \IteratorAggregate<int, string>
*/
final class WritableIterableStream implements WritableStream, \IteratorAggregate
{
use ForbidCloning;
use ForbidSerialization;
private readonly Queue $queue;
/** @var \Traversable<int, string> */
private readonly iterable $iterable;
private int $bufferSize;
private readonly DeferredFuture $onClose;
public function __construct(int $bufferSize)
{
$this->queue = new Queue;
$this->iterable = $this->queue->iterate();
$this->bufferSize = $bufferSize;
$this->onClose = new DeferredFuture;
}
public function __destruct()
{
$this->close();
}
public function close(): void
{
if (!$this->queue->isComplete()) {
$this->queue->complete();
}
if ($this->onClose->isComplete()) {
$this->onClose->complete();
}
}
public function isClosed(): bool
{
return !$this->isWritable();
}
public function onClose(\Closure $onClose): void
{
$this->onClose->getFuture()->finally($onClose);
}
public function write(string $bytes): void
{
if ($this->queue->isComplete() || $this->queue->isDisposed()) {
throw new ClosedException('The stream is no longer writable');
}
$length = \strlen($bytes);
$this->bufferSize -= $length;
$future = $this->queue->pushAsync($bytes)->finally(fn () => $this->bufferSize += $length);
if ($this->bufferSize < 0) {
$future->await();
} else {
$future->ignore();
}
}
public function end(): void
{
if (!$this->queue->isComplete()) {
$this->queue->complete();
}
}
public function isWritable(): bool
{
return !$this->queue->isComplete() && !$this->queue->isDisposed();
}
public function getIterator(): \Traversable
{
return $this->iterable;
}
}