Share
Embed
Transcript
49 slides
-
proposal-async-contextとは 非同期処理をまたいで「コンテキスト」を引き継ぐ仕組みを、 JavaScript の言語標準 として入れようという提案 項目 内容 提案先・段階 TC39(JavaScript の仕様を決める委員会)で
Stage 2 = 仕様の初稿 追加される API AsyncContext.Variable(値を入れて読む) AsyncContext.Snapshot(文脈を保存して後で戻す) できること run で設定した値を、 await・Promise・setTimeout の先でも get で読める 位置づけ Node の AsyncLocalStorage と同じ考え方を、ブラウザを含むどこでも使えるようにする ※ Stage 2 のため、今後仕様が変わる可能性があります © PeopleX Inc. 3 -
何を解決するものか 何が出⼒されますか? let currentId; 出⼒ [?] 完了 [?] 完了 async
function handle(id) { currentId = id; await db.query(); console.log(`[${currentId}] 完了`); } handle("A"); handle("B"); © PeopleX Inc. 6 -
何を解決するものか 何が出⼒されますか? 出⼒ let currentId; [B] 完了 [B] 完了 async
function handle(id) { currentId = id; await db.query(); console.log(`[${currentId}] 完了`); } ↑ 1⾏⽬は A のはずが B handle("A"); handle("B"); © PeopleX Inc. 7 -
何を解決するものか 引数として渡してあげれば解決できる。けど... async function handle(id) { await getUser(id); } async
function getUser(id) { await db.query(id, "SELECT ..."); } // 渡すためだけに id を受け取り続ける © PeopleX Inc. 8 -
何を解決するものか 引数として渡してあげれば解決できる。けど... async function handle(id) { await getUser(id); } async
function getUser(id) { await db.query(id, "SELECT ..."); } // 渡すためだけに id を受け取り続ける © PeopleX Inc. 全部の関数に引数が要る 使わない関数も、下へ渡すためだけに受け取る ライブラリを越えられない npm のライブラリが間に挟まると、そこから先 へ渡せない 9 -
何を解決するものか さっきの例 AsyncContextを使⽤した例 let currentId; const requestId = new AsyncContext.Variable();
async function handle(id) { currentId = id; await db.query(); console.log(`[${currentId}] 完了`); } async function handle(id) { await requestId.run(id, async () => { await db.query(); console.log(requestId.get() + “完了”); }); } handle("A"); handle("B"); © PeopleX Inc. handle("A"); handle("B"); 11 -
何を解決するものか さっきの例 AsyncContextを使⽤した例 let currentId; const requestId = new AsyncContext.Variable();
async function handle(id) { currentId = id; await db.query(); console.log(`[${currentId}] 完了`); } async function handle(id) { await requestId.run(id, async () => { await db.query(); console.log(requestId.get() + “完了”); }); } handle("A"); handle("B"); 出⼒ [B] 完了 [B] 完了 © PeopleX Inc. handle("A"); handle("B"); 出⼒ [A] 完了 [B] 完了 12 -
何を解決するものか 今の回避策は、環境ごとにバラバラ 環境 仕組み 限界 サーバー側 (Node‧Deno‧Bun) AsyncLocalStorage Node の独⾃
API(Deno‧Bun は互換実 装)。ブラウザには無い ブラウザ Zone.js ネイティブの await を追えない (ES2015 への変換が要る) Zone.js は新機能の受け付けを停⽌ ブラウザは Zone.js くらいしかない Angular も v21 から、Zone.js を使わない⽅式 (zoneless)がデフォルトに OpenTelemetry もブラウザ⽤に ZoneContextManager を⽤意している © PeopleX Inc. 13 -
AsyncContextのAPIについて クラス API 役割 new Variable() AsyncContext.Variable run(値, fn) 1つの値を⼊れて、読む
get() new Snapshot() AsyncContext.Snapshot snapshot.run(fn) Snapshot.wrap(fn) © PeopleX Inc. その時点の全 Variable の値を まとめて取っておき、後で戻す 16 -
AsyncContextのAPIについて AsyncContext.Variable 出⼒ const requestId = new AsyncContext.Variable(); async function
handle(id) { await requestId.run(id, async () => { await db.query(); console.log(requestId.get() + “完了”); }); } A 完了 B 完了 handle("A"); handle("B"); © PeopleX Inc. 17 -
AsyncContextのAPIについて AsyncContext.Variable 出⼒ const requestId = new AsyncContext.Variable(); async function
handle(id) { await requestId.run(id, async () => { await db.query(); console.log(requestId.get() + “完了”); }); } A 完了 B 完了 ① AsyncContext.Variable のObjectを⽣成する handle("A"); handle("B"); © PeopleX Inc. 18 -
AsyncContextのAPIについて AsyncContext.Variable 出⼒ const requestId = new AsyncContext.Variable(); async function
handle(id) { await requestId.run(id, async () => { await db.query(); console.log(requestId.get() + “完了”); }); } handle("A"); handle("B"); © PeopleX Inc. A 完了 B 完了 ① AsyncContext.Variable のObjectを⽣成する ② run メソッドを使⽤し、第1引数に値を指定する 19 -
AsyncContextのAPIについて AsyncContext.Variable 出⼒ const requestId = new AsyncContext.Variable(); async function
handle(id) { await requestId.run(id, async () => { await db.query(); console.log(requestId.get() + “完了”); }); } handle("A"); handle("B"); © PeopleX Inc. A 完了 B 完了 ① AsyncContext.Variable のObjectを⽣成する ② run メソッドを使⽤し、第1引数に値を指定する ③ callbackの中で requestId.get() を呼ぶと、 第1引数で指定した値を取得する 20 -
AsyncContextのAPIについて AsyncContext.Variable 出⼒ const requestId = new AsyncContext.Variable(); async function
handle(id) { await requestId.run(id, async () => { await db.query(); console.log(requestId.get() + “完了”); }); } handle("A"); handle("B"); A 完了 B 完了 ① AsyncContext.Variable のObjectを⽣成する ② run メソッドを使⽤し、第1引数に値を指定する ③ callbackの中で requestId.get() を呼ぶと、 第1引数で指定した値を取得する requestId.get()は runメソッドの中だけで有効 © PeopleX Inc. 21 -
AsyncContextのAPIについて AsyncContext.Variable 出⼒ const requestId = new AsyncContext.Variable(); async function
handle(id) { await requestId.run(id, async () => { await db.query(); console.log(requestId.get() + “完了”); }); } handle("A"); handle("B"); A 完了 B 完了 ① AsyncContext.Variable のObjectを⽣成する ② run メソッドを使⽤し、第1引数に値を指定する ③ callbackの中で requestId.get() を呼ぶと、 第1引数で指定した値を取得する requestId.get()は runメソッドの中だけで有効 © PeopleX Inc. 22 -
AsyncContextのAPIについて 何が出⼒されるでしょう? const requestId = new AsyncContext.Variable(); const queue =
[]; const log = () => console.log(requestId.get() + "完了"); 出⼒ ? 完了 ? 完了 requestId.run("A", () => queue.push(log)); requestId.run("B", () => queue.push(log)); for (const fn of queue) fn(); © PeopleX Inc. // あとでまとめて実行 25 -
AsyncContextのAPIについて 何が出⼒されるでしょう? const requestId = new AsyncContext.Variable(); const queue =
[]; const log = () => console.log(requestId.get() + "完了"); 出⼒ undefined 完了 undefined 完了 requestId.run("A", () => queue.push(log)); requestId.run("B", () => queue.push(log)); for (const fn of queue) fn(); © PeopleX Inc. // あとでまとめて実行 26 -
AsyncContextのAPIについて 何が出⼒されるでしょう? const requestId = new AsyncContext.Variable(); const queue =
[]; const log = () => console.log(requestId.get() + "完了"); requestId.run("A", () => queue.push(log)); requestId.run("B", () => queue.push(log)); for (const fn of queue) fn(); © PeopleX Inc. 出⼒ undefined 完了 undefined 完了 どこで呼ばれる??? // あとでまとめて実行 27 -
AsyncContextのAPIについて 何が出⼒されるでしょう? const requestId = new AsyncContext.Variable(); const queue =
[]; const log = () => console.log(requestId.get() + "完了"); requestId.run("A", () => queue.push(log)); requestId.run("B", () => queue.push(log)); for (const fn of queue) fn(); © PeopleX Inc. // あとでまとめて実行 出⼒ undefined 完了 undefined 完了 どこで呼ばれる??? ① requestId.runの中で関数はプッシュされてい るが... 28 -
AsyncContextのAPIについて 何が出⼒されるでしょう? const requestId = new AsyncContext.Variable(); const queue =
[]; const log = () => console.log(requestId.get() + "完了"); requestId.run("A", () => queue.push(log)); requestId.run("B", () => queue.push(log)); for (const fn of queue) fn(); // あとでまとめて実行 出⼒ undefined 完了 undefined 完了 どこで呼ばれる??? ① requestId.runの中で関数はプッシュされてい るが... ② 関数が実⾏されているのはここ © PeopleX Inc. 29 -
AsyncContextのAPIについて 何が出⼒されるでしょう? 出⼒ const requestId = new AsyncContext.Variable(); const queue
= []; const log = () => console.log(requestId.get() + "完了"); requestId.run("A", () => queue.push(log)); requestId.run("B", () => queue.push(log)); for (const fn of queue) fn(); // あとでまとめて実行 undefined 完了 undefined 完了 どこで呼ばれる??? ① requestId.runの中で関数はプッシュされてい るが... ② 関数が実⾏されているのはここ すなわち、requestId.run()の外でrequestId.get()は解決される © PeopleX Inc. 30 -
AsyncContextのAPIについて AsyncContext.Snapshot.wrap 出⼒ const log = () => console.log(requestId.get() +
"完了"); requestId.run("A", () => queue.push(AsyncContext.Snapshot.wrap(log))); requestId.run("B", () => queue.push(AsyncContext.Snapshot.wrap(log))); for (const fn of queue) fn(); © PeopleX Inc. A 完了 B 完了 defaultValueにならず解決する 32 -
AsyncContextのAPIについて AsyncContext.Snapshot.wrap 出⼒ const log = () => console.log(requestId.get() +
"完了"); requestId.run("A", () => queue.push(AsyncContext.Snapshot.wrap(log))); requestId.run("B", () => queue.push(AsyncContext.Snapshot.wrap(log))); A 完了 B 完了 for (const fn of queue) fn(); ① requestId.runの中で呼ぶ © PeopleX Inc. 33 -
AsyncContextのAPIについて AsyncContext.Snapshot.wrap 出⼒ const log = () => console.log(requestId.get() +
"完了"); requestId.run("A", () => queue.push(AsyncContext.Snapshot.wrap(log))); requestId.run("B", () => queue.push(AsyncContext.Snapshot.wrap(log))); A 完了 B 完了 for (const fn of queue) fn(); ① requestId.runの中で呼ぶ ② requestId.runの外でrequestId.get()を呼ぶ © PeopleX Inc. 34 -
AsyncContextのAPIについて AsyncContext.Snapshot.wrap 出⼒ const log = () => console.log(requestId.get() +
"完了"); requestId.run("A", () => queue.push(AsyncContext.Snapshot.wrap(log))); requestId.run("B", () => queue.push(AsyncContext.Snapshot.wrap(log))); for (const fn of queue) fn(); A 完了 B 完了 ③ ①で呼んだときのコンテキストで requestId.get()が解決する ① requestId.runの中で呼ぶ ② requestId.runの外でrequestId.get()を呼ぶ © PeopleX Inc. 35 -
AsyncContextのAPIについて AsyncContext.Snapshot.wrap 出⼒ const log = () => console.log(requestId.get() +
"完了"); requestId.run("A", () => queue.push(AsyncContext.Snapshot.wrap(log))); requestId.run("B", () => queue.push(AsyncContext.Snapshot.wrap(log))); for (const fn of queue) fn(); A 完了 B 完了 ③ ①で呼んだときのコンテキストで requestId.get()が解決する ① requestId.runの中で呼ぶ ② requestId.runの外でrequestId.get()を呼ぶ © PeopleX Inc. 38 -
AsyncContextのAPIについて AsyncContext.Snapshot.wrap 出⼒ const log = () => console.log(requestId.get() +
"完了"); requestId.run("A", () => queue.push(AsyncContext.Snapshot.wrap(log))); requestId.run("B", () => queue.push(AsyncContext.Snapshot.wrap(log))); for (const fn of queue) fn(); A 完了 B 完了 ③ ①で呼んだときのコンテキストで requestId.get()が解決する ① requestId.runの中で呼ぶ ② requestId.runの外でrequestId.get()を呼ぶ 「ためて、あとでまとめて実⾏する」ユースケースなど 例:まとめて書き込む(バッチ)‧接続待ちの列‧イベント通知 © PeopleX Inc. 39 -
どのように活⽤できるか AsyncContext.Variableの例: 分散トレーシング // ① トレースライブラリ( npm) const currentTrace =
new AsyncContext.Variable(); export function startTrace(fn) { return currentTrace.run(newId(), fn); } export function tracedFetch(url, opts){ const traceId = currentTrace.get(); return fetch(url, { ...opts, headers: { "x-trace-id": traceId }}); } // ② アプリ button.onclick = () => tracing.startTrace(async () => { await validateForm(); await tracing.tracedFetch("/api/orders", { method: "POST", body, }); }); © PeopleX Inc. 41 -
どのように活⽤できるか AsyncContext.Variableの例: 分散トレーシング // ① トレースライブラリ( npm) const currentTrace =
new AsyncContext.Variable(); export function startTrace(fn) { ライブラリの中にAsyncContextは閉じ 込める return currentTrace.run(newId(), fn); } export function tracedFetch(url, opts){ const traceId = currentTrace.get(); return fetch(url, { ...opts, headers: { "x-trace-id": traceId }}); } // ② アプリ button.onclick = () => tracing.startTrace(async () => { await validateForm(); await tracing.tracedFetch("/api/orders", { method: "POST", body, }); }); © PeopleX Inc. 42 -
どのように活⽤できるか AsyncContext.Variableの例: 分散トレーシング // ① トレースライブラリ( npm) const currentTrace =
new AsyncContext.Variable(); export function startTrace(fn) { ライブラリの中にAsyncContextは閉じ 込める return currentTrace.run(newId(), fn); } export function tracedFetch(url, opts){ const traceId = currentTrace.get(); return fetch(url, { ...opts, headers: { "x-trace-id": traceId }}); } // ② アプリ button.onclick = () => tracing.startTrace(async () => { await validateForm(); アプリ側ではライブラリの関数を使う だけ await tracing.tracedFetch("/api/orders", { method: "POST", body, }); }); © PeopleX Inc. 43 -
どのように活⽤できるか AsyncContext.Variableの例: 分散トレーシング // ① トレースライブラリ( npm) const currentTrace =
new AsyncContext.Variable(); export function startTrace(fn) { ライブラリの中にAsyncContextは閉じ 込める return currentTrace.run(newId(), fn); } export function tracedFetch(url, opts){ const traceId = currentTrace.get(); return fetch(url, { ...opts, headers: { "x-trace-id": traceId }}); } // ② アプリ button.onclick = () => tracing.startTrace(async () => { await validateForm(); await tracing.tracedFetch("/api/orders", { method: "POST", body, }); }); © PeopleX Inc. アプリ側ではライブラリの関数を使う だけ ライブラリに抽出することで 「関⼼の分離」がしやすくなる (これまでは引数渡しするしかな かった) 44 -
どのように活⽤できるか AsyncContext.Snapshotの例: Batch処理 (時間ある⼈は⾒てみてください...) // ① ライブラリ : 保存依頼をためて、 10msごとに
1回書く wrap なし const pending = []; [undefined] 保存完了 export function save(row, onDone) { [undefined] 保存完了 pending.push({ row, onDone: AsyncContext.Snapshot.wrap(onDone) }); // [undefined] 保存完了 ← ためるときに文脈ごと包む } setInterval(async () => { const batch = pending.splice(0); await db.insertMany(batch.map(p => p.row)); for (const p of batch) p.onDone(); }, 10); wrap あり [req-1] 保存完了 [req-2] 保存完了 [req-3] 保存完了 // ② アプリ const log = (msg) => console.log(`[${requestId.get()}] ${msg}`); requestId.run("req-1", () => save(row, () => log("保存完了"))); // req-2・req-3 も同じように © PeopleX Inc. 45 -
プロポーザルの状況 2026-11-17 時点 状況 TC39(JavaScript の仕様) Stage 2。⾔語側はほぼ固まった(5⽉の会議で「TC39 側の変更はない」) WHATWG(HTML‧DOM)
Stage 1。11/12 に Stage 2 を提案 → 持ち越し(11⽉下旬に再提案) ブラウザの何百もの⾮同期 API にどう組み込むか © PeopleX Inc. 47 -
まとめ 問題: await をまたぐと、共有した変数は別の処理に上書きされる。引数 リレーは全関数とライブラリに負担をかける。 解決: Variable の run /
get で値が届く。ためて後で実⾏するときだけ Snapshot.wrap。関⼼の分離がしやすくなる。 現在の状況: TC39 Stage2。仕様確定しているわけではない。 © PeopleX Inc. 49