|
1 | 1 | # async-context-tc39 |
2 | | -TC39 proposal implementation for AsyncContext |
| 2 | +[](https://github.com/artelk/async-context-tc39/actions?query=workflow%3ACI) |
| 3 | +[](https://www.npmjs.com/package/async-context-tc39) |
| 4 | + |
| 5 | +TC39 proposal implementation for AsyncContext. |
| 6 | + |
| 7 | +## Summary |
| 8 | + |
| 9 | +This is an implementation of the proposal https://github.com/tc39/proposal-async-context that works with JavaScript native `await`s in browser. |
| 10 | +All the `await`s on the path between where the context value is set and where it is used should be wrapped into Ѧ-functions to capture and restore the current context |
| 11 | +(that is required because the `await`s use a native mechanism that is not interceptable/hookable at all). |
| 12 | +Use eslint-plugin-async-context-tc39 that will search for all the `await` expressions not wrapped into the Ѧ-functions in your project and fix them. |
| 13 | + |
| 14 | +Most of the tests were copied from the project https://github.com/iliasbhal/simple-async-context (the TC39 implementation that requires the code to be built to ES6). |
| 15 | + |
| 16 | +## Install |
| 17 | + |
| 18 | +```bash |
| 19 | +npm install async-context-tc39 |
| 20 | +``` |
| 21 | + |
| 22 | +## Usage: |
| 23 | + |
| 24 | +```ts |
| 25 | +import { AsyncContext } from 'async-context-tc39'; |
| 26 | + |
| 27 | +const asyncContext = new AsyncContext.Variable<string>(); |
| 28 | + |
| 29 | +//... |
| 30 | + it("test", async () => { |
| 31 | + const deepCallback = async () => { |
| 32 | + expect(asyncContext.get()).toBe("Inner"); |
| 33 | + Ѧ(await wait(30).Ѧ); |
| 34 | + expect(asyncContext.get()).toBe("Inner"); |
| 35 | + }; |
| 36 | + |
| 37 | + const innerCallback = () => asyncContext.run("Inner", async () => { |
| 38 | + expect(asyncContext.get()).toBe("Inner"); |
| 39 | + Ѧ(await wait(30).Ѧ); |
| 40 | + expect(asyncContext.get()).toBe("Inner"); |
| 41 | + Ѧ(await deepCallback().Ѧ); |
| 42 | + expect(asyncContext.get()).toBe("Inner"); |
| 43 | + Ѧ(await wait(30).Ѧ); |
| 44 | + expect(asyncContext.get()).toBe("Inner"); |
| 45 | + }); |
| 46 | + |
| 47 | + const total = () => asyncContext.run("Outer", async () => { |
| 48 | + expect(asyncContext.get()).toBe("Outer"); |
| 49 | + Ѧ(await innerCallback().Ѧ); |
| 50 | + expect(asyncContext.get()).toBe("Outer"); |
| 51 | + Ѧ(await innerCallback().Ѧ); |
| 52 | + expect(asyncContext.get()).toBe("Outer"); |
| 53 | + Ѧ(await innerCallback().Ѧ); |
| 54 | + expect(asyncContext.get()).toBe("Outer"); |
| 55 | + }); |
| 56 | + |
| 57 | + expect(asyncContext.get()).toBe(undefined); |
| 58 | + Ѧ(await total().Ѧ); |
| 59 | + expect(asyncContext.get()).toBe(undefined); |
| 60 | + }); |
| 61 | +``` |
| 62 | + |
| 63 | +## Classes |
| 64 | + |
| 65 | +```ts |
| 66 | +namespace AsyncContext { |
| 67 | + class Variable<T> { |
| 68 | + constructor(options: AsyncVariableOptions<T>); |
| 69 | + get name(): string; |
| 70 | + get(): T | undefined; |
| 71 | + run<R>(value: T, fn: (...args: any[])=> R, ...args: any[]): R; |
| 72 | + wrap<Fn extends (...args: any) => any>(value: T, callback: Fn): Fn; |
| 73 | + } |
| 74 | + |
| 75 | + interface AsyncVariableOptions<T> { |
| 76 | + name?: string; |
| 77 | + defaultValue?: T; |
| 78 | + } |
| 79 | + |
| 80 | + class Snapshot { |
| 81 | + constructor(); |
| 82 | + run<R>(fn: (...args: any[]) => R, ...args: any[]): R; |
| 83 | + static wrap<T, R>(fn: (this: T, ...args: any[]) => R): (this: T, ...args: any[]) => R; |
| 84 | + } |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | +See README.md on https://github.com/tc39/proposal-async-context for datails |
0 commit comments