Menu
Reference
- Manticore\Ds
- Async
- Classes
- AggregateError
- CancellationToken
- CancelledException
- Channel
- ChannelClosedException
- Context
- DeadlockException
- Mutex
- Once
- Pool
- Received
- SelectCase
- Selected
- Semaphore
- Settled
- Task
- TaskGroup
- TimeoutException
- Functions
- async()
- awaitAll()
- awaitAllSettled()
- awaitAny()
- channel()
- delay()
- dump()
- dumpOn()
- failure()
- group()
- mapConcurrent()
- mapSettled()
- readFile()
- select()
- selectNow()
- selectWithin()
- shield()
- shutdownOn()
- spawn()
- stats()
- timeout()
- watchdog()
- writeFile()
- Http
- Http\WebSocket
- Buffer
- Process
- Io\Poll
- Manticore\Sapi
- Ffi
Received
finalclass
final class Received
One receive from a channel, as an object.
Go returns v, ok. PHP has no multi-return, and collapsing it to "null means closed" would make null an illegal payload, so Channel::next() returns this instead.
$r = $ch->next();
if (!$r->ok) { break; } // closed and drained
echo $r->value;Example
use Async\ChannelClosedException;
use function Async\async;
use function Async\channel;
use function Async\spawn;
async(function () {
// Capacity 0 is a rendezvous: send() waits for a receiver.
$ch = channel();
spawn(function () use ($ch) {
foreach ([1, 2, 3] as $n) {
$ch->send($n);
}
$ch->close();
});
// foreach ends once the channel is closed and drained.
foreach ($ch as $value) {
echo "got $value\n";
}
// next() is the explicit form: ok is false on a closed channel,
// which lets null be a legitimate payload.
$r = $ch->next();
echo $r->ok ? "value\n" : "closed\n";
try {
$ch->send(4);
} catch (ChannelClosedException $e) {
echo "send failed: ", $e->getMessage(), "\n";
}
// A buffered channel takes values without a waiting receiver.
$buf = channel(2);
$buf->send("a");
$buf->send("b");
echo $buf->recv(), $buf->recv(), "\n";
});Output
got 1
got 2
got 3
closed
send failed: send on a closed channel
abSee also: Channel::next
Methods
__construct() | Build a receive result. |
__construct()#source
function __construct(public mixed $value, public bool $ok)
Build a receive result.
Parameters
$valuemixed- the received value; null when the channel was closed
$okbool- false when the channel was closed and drained instead of delivering a value