Files
node/lib/internal/async_local_storage/run_scope.js
Stephen Belanger 5b6091ce31 async_hooks: add using scopes to AsyncLocalStorage
Adds support for using scope = storage.withScope(data) to do
the equivalent of a storage.run(data, fn) with using syntax.
This enables avoiding unnecessary closures.

PR-URL: https://github.com/nodejs/node/pull/61674
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
Reviewed-By: Bryan English <bryan@bryanenglish.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
2026-03-28 20:53:57 +00:00

32 lines
487 B
JavaScript

'use strict';
const {
SymbolDispose,
} = primordials;
class RunScope {
#storage;
#previousStore;
#disposed = false;
constructor(storage, store) {
this.#storage = storage;
this.#previousStore = storage.getStore();
storage.enterWith(store);
}
dispose() {
if (this.#disposed) {
return;
}
this.#disposed = true;
this.#storage.enterWith(this.#previousStore);
}
[SymbolDispose]() {
this.dispose();
}
}
module.exports = RunScope;