shajara图解Scope 管理的对象
中文

Scope 管理的对象

scope 管理 future 与 channel 生命周期

branch(openSession)branch waits for openSessionticket, updateswait(ticket)tryReceive(updates)resumeCheckoutpendingupdatesopenticketopenSessionpending
function* resumeCheckout() {
  const [ticket, updates] = yield* branch(function* openSession() {
    const [ticket] = yield* future<string>();
    const [updates] = yield* channel<string, never>(0);
    yield* sleep(1000);
    return [ticket, updates];
  });
  yield* sleep(1000);

  try { yield* wait(ticket); }
  catch (error) { observeCanceled(error); }
  yield* sleep(1000);
  try { yield* tryReceive(updates); }
  catch (error) { observeRevoked(error); }

  return "owned objects closed";
}
openSession 在 child scope 内创建 ticket 和 updates。
resumeCheckout 仍能使用 ticket 和 updates,但运行对象仍由创建它们的 scope 拥有。
openSession 的 child scope 收束时,会取消未完成的 future,并撤销仍打开的 channel。
后续 wait 和 tryReceive 只是观察这些终止状态。