@difizen/mana-core
Version:
910 lines (906 loc) • 52.9 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.UserSettings = exports.DefaultConfigurationModel = exports.ConfigurationModelParser = exports.ConfigurationModel = exports.ConfigurationChangeEvent = exports.Configuration = exports.AllKeysConfigurationChangeEvent = void 0;
exports.distinct = distinct;
exports.equals = equals;
exports.mergeChanges = mergeChanges;
var _manaCommon = require("@difizen/mana-common");
var _utils = require("../../../utils");
var _platform = require("../platform");
var _configuration = require("./configuration");
var _configurationRegistry = require("./configurationRegistry");
var json = _interopRequireWildcard(require("./json"));
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return e; }; var t, e = {}, r = Object.prototype, n = r.hasOwnProperty, o = Object.defineProperty || function (t, e, r) { t[e] = r.value; }, i = "function" == typeof Symbol ? Symbol : {}, a = i.iterator || "@@iterator", c = i.asyncIterator || "@@asyncIterator", u = i.toStringTag || "@@toStringTag"; function define(t, e, r) { return Object.defineProperty(t, e, { value: r, enumerable: !0, configurable: !0, writable: !0 }), t[e]; } try { define({}, ""); } catch (t) { define = function define(t, e, r) { return t[e] = r; }; } function wrap(t, e, r, n) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype), c = new Context(n || []); return o(a, "_invoke", { value: makeInvokeMethod(t, r, c) }), a; } function tryCatch(t, e, r) { try { return { type: "normal", arg: t.call(e, r) }; } catch (t) { return { type: "throw", arg: t }; } } e.wrap = wrap; var h = "suspendedStart", l = "suspendedYield", f = "executing", s = "completed", y = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var p = {}; define(p, a, function () { return this; }); var d = Object.getPrototypeOf, v = d && d(d(values([]))); v && v !== r && n.call(v, a) && (p = v); var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p); function defineIteratorMethods(t) { ["next", "throw", "return"].forEach(function (e) { define(t, e, function (t) { return this._invoke(e, t); }); }); } function AsyncIterator(t, e) { function invoke(r, o, i, a) { var c = tryCatch(t[r], t, o); if ("throw" !== c.type) { var u = c.arg, h = u.value; return h && "object" == _typeof(h) && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) { invoke("next", t, i, a); }, function (t) { invoke("throw", t, i, a); }) : e.resolve(h).then(function (t) { u.value = t, i(u); }, function (t) { return invoke("throw", t, i, a); }); } a(c.arg); } var r; o(this, "_invoke", { value: function value(t, n) { function callInvokeWithMethodAndArg() { return new e(function (e, r) { invoke(t, n, e, r); }); } return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(e, r, n) { var o = h; return function (i, a) { if (o === f) throw new Error("Generator is already running"); if (o === s) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var c = n.delegate; if (c) { var u = maybeInvokeDelegate(c, n); if (u) { if (u === y) continue; return u; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (o === h) throw o = s, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = f; var p = tryCatch(e, r, n); if ("normal" === p.type) { if (o = n.done ? s : l, p.arg === y) continue; return { value: p.arg, done: n.done }; } "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg); } }; } function maybeInvokeDelegate(e, r) { var n = r.method, o = e.iterator[n]; if (o === t) return r.delegate = null, "throw" === n && e.iterator.return && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y; var i = tryCatch(o, e.iterator, r.arg); if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y; var a = i.arg; return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y); } function pushTryEntry(t) { var e = { tryLoc: t[0] }; 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e); } function resetTryEntry(t) { var e = t.completion || {}; e.type = "normal", delete e.arg, t.completion = e; } function Context(t) { this.tryEntries = [{ tryLoc: "root" }], t.forEach(pushTryEntry, this), this.reset(!0); } function values(e) { if (e || "" === e) { var r = e[a]; if (r) return r.call(e); if ("function" == typeof e.next) return e; if (!isNaN(e.length)) { var o = -1, i = function next() { for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next; return next.value = t, next.done = !0, next; }; return i.next = i; } } throw new TypeError(_typeof(e) + " is not iterable"); } return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), o(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) { var e = "function" == typeof t && t.constructor; return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name)); }, e.mark = function (t) { return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t; }, e.awrap = function (t) { return { __await: t }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () { return this; }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) { void 0 === i && (i = Promise); var a = new AsyncIterator(wrap(t, r, n, o), i); return e.isGeneratorFunction(r) ? a : a.next().then(function (t) { return t.done ? t.value : a.next(); }); }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () { return this; }), define(g, "toString", function () { return "[object Generator]"; }), e.keys = function (t) { var e = Object(t), r = []; for (var n in e) r.push(n); return r.reverse(), function next() { for (; r.length;) { var t = r.pop(); if (t in e) return next.value = t, next.done = !1, next; } return next.done = !0, next; }; }, e.values = values, Context.prototype = { constructor: Context, reset: function reset(e) { if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t); }, stop: function stop() { this.done = !0; var t = this.tryEntries[0].completion; if ("throw" === t.type) throw t.arg; return this.rval; }, dispatchException: function dispatchException(e) { if (this.done) throw e; var r = this; function handle(n, o) { return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o; } for (var o = this.tryEntries.length - 1; o >= 0; --o) { var i = this.tryEntries[o], a = i.completion; if ("root" === i.tryLoc) return handle("end"); if (i.tryLoc <= this.prev) { var c = n.call(i, "catchLoc"), u = n.call(i, "finallyLoc"); if (c && u) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } else if (c) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); } else { if (!u) throw new Error("try statement without catch or finally"); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } } } }, abrupt: function abrupt(t, e) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var o = this.tryEntries[r]; if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) { var i = o; break; } } i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null); var a = i ? i.completion : {}; return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a); }, complete: function complete(t, e) { if ("throw" === t.type) throw t.arg; return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y; }, finish: function finish(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y; } }, catch: function _catch(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.tryLoc === t) { var n = r.completion; if ("throw" === n.type) { var o = n.arg; resetTryEntry(r); } return o; } } throw new Error("illegal catch attempt"); }, delegateYield: function delegateYield(e, r, n) { return this.delegate = { iterator: values(e), resultName: r, nextLoc: n }, "next" === this.method && (this.arg = t), y; } }, e; }
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } /*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
//TODO
function distinct(array, keyFn) {
if (!keyFn) {
return array.filter(function (element, position) {
return array.indexOf(element) === position;
});
}
var seen = Object.create(null);
return array.filter(function (elem) {
var key = keyFn(elem);
if (seen[key]) {
return false;
}
seen[key] = true;
return true;
});
}
function equals(one, other) {
var itemEquals = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : function (a, b) {
return a === b;
};
if (one === other) {
return true;
}
if (!one || !other) {
return false;
}
if (one.length !== other.length) {
return false;
}
for (var i = 0, len = one.length; i < len; i++) {
if (!itemEquals(one[i], other[i])) {
return false;
}
}
return true;
}
var ConfigurationModel = exports.ConfigurationModel = /*#__PURE__*/function () {
function ConfigurationModel() {
var _contents = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var _keys = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
var _overrides = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
_classCallCheck(this, ConfigurationModel);
this.isFrozen = false;
this._contents = {};
this._keys = [];
this._overrides = [];
this._contents = _contents;
this._keys = _keys;
this._overrides = _overrides;
}
_createClass(ConfigurationModel, [{
key: "contents",
get: function get() {
return this.checkAndFreeze(this._contents);
}
}, {
key: "overrides",
get: function get() {
return this.checkAndFreeze(this._overrides);
}
}, {
key: "keys",
get: function get() {
return this.checkAndFreeze(this._keys);
}
}, {
key: "isEmpty",
value: function isEmpty() {
return this._keys.length === 0 && Object.keys(this._contents).length === 0 && this._overrides.length === 0;
}
}, {
key: "getValue",
value: function getValue(section) {
return section ? (0, _configuration.getConfigurationValue)(this.contents, section) : this.contents;
}
}, {
key: "getOverrideValue",
value: function getOverrideValue(section, overrideIdentifier) {
var overrideContents = this.getContentsForOverrideIdentifer(overrideIdentifier);
return overrideContents ? section ? (0, _configuration.getConfigurationValue)(overrideContents, section) : overrideContents : undefined;
}
}, {
key: "getKeysForOverrideIdentifier",
value: function getKeysForOverrideIdentifier(identifier) {
var _iterator = _createForOfIteratorHelper(this.overrides),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var override = _step.value;
if (override.identifiers.indexOf(identifier) !== -1) {
return override.keys;
}
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
return [];
}
}, {
key: "override",
value: function override(identifier) {
var overrideContents = this.getContentsForOverrideIdentifer(identifier);
if (!overrideContents || _typeof(overrideContents) !== 'object' || !Object.keys(overrideContents).length) {
// If there are no valid overrides, return self
return this;
}
var contents = {};
var _iterator2 = _createForOfIteratorHelper(distinct([].concat(_toConsumableArray(Object.keys(this.contents)), _toConsumableArray(Object.keys(overrideContents))))),
_step2;
try {
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
var key = _step2.value;
var contentsForKey = this.contents[key];
var overrideContentsForKey = overrideContents[key];
// If there are override contents for the key, clone and merge otherwise use base contents
if (overrideContentsForKey) {
// Clone and merge only if base contents and override contents are of type object otherwise just override
if (_typeof(contentsForKey) === 'object' && _typeof(overrideContentsForKey) === 'object') {
contentsForKey = _manaCommon.objects.deepClone(contentsForKey);
this.mergeContents(contentsForKey, overrideContentsForKey);
} else {
contentsForKey = overrideContentsForKey;
}
}
contents[key] = contentsForKey;
}
} catch (err) {
_iterator2.e(err);
} finally {
_iterator2.f();
}
return new ConfigurationModel(contents, this.keys, this.overrides);
}
}, {
key: "merge",
value: function merge() {
var _this = this;
var contents = _manaCommon.objects.deepClone(this.contents);
var overrides = _manaCommon.objects.deepClone(this.overrides);
var keys = _toConsumableArray(this.keys);
for (var _len = arguments.length, others = new Array(_len), _key2 = 0; _key2 < _len; _key2++) {
others[_key2] = arguments[_key2];
}
for (var _i = 0, _others = others; _i < _others.length; _i++) {
var other = _others[_i];
this.mergeContents(contents, other.contents);
var _iterator3 = _createForOfIteratorHelper(other.overrides),
_step3;
try {
var _loop = function _loop() {
var otherOverride = _step3.value;
var _overrides$filter = overrides.filter(function (o) {
return equals(o.identifiers, otherOverride.identifiers);
}),
_overrides$filter2 = _slicedToArray(_overrides$filter, 1),
override = _overrides$filter2[0];
if (override) {
_this.mergeContents(override.contents, otherOverride.contents);
} else {
overrides.push(_manaCommon.objects.deepClone(otherOverride));
}
};
for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
_loop();
}
} catch (err) {
_iterator3.e(err);
} finally {
_iterator3.f();
}
var _iterator4 = _createForOfIteratorHelper(other.keys),
_step4;
try {
for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
var key = _step4.value;
if (keys.indexOf(key) === -1) {
keys.push(key);
}
}
} catch (err) {
_iterator4.e(err);
} finally {
_iterator4.f();
}
}
return new ConfigurationModel(contents, keys, overrides);
}
}, {
key: "freeze",
value: function freeze() {
this.isFrozen = true;
return this;
}
}, {
key: "mergeContents",
value: function mergeContents(source, target) {
for (var _i2 = 0, _Object$keys = Object.keys(target); _i2 < _Object$keys.length; _i2++) {
var key = _Object$keys[_i2];
if (key in source) {
if (_manaCommon.types.isObject(source[key]) && _manaCommon.types.isObject(target[key])) {
this.mergeContents(source[key], target[key]);
continue;
}
}
source[key] = _manaCommon.objects.deepClone(target[key]);
}
}
}, {
key: "checkAndFreeze",
value: function checkAndFreeze(data) {
if (this.isFrozen && !Object.isFrozen(data)) {
return _manaCommon.objects.deepFreeze(data);
}
return data;
}
}, {
key: "getContentsForOverrideIdentifer",
value: function getContentsForOverrideIdentifer(identifier) {
var _iterator5 = _createForOfIteratorHelper(this.overrides),
_step5;
try {
for (_iterator5.s(); !(_step5 = _iterator5.n()).done;) {
var override = _step5.value;
if (override.identifiers.indexOf(identifier) !== -1) {
return override.contents;
}
}
} catch (err) {
_iterator5.e(err);
} finally {
_iterator5.f();
}
return null;
}
}, {
key: "toJSON",
value: function toJSON() {
return {
contents: this.contents,
overrides: this.overrides,
keys: this.keys
};
}
// Update methods
}, {
key: "setValue",
value: function setValue(key, value) {
this.addKey(key);
(0, _configuration.addToValueTree)(this.contents, key, value, function (e) {
throw new Error(e);
});
}
}, {
key: "removeValue",
value: function removeValue(key) {
if (this.removeKey(key)) {
(0, _configuration.removeFromValueTree)(this.contents, key);
}
}
}, {
key: "addKey",
value: function addKey(key) {
var index = this.keys.length;
for (var i = 0; i < index; i++) {
if (key.indexOf(this.keys[i]) === 0) {
index = i;
}
}
this.keys.splice(index, 1, key);
}
}, {
key: "removeKey",
value: function removeKey(key) {
var index = this.keys.indexOf(key);
if (index !== -1) {
this.keys.splice(index, 1);
return true;
}
return false;
}
}]);
return ConfigurationModel;
}();
var DefaultConfigurationModel = exports.DefaultConfigurationModel = /*#__PURE__*/function (_ConfigurationModel2) {
_inherits(DefaultConfigurationModel, _ConfigurationModel2);
var _super = _createSuper(DefaultConfigurationModel);
function DefaultConfigurationModel() {
_classCallCheck(this, DefaultConfigurationModel);
var contents = (0, _configuration.getDefaultValues)();
var keys = (0, _configuration.getConfigurationKeys)();
var overrides = [];
for (var _i3 = 0, _Object$keys2 = Object.keys(contents); _i3 < _Object$keys2.length; _i3++) {
var key = _Object$keys2[_i3];
if (_configurationRegistry.OVERRIDE_PROPERTY_PATTERN.test(key)) {
overrides.push({
identifiers: [(0, _configurationRegistry.overrideIdentifierFromKey)(key).trim()],
keys: Object.keys(contents[key]),
contents: (0, _configuration.toValuesTree)(contents[key], function (message) {
return console.error("Conflict in default settings file: ".concat(message));
})
});
}
}
return _super.call(this, contents, keys, overrides);
}
return _createClass(DefaultConfigurationModel);
}(ConfigurationModel);
var ConfigurationModelParser = exports.ConfigurationModelParser = /*#__PURE__*/function () {
function ConfigurationModelParser(_name, _scopes) {
_classCallCheck(this, ConfigurationModelParser);
this._raw = null;
this._configurationModel = null;
this._parseErrors = [];
this._name = _name;
this._scopes = _scopes;
}
_createClass(ConfigurationModelParser, [{
key: "configurationModel",
get: function get() {
return this._configurationModel || new ConfigurationModel();
}
}, {
key: "errors",
get: function get() {
return this._parseErrors;
}
}, {
key: "parseContent",
value: function parseContent(content) {
if (!_manaCommon.types.isUndefinedOrNull(content)) {
var raw = this.doParseContent(content);
this.parseRaw(raw);
}
}
}, {
key: "parseRaw",
value: function parseRaw(raw) {
this._raw = raw;
var configurationModel = this.doParseRaw(raw);
this._configurationModel = new ConfigurationModel(configurationModel.contents, configurationModel.keys, configurationModel.overrides);
}
}, {
key: "parse",
value: function parse() {
if (this._raw) {
this.parseRaw(this._raw);
}
}
}, {
key: "doParseContent",
value: function doParseContent(content) {
var raw = {};
var currentProperty = null;
var currentParent = [];
var previousParents = [];
var parseErrors = [];
function onValue(value) {
if (Array.isArray(currentParent)) {
currentParent.push(value);
} else if (currentProperty) {
currentParent[currentProperty] = value;
}
}
var visitor = {
onObjectBegin: function onObjectBegin() {
var object = {};
onValue(object);
previousParents.push(currentParent);
currentParent = object;
currentProperty = null;
},
onObjectProperty: function onObjectProperty(name) {
currentProperty = name;
},
onObjectEnd: function onObjectEnd() {
currentParent = previousParents.pop();
},
onArrayBegin: function onArrayBegin() {
var array = [];
onValue(array);
previousParents.push(currentParent);
currentParent = array;
currentProperty = null;
},
onArrayEnd: function onArrayEnd() {
currentParent = previousParents.pop();
},
onLiteralValue: onValue,
onError: function onError(error, offset, length) {
parseErrors.push({
error: error,
offset: offset,
length: length
});
}
};
if (content) {
try {
json.visit(content, visitor);
raw = currentParent[0] || {};
} catch (e) {
console.error("Error while parsing settings file ".concat(this._name, ": ").concat(e));
this._parseErrors = [e];
}
}
return raw;
}
}, {
key: "doParseRaw",
value: function doParseRaw(raw) {
var _this2 = this;
if (this._scopes) {
var configurationProperties = _platform.Registry.as(_configurationRegistry.Extensions.Configuration).getConfigurationProperties();
raw = this.filterByScope(raw, configurationProperties, true, this._scopes);
}
var contents = (0, _configuration.toValuesTree)(raw, function (message) {
return console.error("Conflict in settings file ".concat(_this2._name, ": ").concat(message));
});
var keys = Object.keys(raw);
var overrides = (0, _configuration.toOverrides)(raw, function (message) {
return console.error("Conflict in settings file ".concat(_this2._name, ": ").concat(message));
});
return {
contents: contents,
keys: keys,
overrides: overrides
};
}
}, {
key: "filterByScope",
value: function filterByScope(properties, configurationProperties, filterOverriddenProperties, scopes) {
var result = {};
for (var key in properties) {
if (_configurationRegistry.OVERRIDE_PROPERTY_PATTERN.test(key) && filterOverriddenProperties) {
result[key] = this.filterByScope(properties[key], configurationProperties, false, scopes);
} else {
var scope = this.getScope(key, configurationProperties);
// Load unregistered configurations always.
if (scope === undefined || scopes.indexOf(scope) !== -1) {
result[key] = properties[key];
}
}
}
return result;
}
}, {
key: "getScope",
value: function getScope(key, configurationProperties) {
var propertySchema = configurationProperties[key];
return propertySchema ? typeof propertySchema.scope !== 'undefined' ? propertySchema.scope : _configurationRegistry.ConfigurationScope.WINDOW : undefined;
}
}]);
return ConfigurationModelParser;
}();
var UserSettings = exports.UserSettings = /*#__PURE__*/function (_VSDisposable) {
_inherits(UserSettings, _VSDisposable);
var _super2 = _createSuper(UserSettings);
function UserSettings(scopes) {
var _this3;
_classCallCheck(this, UserSettings);
_this3 = _super2.call(this);
_this3._onDidChange = _this3._register(new _manaCommon.Emitter());
_this3.onDidChange = _this3._onDidChange.event;
_this3.scopes = scopes;
_this3.parser = new ConfigurationModelParser('UserSettings', _this3.scopes);
return _this3;
}
_createClass(UserSettings, [{
key: "loadConfiguration",
value: function () {
var _loadConfiguration = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
return _regeneratorRuntime().wrap(function _callee$(_context) {
while (1) switch (_context.prev = _context.next) {
case 0:
_context.prev = 0;
this.parser.parseContent('{}');
return _context.abrupt("return", this.parser.configurationModel);
case 5:
_context.prev = 5;
_context.t0 = _context["catch"](0);
return _context.abrupt("return", new ConfigurationModel());
case 8:
case "end":
return _context.stop();
}
}, _callee, this, [[0, 5]]);
}));
function loadConfiguration() {
return _loadConfiguration.apply(this, arguments);
}
return loadConfiguration;
}()
}, {
key: "reprocess",
value: function reprocess() {
this.parser.parse();
return this.parser.configurationModel;
}
}]);
return UserSettings;
}(_utils.VSDisposable);
var Configuration = exports.Configuration = /*#__PURE__*/function () {
function Configuration(_defaultConfiguration, _localUserConfiguration) {
var _remoteUserConfiguration = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : new ConfigurationModel();
var _workspaceConfiguration = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : new ConfigurationModel();
var _memoryConfiguration = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : new ConfigurationModel();
var _memoryConfigurationByResource = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : new _utils.ResourceMap();
var _freeze = arguments.length > 6 && arguments[6] !== undefined ? arguments[6] : true;
_classCallCheck(this, Configuration);
this._workspaceConsolidatedConfiguration = null;
this._foldersConsolidatedConfigurations = new _utils.ResourceMap();
this._remoteUserConfiguration = new ConfigurationModel();
this._workspaceConfiguration = new ConfigurationModel();
this._memoryConfiguration = new ConfigurationModel();
this._memoryConfigurationByResource = new _utils.ResourceMap();
this._freeze = true;
this._userConfiguration = null;
this._memoryConfigurationByResource;
this._defaultConfiguration = _defaultConfiguration;
this._localUserConfiguration = _localUserConfiguration;
this._remoteUserConfiguration = _remoteUserConfiguration;
this._workspaceConfiguration = _workspaceConfiguration;
this._memoryConfiguration = _memoryConfiguration;
this._memoryConfigurationByResource = _memoryConfigurationByResource;
this._freeze = _freeze;
}
_createClass(Configuration, [{
key: "getValue",
value: function getValue(section, overrides, workspace) {
var consolidateConfigurationModel = this.getConsolidateConfigurationModel(overrides, workspace);
return consolidateConfigurationModel.getValue(section);
}
}, {
key: "updateValue",
value: function updateValue(_key, _value) {
var _overrides = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
} //
}, {
key: "inspect",
value: function inspect(key, overrides, _workspace) {
var defaultValue = overrides.overrideIdentifier ? this._defaultConfiguration.freeze().override(overrides.overrideIdentifier).getValue(key) : this._defaultConfiguration.freeze().getValue(key);
return {
defaultValue: defaultValue,
default: defaultValue !== undefined ? {
value: this._defaultConfiguration.freeze().getValue(key),
override: overrides.overrideIdentifier ? this._defaultConfiguration.freeze().getOverrideValue(key, overrides.overrideIdentifier) : undefined
} : undefined
};
}
}, {
key: "keys",
value: function keys(_workspace) {
return {
default: this._defaultConfiguration.freeze().keys,
user: this._defaultConfiguration.freeze().keys,
workspace: this._defaultConfiguration.freeze().keys,
workspaceFolder: this._defaultConfiguration.freeze().keys
};
}
}, {
key: "updateDefaultConfiguration",
value: function updateDefaultConfiguration(defaultConfiguration) {
this._defaultConfiguration = defaultConfiguration;
this._workspaceConsolidatedConfiguration = null;
this._foldersConsolidatedConfigurations.clear();
}
}, {
key: "updateLocalUserConfiguration",
value: function updateLocalUserConfiguration(localUserConfiguration) {
this._localUserConfiguration = localUserConfiguration;
this._userConfiguration = null;
this._workspaceConsolidatedConfiguration = null;
this._foldersConsolidatedConfigurations.clear();
}
}, {
key: "updateRemoteUserConfiguration",
value: function updateRemoteUserConfiguration(remoteUserConfiguration) {
this._remoteUserConfiguration = remoteUserConfiguration;
this._userConfiguration = null;
this._workspaceConsolidatedConfiguration = null;
this._foldersConsolidatedConfigurations.clear();
}
}, {
key: "updateWorkspaceConfiguration",
value: function updateWorkspaceConfiguration(workspaceConfiguration) {
this._workspaceConfiguration = workspaceConfiguration;
this._workspaceConsolidatedConfiguration = null;
this._foldersConsolidatedConfigurations.clear();
}
}, {
key: "compareAndUpdateDefaultConfiguration",
value: function compareAndUpdateDefaultConfiguration(defaults, keys) {
var _this4 = this;
var overrides = keys.filter(function (key) {
return _configurationRegistry.OVERRIDE_PROPERTY_PATTERN.test(key);
}).map(function (key) {
var overrideIdentifier = (0, _configurationRegistry.overrideIdentifierFromKey)(key);
var fromKeys = _this4._defaultConfiguration.getKeysForOverrideIdentifier(overrideIdentifier);
var toKeys = defaults.getKeysForOverrideIdentifier(overrideIdentifier);
var keys = [].concat(_toConsumableArray(toKeys.filter(function (key) {
return fromKeys.indexOf(key) === -1;
})), _toConsumableArray(fromKeys.filter(function (key) {
return toKeys.indexOf(key) === -1;
})), _toConsumableArray(fromKeys.filter(function (key) {
return !_manaCommon.objects.objectEquals(_this4._defaultConfiguration.override(overrideIdentifier).getValue(key), defaults.override(overrideIdentifier).getValue(key));
})));
return [overrideIdentifier, keys];
});
this.updateDefaultConfiguration(defaults);
return {
keys: keys,
overrides: overrides
};
}
}, {
key: "compareAndUpdateLocalUserConfiguration",
value: function compareAndUpdateLocalUserConfiguration(user) {
var _compare = (0, _configuration.compare)(this.localUserConfiguration, user),
added = _compare.added,
updated = _compare.updated,
removed = _compare.removed,
overrides = _compare.overrides;
var keys = [].concat(_toConsumableArray(added), _toConsumableArray(updated), _toConsumableArray(removed));
if (keys.length) {
this.updateLocalUserConfiguration(user);
}
return {
keys: keys,
overrides: overrides
};
}
}, {
key: "compareAndUpdateRemoteUserConfiguration",
value: function compareAndUpdateRemoteUserConfiguration(user) {
var _compare2 = (0, _configuration.compare)(this.remoteUserConfiguration, user),
added = _compare2.added,
updated = _compare2.updated,
removed = _compare2.removed,
overrides = _compare2.overrides;
var keys = [].concat(_toConsumableArray(added), _toConsumableArray(updated), _toConsumableArray(removed));
if (keys.length) {
this.updateRemoteUserConfiguration(user);
}
return {
keys: keys,
overrides: overrides
};
}
}, {
key: "compareAndUpdateWorkspaceConfiguration",
value: function compareAndUpdateWorkspaceConfiguration(workspaceConfiguration) {
var _compare3 = (0, _configuration.compare)(this.workspaceConfiguration, workspaceConfiguration),
added = _compare3.added,
updated = _compare3.updated,
removed = _compare3.removed,
overrides = _compare3.overrides;
var keys = [].concat(_toConsumableArray(added), _toConsumableArray(updated), _toConsumableArray(removed));
if (keys.length) {
this.updateWorkspaceConfiguration(workspaceConfiguration);
}
return {
keys: keys,
overrides: overrides
};
}
}, {
key: "compareAndUpdateFolderConfiguration",
value: function compareAndUpdateFolderConfiguration(_resource, _folderConfiguration) {
return {
keys: [],
overrides: []
};
}
}, {
key: "compareAndDeleteFolderConfiguration",
value: function compareAndDeleteFolderConfiguration(_folder) {
return {
keys: [],
overrides: []
};
}
}, {
key: "defaults",
get: function get() {
return this._defaultConfiguration;
}
}, {
key: "userConfiguration",
get: function get() {
if (!this._userConfiguration) {
this._userConfiguration = this._remoteUserConfiguration.isEmpty() ? this._localUserConfiguration : this._localUserConfiguration.merge(this._remoteUserConfiguration);
if (this._freeze) {
this._userConfiguration.freeze();
}
}
return this._userConfiguration;
}
}, {
key: "localUserConfiguration",
get: function get() {
return this._localUserConfiguration;
}
}, {
key: "remoteUserConfiguration",
get: function get() {
return this._remoteUserConfiguration;
}
}, {
key: "workspaceConfiguration",
get: function get() {
return this._workspaceConfiguration;
}
}, {
key: "getConsolidateConfigurationModel",
value: function getConsolidateConfigurationModel(overrides, workspace) {
var configurationModel = this.getConsolidatedConfigurationModelForResource(overrides, workspace);
return overrides.overrideIdentifier ? configurationModel.override(overrides.overrideIdentifier) : configurationModel;
}
}, {
key: "getConsolidatedConfigurationModelForResource",
value: function getConsolidatedConfigurationModelForResource(_ref, _workspace) {
var _resource = _ref.resource;
var consolidateConfiguration = this.getWorkspaceConsolidatedConfiguration();
return consolidateConfiguration;
}
}, {
key: "getWorkspaceConsolidatedConfiguration",
value: function getWorkspaceConsolidatedConfiguration() {
if (!this._workspaceConsolidatedConfiguration) {
this._workspaceConsolidatedConfiguration = this._defaultConfiguration.merge(this.userConfiguration, this._workspaceConfiguration, this._memoryConfiguration);
if (this._freeze) {
this._workspaceConfiguration = this._workspaceConfiguration.freeze();
}
}
return this._workspaceConsolidatedConfiguration;
}
}, {
key: "toData",
value: function toData() {
return {
defaults: {
contents: this._defaultConfiguration.contents,
overrides: this._defaultConfiguration.overrides,
keys: this._defaultConfiguration.keys
},
user: {
contents: this.userConfiguration.contents,
overrides: this.userConfiguration.overrides,
keys: this.userConfiguration.keys
},
workspace: {
contents: this._workspaceConfiguration.contents,
overrides: this._workspaceConfiguration.overrides,
keys: this._workspaceConfiguration.keys
}
};
}
}, {
key: "allKeys",
value: function allKeys() {
var keys = new Set();
this._defaultConfiguration.freeze().keys.forEach(function (key) {
return keys.add(key);
});
this.userConfiguration.freeze().keys.forEach(function (key) {
return keys.add(key);
});
this._workspaceConfiguration.freeze().keys.forEach(function (key) {
return keys.add(key);
});
return _toConsumableArray(keys.values());
}
}, {
key: "getAllKeysForOverrideIdentifier",
value: function getAllKeysForOverrideIdentifier(overrideIdentifier) {
var keys = new Set();
this._defaultConfiguration.getKeysForOverrideIdentifier(overrideIdentifier).forEach(function (key) {
return keys.add(key);
});
this.userConfiguration.getKeysForOverrideIdentifier(overrideIdentifier).forEach(function (key) {
return keys.add(key);
});
this._workspaceConfiguration.getKeysForOverrideIdentifier(overrideIdentifier).forEach(function (key) {
return keys.add(key);
});
return _toConsumableArray(keys.values());
}
}], [{
key: "parse",
value: function parse(data) {
var defaultConfiguration = this.parseConfigurationModel(data.defaults);
var userConfiguration = this.parseConfigurationModel(data.user);
var workspaceConfiguration = this.parseConfigurationModel(data.workspace);
return new Configuration(defaultConfiguration, userConfiguration, new ConfigurationModel(), workspaceConfiguration, new ConfigurationModel(), new _utils.ResourceMap(), false);
}
}, {
key: "parseConfigurationModel",
value: function parseConfigurationModel(model) {
return new ConfigurationModel(model.contents, model.keys, model.overrides).freeze();
}
}]);
return Configuration;
}();
function mergeChanges() {
for (var _len2 = arguments.length, changes = new Array(_len2), _key3 = 0; _key3 < _len2; _key3++) {
changes[_key3] = arguments[_key3];
}
if (changes.length === 0) {
return {
keys: [],
overrides: []
};
}
if (changes.length === 1) {
return changes[0];
}
var keysSet = new Set();
var overridesMap = new Map();
for (var _i4 = 0, _changes = changes; _i4 < _changes.length; _i4++) {
var change = _changes[_i4];
change.keys.forEach(function (key) {
return keysSet.add(key);
});
change.overrides.forEach(function (_ref2) {
var _ref3 = _slicedToArray(_ref2, 2),
identifier = _ref3[0],
keys = _ref3[1];
var result = (0, _utils.getOrSet)(overridesMap, identifier, new Set());
keys.forEach(function (key) {
return result.add(key);
});
});
}
var overrides = [];
overridesMap.forEach(function (keys, identifier) {
return overrides.push([identifier, _toConsumableArray(keys.values())]);
});
return {
keys: _toConsumableArray(keysSet.values()),
overrides: overrides
};
}
var ConfigurationChangeEvent = exports.ConfigurationChangeEvent = /*#__PURE__*/function () {
function ConfigurationChangeEvent(change, previous, currentConfiguraiton, currentWorkspace) {
_classCallCheck(this, ConfigurationChangeEvent);
this._previousConfiguration = undefined;
this.change = change;
this.previous = previous;
this.currentConfiguraiton = currentConfiguraiton;
this.currentWorkspace = currentWorkspace;
var keysSet = new Set();
change.keys.forEach(function (key) {
return keysSet.add(key);
});
change.overrides.forEach(function (_ref4) {
var _ref5 = _slicedToArray(_ref4, 2