Created
March 19, 2026 00:58
-
-
Save 3p3r/540ed2321443a04f44d5165ecfb04ec9 to your computer and use it in GitHub Desktop.
AsyncLocalStorage mock
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export class AsyncLocalStorage<T = any> { | |
| private store: T | undefined; | |
| getStore(): T | undefined { | |
| return this.store; | |
| } | |
| run<R>(store: T, callback: (...args: any[]) => R, ...args: any[]): R { | |
| const previousStore = this.store; | |
| this.store = store; | |
| try { | |
| return callback(...args); | |
| } finally { | |
| this.store = previousStore; | |
| } | |
| } | |
| enterWith(store: T): void { | |
| this.store = store; | |
| } | |
| exit<R>(callback: (...args: any[]) => R, ...args: any[]): R { | |
| const previousStore = this.store; | |
| this.store = undefined; | |
| try { | |
| return callback(...args); | |
| } finally { | |
| this.store = previousStore; | |
| } | |
| } | |
| disable(): void { | |
| this.store = undefined; | |
| } | |
| } | |
| export default { AsyncLocalStorage }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment