UNPKG

@backstage/backend-test-utils

Version:

Test helpers library for Backstage backends

127 lines (121 loc) 3.36 kB
'use strict'; var config = require('@backstage/config'); var isEqual = require('lodash/isEqual'); function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e : { default: e }; } var isEqual__default = /*#__PURE__*/_interopDefaultCompat(isEqual); class ObservableConfigProxy { config = new config.ConfigReader({}); subscribers = []; static create(abortController) { return new ObservableConfigProxy(void 0, void 0, abortController); } parent; parentKey; abortController; constructor(parent, parentKey, abortController) { this.parent = parent; this.parentKey = parentKey; this.abortController = abortController; if (parent && !parentKey) { throw new Error("parentKey is required if parent is set"); } } setConfig(config) { if (this.parent) { throw new Error("immutable"); } const changed = !isEqual__default.default(this.config.get(), config.get()); this.config = config; if (changed) { for (const subscriber of this.subscribers) { try { subscriber(); } catch (error) { console.error(`Config subscriber threw error, ${error}`); } } } } close() { if (!this.abortController) { throw new Error("Only the root config can be closed"); } this.abortController.abort(); } subscribe(onChange) { if (this.parent) { return this.parent.subscribe(onChange); } this.subscribers.push(onChange); return { unsubscribe: () => { const index = this.subscribers.indexOf(onChange); if (index >= 0) { this.subscribers.splice(index, 1); } } }; } select(required) { if (this.parent && this.parentKey) { if (required) { return this.parent.select(true).getConfig(this.parentKey); } return this.parent.select(false)?.getOptionalConfig(this.parentKey); } return this.config; } has(key) { return this.select(false)?.has(key) ?? false; } keys() { return this.select(false)?.keys() ?? []; } get(key) { return this.select(true).get(key); } getOptional(key) { return this.select(false)?.getOptional(key); } getConfig(key) { return new ObservableConfigProxy(this, key); } getOptionalConfig(key) { if (this.select(false)?.has(key)) { return new ObservableConfigProxy(this, key); } return void 0; } getConfigArray(key) { return this.select(true).getConfigArray(key); } getOptionalConfigArray(key) { return this.select(false)?.getOptionalConfigArray(key); } getNumber(key) { return this.select(true).getNumber(key); } getOptionalNumber(key) { return this.select(false)?.getOptionalNumber(key); } getBoolean(key) { return this.select(true).getBoolean(key); } getOptionalBoolean(key) { return this.select(false)?.getOptionalBoolean(key); } getString(key) { return this.select(true).getString(key); } getOptionalString(key) { return this.select(false)?.getOptionalString(key); } getStringArray(key) { return this.select(true).getStringArray(key); } getOptionalStringArray(key) { return this.select(false)?.getOptionalStringArray(key); } } exports.ObservableConfigProxy = ObservableConfigProxy; //# sourceMappingURL=ObservableConfigProxy.cjs.js.map