2021-11-18 22:30:09 -08:00
|
|
|
/**
|
|
|
|
* Copyright (c) Microsoft Corporation.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2024-04-16 12:51:20 -07:00
|
|
|
import { AsyncLocalStorage } from 'async_hooks';
|
2023-02-24 12:17:03 -08:00
|
|
|
|
2023-03-30 21:05:07 -07:00
|
|
|
export type ZoneType = 'apiZone' | 'expectZone' | 'stepZone';
|
2021-11-18 22:30:09 -08:00
|
|
|
|
|
|
|
class ZoneManager {
|
2024-04-16 12:51:20 -07:00
|
|
|
private readonly _asyncLocalStorage = new AsyncLocalStorage<Zone<unknown>|undefined>();
|
2021-11-18 22:30:09 -08:00
|
|
|
|
2024-04-16 16:18:37 -07:00
|
|
|
run<T, R>(type: ZoneType, data: T, func: () => R): R {
|
2024-04-16 12:51:20 -07:00
|
|
|
const previous = this._asyncLocalStorage.getStore();
|
|
|
|
const zone = new Zone(previous, type, data);
|
2024-04-16 16:18:37 -07:00
|
|
|
return this._asyncLocalStorage.run(zone, func);
|
2021-11-18 22:30:09 -08:00
|
|
|
}
|
|
|
|
|
2024-04-16 16:18:37 -07:00
|
|
|
zoneData<T>(type: ZoneType): T | undefined {
|
2024-04-16 12:51:20 -07:00
|
|
|
for (let zone = this._asyncLocalStorage.getStore(); zone; zone = zone.previous) {
|
|
|
|
if (zone.type === type)
|
|
|
|
return zone.data as T;
|
2021-11-18 22:30:09 -08:00
|
|
|
}
|
2024-04-16 16:18:37 -07:00
|
|
|
return undefined;
|
2021-11-18 22:30:09 -08:00
|
|
|
}
|
2023-04-28 08:57:43 -07:00
|
|
|
|
2024-04-16 12:51:20 -07:00
|
|
|
exitZones<R>(func: () => R): R {
|
|
|
|
return this._asyncLocalStorage.run(undefined, func);
|
2023-04-28 08:57:43 -07:00
|
|
|
}
|
|
|
|
|
2024-04-16 12:51:20 -07:00
|
|
|
printZones() {
|
|
|
|
const zones = [];
|
|
|
|
for (let zone = this._asyncLocalStorage.getStore(); zone; zone = zone.previous) {
|
|
|
|
let str = zone.type;
|
|
|
|
if (zone.type === 'apiZone')
|
|
|
|
str += `(${(zone.data as any).apiName})`;
|
|
|
|
zones.push(str);
|
|
|
|
|
|
|
|
}
|
|
|
|
console.log('zones: ', zones.join(' -> '));
|
|
|
|
}
|
2021-11-18 22:30:09 -08:00
|
|
|
}
|
|
|
|
|
2023-02-24 12:17:03 -08:00
|
|
|
class Zone<T> {
|
|
|
|
readonly type: ZoneType;
|
2024-04-16 12:51:20 -07:00
|
|
|
readonly data: T;
|
|
|
|
readonly previous: Zone<unknown> | undefined;
|
2021-11-18 22:30:09 -08:00
|
|
|
|
2024-04-16 12:51:20 -07:00
|
|
|
constructor(previous: Zone<unknown> | undefined, type: ZoneType, data: T) {
|
2021-11-18 22:30:09 -08:00
|
|
|
this.type = type;
|
|
|
|
this.data = data;
|
2024-04-16 12:51:20 -07:00
|
|
|
this.previous = previous;
|
2021-11-18 22:30:09 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const zones = new ZoneManager();
|