UNPKG

@itwin/presentation-frontend

Version:

Frontend of iModel.js Presentation library

220 lines • 8.99 kB
"use strict"; /*--------------------------------------------------------------------------------------------- * 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 */ Object.defineProperty(exports, "__esModule", { value: true }); exports.RulesetVariablesManagerImpl = void 0; const core_bentley_1 = require("@itwin/core-bentley"); const presentation_common_1 = require("@itwin/presentation-common"); /** @internal */ class RulesetVariablesManagerImpl { _clientValues = new Map(); _rulesetId; _ipcHandler; onVariableChanged = new core_bentley_1.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 presentation_common_1.VariableValueTypes.Bool: switch (variable.type) { case presentation_common_1.VariableValueTypes.Int: return 0 !== variable.value; case presentation_common_1.VariableValueTypes.Id64: return core_bentley_1.Id64.isValidId64(variable.value); default: return undefined; } case presentation_common_1.VariableValueTypes.Int: switch (variable.type) { case presentation_common_1.VariableValueTypes.Bool: return variable.value ? 1 : 0; case presentation_common_1.VariableValueTypes.Id64: return core_bentley_1.Id64.getUpperUint32(variable.value); default: return undefined; } case presentation_common_1.VariableValueTypes.IntArray: switch (variable.type) { case presentation_common_1.VariableValueTypes.Id64Array: return variable.value.map((id) => core_bentley_1.Id64.getUpperUint32(id)); default: return undefined; } case presentation_common_1.VariableValueTypes.Id64: switch (variable.type) { case presentation_common_1.VariableValueTypes.Bool: return core_bentley_1.Id64.fromLocalAndBriefcaseIds(variable.value ? 1 : 0, 0); case presentation_common_1.VariableValueTypes.Int: return core_bentley_1.Id64.fromLocalAndBriefcaseIds(variable.value, 0); default: return undefined; } case presentation_common_1.VariableValueTypes.Id64Array: switch (variable.type) { case presentation_common_1.VariableValueTypes.IntArray: return variable.value.map((int) => core_bentley_1.Id64.fromLocalAndBriefcaseIds(int, 0)); default: return undefined; } case presentation_common_1.VariableValueTypes.String: switch (variable.type) { case presentation_common_1.VariableValueTypes.IntArray: case presentation_common_1.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, presentation_common_1.VariableValueTypes.String)) || ""; } /** * Sets `string` variable value */ async setString(variableId, value) { await this.setValue({ id: variableId, type: presentation_common_1.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, presentation_common_1.VariableValueTypes.Bool)) || false; } /** * Sets `boolean` variable value */ async setBool(variableId, value) { await this.setValue({ id: variableId, type: presentation_common_1.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, presentation_common_1.VariableValueTypes.Int)) || 0; } /** * Sets `number` variable value */ async setInt(variableId, value) { await this.setValue({ id: variableId, type: presentation_common_1.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, presentation_common_1.VariableValueTypes.IntArray)) || []; } /** * Sets `number[]` variable value */ async setInts(variableId, value) { await this.setValue({ id: variableId, type: presentation_common_1.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, presentation_common_1.VariableValueTypes.Id64)) || core_bentley_1.Id64.invalid; } /** * Sets `Id64String` variable value */ async setId64(variableId, value) { await this.setValue({ id: variableId, type: presentation_common_1.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, presentation_common_1.VariableValueTypes.Id64Array)) || []; } /** * Sets `Id64String[]` variable value */ async setId64s(variableId, value) { await this.setValue({ id: variableId, type: presentation_common_1.VariableValueTypes.Id64Array, value: [...core_bentley_1.OrderedId64Iterable.sortArray(value)] }); } } exports.RulesetVariablesManagerImpl = RulesetVariablesManagerImpl; function variablesEqual(lhs, rhs) { if (lhs.type !== rhs.type) { return false; } switch (lhs.type) { case presentation_common_1.VariableValueTypes.IntArray: case presentation_common_1.VariableValueTypes.Id64Array: (0, core_bentley_1.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