"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.IModelRoutingContext = void 0;
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
 * @module IModelConnection
 */
const core_bentley_1 = require("@itwin/core-bentley");
const core_common_1 = require("@itwin/core-common");
/**
 * Controls the RPC routing for an iModel connection.
 * @public
 */
class IModelRoutingContext {
    static _current;
    static for(token) {
        return new IModelRoutingContext(token);
    }
    static default = new IModelRoutingContext(core_common_1.RpcRoutingToken.default);
    static get current() {
        return this._current;
    }
    token;
    get active() { return IModelRoutingContext.current === this; }
    constructor(token) {
        this.token = token;
    }
    route(handler) {
        if (IModelRoutingContext.current) {
            throw new core_common_1.IModelError(core_bentley_1.BentleyStatus.ERROR, "Concurrent use is not supported.");
        }
        IModelRoutingContext._current = this;
        const value = handler();
        IModelRoutingContext._current = undefined;
        return value;
    }
}
exports.IModelRoutingContext = IModelRoutingContext;
//# sourceMappingURL=IModelRoutingContext.js.map