shared-helper-callback-binding.ts 1.2 KB

12345678910111213141516171819202122
  1. // A rate-limit helper shared by five call sites: two fan-outs, a top-level call, and a
  2. // call whose callback calls the helper again. `fn()` inside the helper inlines ONLY the
  3. // callback the call being inlined passed (docs/analysis.md "Inlining"): no exhaustive
  4. // choice over every caller's callback, no false `loop recursive` around the helper, and
  5. // each ask's `within` names its own fan-out — ask#1 fan-out#1, ask#2 fan-out#2, the rest
  6. // none. The nested use is two activations with different bindings, not recursion.
  7. async function limited<T>(slots: number, fn: () => Promise<T>): Promise<T> {
  8. return await fn();
  9. }
  10. const xs0 = ["a", "b"];
  11. const xs1 = ["c", "d"];
  12. const out: string[] = [];
  13. out.push(...(await Promise.all(xs0.map(async (x) => limited(2, async () => agent(`f0-${x}`).ask<string>(`do ${x}`))))));
  14. out.push(...(await Promise.all(xs1.map(async (x) => limited(2, async () => agent(`f1-${x}`).ask<string>(`do ${x}`))))));
  15. out.push(await limited(2, async () => agent("m0").ask<string>("top 0")));
  16. out.push(
  17. await limited(2, async () => {
  18. const inner = await limited(2, async () => agent("n1").ask<string>("nested"));
  19. return agent("m1").ask<string>(`after ${inner}`);
  20. }),
  21. );
  22. return out;