-
Notifications
You must be signed in to change notification settings - Fork 208
/
RulesetVariablesManager.ts
323 lines (286 loc) · 10.3 KB
/
RulesetVariablesManager.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module Core
*/
import { assert, BeEvent, Id64, Id64String, OrderedId64Iterable } from "@itwin/core-bentley";
import { RulesetVariable, VariableValue, VariableValueTypes } from "@itwin/presentation-common";
import { IpcRequestsHandler } from "./IpcRequestsHandler";
/**
* Presentation ruleset variables' registry.
* @public
*/
export interface RulesetVariablesManager {
/**
* An event that is raised when variable changes.
*/
onVariableChanged: BeEvent<(variableId: string, prevValue: VariableValue | undefined, currValue: VariableValue | undefined) => void>;
/**
* Retrieves `string` variable value.
* Returns empty string if variable does not exist or does not convert to string.
*/
getString(variableId: string): Promise<string>;
/**
* Sets `string` variable value
*/
setString(variableId: string, value: string): Promise<void>;
/**
* Retrieves `boolean` variable value.
* Returns `false` if variable does not exist or does not convert to boolean.
*/
getBool(variableId: string): Promise<boolean>;
/**
* Sets `boolean` variable value
*/
setBool(variableId: string, value: boolean): Promise<void>;
/**
* Retrieves `number` variable value.
* Returns `0` if variable does not exist or does not convert to integer.
*/
getInt(variableId: string): Promise<number>;
/**
* Sets `number` variable value
*/
setInt(variableId: string, value: number): Promise<void>;
/**
* Retrieves `number[]` variable value.
* Returns empty array if variable does not exist or does not convert to integer array.
*/
getInts(variableId: string): Promise<number[]>;
/**
* Sets `number[]` variable value
*/
setInts(variableId: string, value: number[]): Promise<void>;
/**
* Retrieves `Id64String` variable value.
* Returns invalid Id64String if variable does not exist or does not convert to Id64String.
*/
getId64(variableId: string): Promise<Id64String>;
/**
* Sets `Id64String` variable value
*/
setId64(variableId: string, value: Id64String): Promise<void>;
/**
* Retrieves `Id64String[]` variable value.
* Returns empty array if variable does not exist or does not convert to Id64String array.
*/
getId64s(variableId: string): Promise<Id64String[]>;
/**
* Sets `Id64String[]` variable value
*/
setId64s(variableId: string, value: Id64String[]): Promise<void>;
/** Unsets variable with given id. */
unset(variableId: string): Promise<void>;
/** Retrieves all variables.
* @internal
*/
getAllVariables(): RulesetVariable[];
}
/** @internal */
export class RulesetVariablesManagerImpl implements RulesetVariablesManager {
private _clientValues = new Map<string, RulesetVariable>();
private _rulesetId: string;
private _ipcHandler?: IpcRequestsHandler;
public onVariableChanged = new BeEvent<(variableId: string, prevValue: VariableValue | undefined, currValue: VariableValue | undefined) => void>();
public constructor(rulesetId: string, ipcHandler?: IpcRequestsHandler) {
this._rulesetId = rulesetId;
this._ipcHandler = ipcHandler;
}
public getAllVariables(): RulesetVariable[] {
const variables: RulesetVariable[] = [];
for (const entry of this._clientValues) {
variables.push(entry[1]);
}
return variables;
}
private changeValueType(variable: RulesetVariable, toType: VariableValueTypes): VariableValue | undefined {
switch (toType) {
case VariableValueTypes.Bool:
switch (variable.type) {
case VariableValueTypes.Int:
return 0 !== variable.value;
case VariableValueTypes.Id64:
return Id64.isValidId64(variable.value);
default:
return undefined;
}
case VariableValueTypes.Int:
switch (variable.type) {
case VariableValueTypes.Bool:
return variable.value ? 1 : 0;
case VariableValueTypes.Id64:
return Id64.getUpperUint32(variable.value);
default:
return undefined;
}
case VariableValueTypes.IntArray:
switch (variable.type) {
case VariableValueTypes.Id64Array:
return variable.value.map((id) => Id64.getUpperUint32(id));
default:
return undefined;
}
case VariableValueTypes.Id64:
switch (variable.type) {
case VariableValueTypes.Bool:
return Id64.fromLocalAndBriefcaseIds(variable.value ? 1 : 0, 0);
case VariableValueTypes.Int:
return Id64.fromLocalAndBriefcaseIds(variable.value, 0);
default:
return undefined;
}
case VariableValueTypes.Id64Array:
switch (variable.type) {
case VariableValueTypes.IntArray:
return variable.value.map((int) => Id64.fromLocalAndBriefcaseIds(int, 0));
default:
return undefined;
}
case VariableValueTypes.String:
switch (variable.type) {
case VariableValueTypes.IntArray:
case VariableValueTypes.Id64Array:
return undefined;
default:
variable.value.toString();
}
}
return undefined;
}
private async getValue(id: string, type: VariableValueTypes): Promise<VariableValue | undefined> {
const variable = this._clientValues.get(id);
if (!variable) {
return undefined;
}
if (variable.type !== type) {
return this.changeValueType(variable, type);
}
return variable.value;
}
private async setValue(variable: RulesetVariable): Promise<void> {
const oldVariable = this._clientValues.get(variable.id);
if (oldVariable && variablesEqual(oldVariable, variable)) {
return;
}
this._clientValues.set(variable.id, variable);
if (this._ipcHandler) {
await this._ipcHandler.setRulesetVariable({ rulesetId: this._rulesetId, variable });
}
this.onVariableChanged.raiseEvent(variable.id, oldVariable?.value, variable.value);
}
public async unset(variableId: string): Promise<void> {
const variable = this._clientValues.get(variableId);
if (variable === undefined) {
return;
}
this._clientValues.delete(variable.id);
if (this._ipcHandler) {
await this._ipcHandler.unsetRulesetVariable({ rulesetId: this._rulesetId, variableId });
}
this.onVariableChanged.raiseEvent(variable.id, variable.value, undefined);
}
/**
* Retrieves `string` variable value.
* Returns empty string if variable does not exist or does not convert to string.
*/
public async getString(variableId: string): Promise<string> {
return ((await this.getValue(variableId, VariableValueTypes.String)) as string) || "";
}
/**
* Sets `string` variable value
*/
public async setString(variableId: string, value: string): Promise<void> {
await this.setValue({ id: variableId, type: VariableValueTypes.String, value });
}
/**
* Retrieves `boolean` variable value.
* Returns `false` if variable does not exist or does not convert to boolean.
*/
public async getBool(variableId: string): Promise<boolean> {
return ((await this.getValue(variableId, VariableValueTypes.Bool)) as boolean) || false;
}
/**
* Sets `boolean` variable value
*/
public async setBool(variableId: string, value: boolean): Promise<void> {
await this.setValue({ id: variableId, type: VariableValueTypes.Bool, value });
}
/**
* Retrieves `number` variable value.
* Returns `0` if variable does not exist or does not convert to integer.
*/
public async getInt(variableId: string): Promise<number> {
return ((await this.getValue(variableId, VariableValueTypes.Int)) as number) || 0;
}
/**
* Sets `number` variable value
*/
public async setInt(variableId: string, value: number): Promise<void> {
await this.setValue({ id: variableId, type: VariableValueTypes.Int, value });
}
/**
* Retrieves `number[]` variable value.
* Returns empty array if variable does not exist or does not convert to integer array.
*/
public async getInts(variableId: string): Promise<number[]> {
return ((await this.getValue(variableId, VariableValueTypes.IntArray)) as number[]) || [];
}
/**
* Sets `number[]` variable value
*/
public async setInts(variableId: string, value: number[]): Promise<void> {
await this.setValue({ id: variableId, type: VariableValueTypes.IntArray, value: [...value] });
}
/**
* Retrieves `Id64String` variable value.
* Returns invalid Id64String if variable does not exist or does not convert to Id64String.
*/
public async getId64(variableId: string): Promise<Id64String> {
return ((await this.getValue(variableId, VariableValueTypes.Id64)) as Id64String) || Id64.invalid;
}
/**
* Sets `Id64String` variable value
*/
public async setId64(variableId: string, value: Id64String): Promise<void> {
await this.setValue({ id: variableId, type: VariableValueTypes.Id64, value });
}
/**
* Retrieves `Id64String[]` variable value.
* Returns empty array if variable does not exist or does not convert to Id64String array.
*/
public async getId64s(variableId: string): Promise<Id64String[]> {
return ((await this.getValue(variableId, VariableValueTypes.Id64Array)) as Id64String[]) || [];
}
/**
* Sets `Id64String[]` variable value
*/
public async setId64s(variableId: string, value: Id64String[]): Promise<void> {
await this.setValue({ id: variableId, type: VariableValueTypes.Id64Array, value: [...OrderedId64Iterable.sortArray(value)] });
}
}
function variablesEqual(lhs: RulesetVariable, rhs: RulesetVariable) {
if (lhs.type !== rhs.type) {
return false;
}
switch (lhs.type) {
case VariableValueTypes.IntArray:
case VariableValueTypes.Id64Array:
assert(rhs.type === lhs.type);
return arraysEqual(lhs.value, rhs.value);
default:
return lhs.value === rhs.value;
}
}
function arraysEqual(lhs: any[], rhs: any[]) {
if (lhs.length !== rhs.length) {
return false;
}
for (let i = 0; i < lhs.length; ++i) {
if (lhs[i] !== rhs[i]) {
return false;
}
}
return true;
}