/*---------------------------------------------------------------------------------------------
* 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
 */
import { BentleyStatus, IModelError, RpcRoutingToken } from "@itwin/core-common";
/**
 * Controls the RPC routing for an iModel connection.
 * @public
 */
export class IModelRoutingContext {
    static for(token) {
        return new IModelRoutingContext(token);
    }
    static get current() {
        return this._current;
    }
    get active() { return IModelRoutingContext.current === this; }
    constructor(token) {
        this.token = token;
    }
    route(handler) {
        if (IModelRoutingContext.current) {
            throw new IModelError(BentleyStatus.ERROR, "Concurrent use is not supported.");
        }
        IModelRoutingContext._current = this;
        const value = handler();
        IModelRoutingContext._current = undefined;
        return value;
    }
}
IModelRoutingContext.default = new IModelRoutingContext(RpcRoutingToken.default);
//# sourceMappingURL=IModelRoutingContext.js.map