shajara图解有界 Channel
中文

有界 Channel

send 与 receive 的 channel 节奏

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 创建容量为一的 channel,并把它交给 writeBatches 共享。
queueBatches 把 draft 发送进缓冲,writeBatches 不需要等待就能接收。
review 和 publish 两次 send 抵达已满的 channel,并等待 writeBatches。
writeBatches 后续抵达空 channel,并等待 archive。