-
-
Notifications
You must be signed in to change notification settings - Fork 69
/
BoundedChannelState.php
182 lines (152 loc) · 3.79 KB
/
BoundedChannelState.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
declare(strict_types=1);
namespace Psl\Channel\Internal;
use Psl\Channel\ChannelInterface;
use Psl\Channel\Exception;
use Revolt\EventLoop\Suspension;
use function array_shift;
/**
* @template T
*
* @implements ChannelInterface<T>
*
* @internal
*
* @psalm-suppress LessSpecificReturnStatement
* @psalm-suppress MoreSpecificReturnType
*/
final class BoundedChannelState implements ChannelInterface
{
/**
* @var list<Suspension<mixed>>
*/
private array $waitingForMessage = [];
/**
* @var list<Suspension<mixed>>
*/
private array $waitingForSpace = [];
/**
* @var array<array-key, T>
*/
private array $messages = [];
private int $size = 0;
public bool $closed = false;
/**
* @param positive-int $capacity
*/
public function __construct(
private int $capacity
) {
}
public function __destruct()
{
$this->close();
}
/**
* @param Suspension<mixed> $suspension
*/
public function waitForSpace(Suspension $suspension): void
{
$this->waitingForSpace[] = $suspension;
}
/**
* @param Suspension<mixed> $suspension
*/
public function waitForMessage(Suspension $suspension): void
{
$this->waitingForMessage[] = $suspension;
}
/**
* {@inheritDoc}
*/
public function getCapacity(): int
{
return $this->capacity;
}
/**
* {@inheritDoc}
*/
public function close(): void
{
$this->closed = true;
$suspensions = $this->waitingForSpace;
$this->waitingForSpace = [];
foreach ($suspensions as $suspension) {
$suspension->throw(Exception\ClosedChannelException::forSending());
}
$suspensions = $this->waitingForMessage;
$this->waitingForMessage = [];
foreach ($suspensions as $suspension) {
$suspension->throw(Exception\ClosedChannelException::forReceiving());
}
}
/**
* {@inheritDoc}
*/
public function isClosed(): bool
{
return $this->closed;
}
/**
* {@inheritDoc}
*/
public function count(): int
{
return $this->size;
}
/**
* {@inheritDoc}
*/
public function isFull(): bool
{
return $this->capacity === $this->size;
}
/**
* {@inheritDoc}
*/
public function isEmpty(): bool
{
return !$this->messages;
}
/**
* @param T $message
*
* @throws Exception\ClosedChannelException If the channel is closed.
* @throws Exception\FullChannelException If the channel is full.
*/
public function send(mixed $message): void
{
if ($this->closed) {
throw Exception\ClosedChannelException::forSending();
}
if ($this->capacity === $this->size) {
throw Exception\FullChannelException::ofCapacity($this->capacity);
}
$this->messages[] = $message;
$this->size++;
if ($suspension = array_shift($this->waitingForMessage)) {
$suspension->resume(null);
}
}
/**
* @throws Exception\ClosedChannelException If the channel is closed, and there's no more messages to receive.
* @throws Exception\EmptyChannelException If the channel is empty.
*
* @return T
*/
public function receive(): mixed
{
if (!$this->messages) {
if ($this->closed) {
throw Exception\ClosedChannelException::forReceiving();
}
throw Exception\EmptyChannelException::create();
}
$item = array_shift($this->messages);
$this->size--;
if ($suspension = array_shift($this->waitingForSpace)) {
$suspension->resume(null);
}
return $item;
}
}