| 21 | const asyncLocalStorage = new AsyncLocalStorage<Zone | undefined>(); |
| 22 | |
| 23 | export class Zone { |
| 24 | private readonly _asyncLocalStorage: AsyncLocalStorage<Zone | undefined>; |
| 25 | private readonly _data: ReadonlyMap<ZoneType, unknown>; |
| 26 | |
| 27 | constructor(asyncLocalStorage: AsyncLocalStorage<Zone | undefined>, store: Map<ZoneType, unknown>) { |
| 28 | this._asyncLocalStorage = asyncLocalStorage; |
| 29 | this._data = store; |
| 30 | } |
| 31 | |
| 32 | with(type: ZoneType, data: unknown): Zone { |
| 33 | return new Zone(this._asyncLocalStorage, new Map(this._data).set(type, data)); |
| 34 | } |
| 35 | |
| 36 | without(type?: ZoneType): Zone { |
| 37 | const data = type ? new Map(this._data) : new Map(); |
| 38 | data.delete(type); |
| 39 | return new Zone(this._asyncLocalStorage, data); |
| 40 | } |
| 41 | |
| 42 | run<R>(func: () => R): R { |
| 43 | return this._asyncLocalStorage.run(this, func); |
| 44 | } |
| 45 | |
| 46 | data<T>(type: ZoneType): T | undefined { |
| 47 | return this._data.get(type) as T | undefined; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | export const emptyZone = new Zone(asyncLocalStorage, new Map()); |
| 52 |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…