-
Notifications
You must be signed in to change notification settings - Fork 208
/
IModelRoutingContext.ts
46 lines (36 loc) · 1.42 KB
/
IModelRoutingContext.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*---------------------------------------------------------------------------------------------
* 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 {
private static _current: IModelRoutingContext | undefined;
public static for(token: RpcRoutingToken) {
return new IModelRoutingContext(token);
}
public static readonly default = new IModelRoutingContext(RpcRoutingToken.default);
public static get current(): IModelRoutingContext | undefined {
return this._current;
}
public readonly token: RpcRoutingToken;
public get active(): boolean { return IModelRoutingContext.current === this; }
private constructor(token: RpcRoutingToken) {
this.token = token;
}
public route<T>(handler: () => T): T {
if (IModelRoutingContext.current) {
throw new IModelError(BentleyStatus.ERROR, "Concurrent use is not supported.");
}
IModelRoutingContext._current = this;
const value = handler();
IModelRoutingContext._current = undefined;
return value;
}
}