/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/4import { BugIndicatingError } from './errors.js';56/*7* This file contains helper classes to manage control flow.8*/910/**11* Prevents code from being re-entrant.12*/13export class ReentrancyBarrier {14private _isOccupied = false;1516/**17* Calls `runner` if the barrier is not occupied.18* During the call, the barrier becomes occupied.19*/20public runExclusivelyOrSkip(runner: () => void): void {21if (this._isOccupied) {22return;23}24this._isOccupied = true;25try {26runner();27} finally {28this._isOccupied = false;29}30}3132/**33* Calls `runner`. If the barrier is occupied, throws an error.34* During the call, the barrier becomes active.35*/36public runExclusivelyOrThrow(runner: () => void): void {37if (this._isOccupied) {38throw new BugIndicatingError(`ReentrancyBarrier: reentrant call detected!`);39}40this._isOccupied = true;41try {42runner();43} finally {44this._isOccupied = false;45}46}4748/**49* Indicates if some runner occupies this barrier.50*/51public get isOccupied() {52return this._isOccupied;53}5455public makeExclusiveOrSkip<TFunction extends Function>(fn: TFunction): TFunction {56return ((...args: any[]) => {57if (this._isOccupied) {58return;59}60this._isOccupied = true;61try {62return fn(...args);63} finally {64this._isOccupied = false;65}66}) as any;67}68}697071