@itwin/core-backend
Version:
iTwin.js backend components
158 lines • 6.14 kB
JavaScript
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module Workspace
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.SettingsImpl = void 0;
const fs = require("fs-extra");
const json5_1 = require("json5");
const path_1 = require("path");
const core_bentley_1 = require("@itwin/core-bentley");
const IModelJsFs_1 = require("../../IModelJsFs");
const Settings_1 = require("../../workspace/Settings");
const IModelHost_1 = require("../../IModelHost");
const Symbols_1 = require("../Symbols");
const dictionaryMatches = (d1, d2) => {
return (d1.workspaceDb === d2.workspaceDb) && (d1.name === d2.name);
};
class SettingsDictionaryImpl {
[Symbols_1._implementationProhibited] = undefined;
props;
settings;
constructor(props, settings) {
this.props = { ...props }; // make a copy so it can't be changed by caller
this.settings = settings;
}
getSetting(settingName) {
const value = this.settings[settingName];
return undefined !== value ? Settings_1.Setting.clone(value) : undefined;
}
}
/**
* Internal implementation of Settings interface.
* @internal
*/
class SettingsImpl {
[Symbols_1._implementationProhibited] = undefined;
dictionaries = [];
verifyPriority(_priority) { }
close() { }
onSettingsChanged = new core_bentley_1.BeEvent();
addFile(fileName, priority) {
this.addJson({ name: fileName, priority }, fs.readFileSync(fileName, "utf-8"));
}
addDirectory(dirName, priority) {
for (const fileName of IModelJsFs_1.IModelJsFs.readdirSync(dirName)) {
const ext = (0, path_1.extname)(fileName);
if (ext === ".json5" || ext === ".json")
this.addFile((0, path_1.join)(dirName, fileName), priority);
}
}
addJson(props, settingsJson) {
this.addDictionary(props, (0, json5_1.parse)(settingsJson));
}
addDictionary(props, settings) {
this.verifyPriority(props.priority);
this.dropDictionary(props, false); // make sure we don't have the same dictionary twice
const dict = new SettingsDictionaryImpl(props, settings);
const doAdd = () => {
for (let i = 0; i < this.dictionaries.length; ++i) {
if (this.dictionaries[i].props.priority <= dict.props.priority) {
this.dictionaries.splice(i, 0, dict);
return;
}
}
this.dictionaries.push(dict);
};
doAdd();
this.onSettingsChanged.raiseEvent();
}
getDictionary(source) {
for (const dictionary of this.dictionaries) {
if (dictionaryMatches(dictionary.props, source))
return dictionary;
}
return undefined;
}
dropDictionary(source, raiseEvent = true) {
for (let i = 0; i < this.dictionaries.length; ++i) {
if (dictionaryMatches(this.dictionaries[i].props, source)) {
this.dictionaries.splice(i, 1);
if (raiseEvent)
this.onSettingsChanged.raiseEvent();
return true;
}
}
return false;
}
*getSettingEntries(settingName) {
for (const dictionary of this.dictionaries) {
const value = dictionary.getSetting(settingName);
if (undefined !== value) {
yield { value, dictionary };
}
}
}
*getSettingValues(settingName) {
for (const entry of this.getSettingEntries(settingName)) {
yield entry.value;
}
}
getSetting(settingName, defaultValue) {
for (const value of this.getSettingValues(settingName)) {
return value;
}
return defaultValue;
}
// get the setting and verify the result is either undefined or the correct type. If so, return it. Otherwise throw an exception.
getResult(name, expectedType) {
const out = this.getSetting(name);
if (out === undefined || typeof out === expectedType)
return out;
throw new Error(`setting "${name}" is not a ${expectedType}: ${typeof out}`);
}
getString(name, defaultValue) {
return this.getResult(name, "string") ?? defaultValue;
}
getBoolean(name, defaultValue) {
return this.getResult(name, "boolean") ?? defaultValue;
}
getNumber(name, defaultValue) {
return this.getResult(name, "number") ?? defaultValue;
}
getObject(name, defaultValue) {
const out = this.getResult(name, "object");
return out ? IModelHost_1.IModelHost.settingsSchemas.validateSetting(out, name) : defaultValue;
}
getArray(name, defaultValue) {
if (IModelHost_1.IModelHost.settingsSchemas.settingDefs.get(name)?.combineArray) {
return this.getCombinedArray(name, defaultValue);
}
const out = this.getSetting(name);
if (out === undefined)
return defaultValue;
if (!Array.isArray(out))
throw new Error(`setting ${name} is not an array: ${out}`);
return IModelHost_1.IModelHost.settingsSchemas.validateSetting(out, name);
}
getCombinedArray(name, defaultValue) {
let foundSetting = false;
const out = [];
for (const array of this.getSettingValues(name)) {
foundSetting = true;
IModelHost_1.IModelHost.settingsSchemas.validateSetting(array, name);
for (const value of array) {
if (undefined === out.find((x) => Settings_1.Setting.areEqual(x, value))) {
out.push(value);
}
}
}
return foundSetting ? out : defaultValue;
}
}
exports.SettingsImpl = SettingsImpl;
//# sourceMappingURL=SettingsImpl.js.map
;