@@ -19,54 +19,54 @@ import { AsyncLocalStorage } from 'async_hooks';
1919export type ZoneType = 'apiZone' | 'expectZone' | 'stepZone' ;
2020
2121class ZoneManager {
22- private readonly _asyncLocalStorage = new AsyncLocalStorage < Zone | undefined > ( ) ;
22+ private readonly _asyncLocalStorage = new AsyncLocalStorage < Zone | undefined > ( ) ;
23+ private readonly _emptyZone = Zone . createEmpty ( this . _asyncLocalStorage ) ;
2324
2425 run < T , R > ( type : ZoneType , data : T , func : ( ) => R ) : R {
25- const zone = Zone . _createWithData ( this . _asyncLocalStorage , type , data ) ;
26- return this . _asyncLocalStorage . run ( zone , func ) ;
26+ return this . current ( ) . with ( type , data ) . run ( func ) ;
2727 }
2828
2929 zoneData < T > ( type : ZoneType ) : T | undefined {
30- const zone = this . _asyncLocalStorage . getStore ( ) ;
31- return zone ?. get ( type ) ;
30+ return this . current ( ) . data ( type ) ;
3231 }
3332
34- currentZone ( ) : Zone {
35- return this . _asyncLocalStorage . getStore ( ) ?? Zone . _createEmpty ( this . _asyncLocalStorage ) ;
33+ current ( ) : Zone {
34+ return this . _asyncLocalStorage . getStore ( ) ?? this . _emptyZone ;
3635 }
3736
38- exitZones < R > ( func : ( ) => R ) : R {
39- return this . _asyncLocalStorage . run ( undefined , func ) ;
37+ empty ( ) : Zone {
38+ return this . _emptyZone ;
4039 }
4140}
4241
4342export class Zone {
4443 private readonly _asyncLocalStorage : AsyncLocalStorage < Zone | undefined > ;
45- private readonly _data : Map < ZoneType , unknown > ;
44+ private readonly _data : ReadonlyMap < ZoneType , unknown > ;
4645
47- static _createWithData ( asyncLocalStorage : AsyncLocalStorage < Zone | undefined > , type : ZoneType , data : unknown ) {
48- const store = new Map ( asyncLocalStorage . getStore ( ) ?. _data ) ;
49- store . set ( type , data ) ;
50- return new Zone ( asyncLocalStorage , store ) ;
51- }
52-
53- static _createEmpty ( asyncLocalStorage : AsyncLocalStorage < Zone | undefined > ) {
46+ static createEmpty ( asyncLocalStorage : AsyncLocalStorage < Zone | undefined > ) {
5447 return new Zone ( asyncLocalStorage , new Map ( ) ) ;
5548 }
5649
57- private constructor ( asyncLocalStorage : AsyncLocalStorage < Zone | undefined > , store : Map < ZoneType , unknown > ) {
50+ private constructor ( asyncLocalStorage : AsyncLocalStorage < Zone | undefined > , store : Map < ZoneType , unknown > ) {
5851 this . _asyncLocalStorage = asyncLocalStorage ;
5952 this . _data = store ;
6053 }
6154
55+ with ( type : ZoneType , data : unknown ) : Zone {
56+ return new Zone ( this . _asyncLocalStorage , new Map ( this . _data ) . set ( type , data ) ) ;
57+ }
58+
59+ without ( type ?: ZoneType ) : Zone {
60+ const data = type ? new Map ( this . _data ) : new Map ( ) ;
61+ data . delete ( type ) ;
62+ return new Zone ( this . _asyncLocalStorage , data ) ;
63+ }
64+
6265 run < R > ( func : ( ) => R ) : R {
63- // Reset apiZone and expectZone, but restore stepZone.
64- const entries = [ ...this . _data . entries ( ) ] . filter ( ( [ type ] ) => ( type !== 'apiZone' && type !== 'expectZone' ) ) ;
65- const resetZone = new Zone ( this . _asyncLocalStorage , new Map ( entries ) ) ;
66- return this . _asyncLocalStorage . run ( resetZone , func ) ;
66+ return this . _asyncLocalStorage . run ( this , func ) ;
6767 }
6868
69- get < T > ( type : ZoneType ) : T | undefined {
69+ data < T > ( type : ZoneType ) : T | undefined {
7070 return this . _data . get ( type ) as T | undefined ;
7171 }
7272}
0 commit comments