UNPKG

@itwin/presentation-frontend

Version:

Frontend of iModel.js Presentation library

216 lines 8.02 kB
/*--------------------------------------------------------------------------------------------- * 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, OrderedId64Iterable } from "@itwin/core-bentley"; import { VariableValueTypes } from "@itwin/presentation-common"; /** @internal */ export class RulesetVariablesManagerImpl { _clientValues = new Map(); _rulesetId; _ipcHandler; onVariableChanged = new BeEvent(); constructor(rulesetId, ipcHandler) { this._rulesetId = rulesetId; this._ipcHandler = ipcHandler; } getAllVariables() { const variables = []; for (const [_, variable] of this._clientValues) { variables.push(variable); } return variables; } changeValueType(variable, toType) { 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; } async getValue(id, type) { const variable = this._clientValues.get(id); if (!variable) { return undefined; } if (variable.type !== type) { return this.changeValueType(variable, type); } return variable.value; } async setValue(variable) { 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); } async unset(variableId) { 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. */ async getString(variableId) { return (await this.getValue(variableId, VariableValueTypes.String)) || ""; } /** * Sets `string` variable value */ async setString(variableId, value) { 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. */ async getBool(variableId) { return (await this.getValue(variableId, VariableValueTypes.Bool)) || false; } /** * Sets `boolean` variable value */ async setBool(variableId, value) { 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. */ async getInt(variableId) { return (await this.getValue(variableId, VariableValueTypes.Int)) || 0; } /** * Sets `number` variable value */ async setInt(variableId, value) { 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. */ async getInts(variableId) { return (await this.getValue(variableId, VariableValueTypes.IntArray)) || []; } /** * Sets `number[]` variable value */ async setInts(variableId, value) { 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. */ async getId64(variableId) { return (await this.getValue(variableId, VariableValueTypes.Id64)) || Id64.invalid; } /** * Sets `Id64String` variable value */ async setId64(variableId, value) { 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. */ async getId64s(variableId) { return (await this.getValue(variableId, VariableValueTypes.Id64Array)) || []; } /** * Sets `Id64String[]` variable value */ async setId64s(variableId, value) { await this.setValue({ id: variableId, type: VariableValueTypes.Id64Array, value: [...OrderedId64Iterable.sortArray(value)] }); } } function variablesEqual(lhs, rhs) { 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, rhs) { if (lhs.length !== rhs.length) { return false; } for (let i = 0; i < lhs.length; ++i) { if (lhs[i] !== rhs[i]) { return false; } } return true; } //# sourceMappingURL=RulesetVariablesManager.js.map