shajaraExplorerBounded Channel
English

Bounded Channel

channel pacing for send and receive

spawn(writeBatches)send enters channelreceive takes valuequeueBatchespendingbatchQueue0/1writeBatchespending
function* queueBatches() {
  const [receiver, sender] = yield* channel<string, never>(1);

  yield* spawn(function* writeBatches() {
    const first = yield* receive(receiver);
    yield* sleep(1000);
    const second = yield* receive(receiver);
    const third = yield* receive(receiver);
    const fourth = yield* receive(receiver);
    return [first, second, third, fourth];
  });

  yield* send(sender, "draft");
  yield* send(sender, "review");
  yield* send(sender, "publish");
  yield* sleep(1000);
  yield* send(sender, "archive");

  return 4;
}
queueBatches creates a capacity-one channel shared with writeBatches.
queueBatches sends draft into the buffer, and writeBatches receives it without waiting.
The review and publish sends reach a full channel and wait for writeBatches.
writeBatches later reaches an empty channel and waits for archive.