UNPKG

@theia/core

Version:

Theia is a cloud & desktop IDE framework implemented in TypeScript.

254 lines • 10.1 kB
// ***************************************************************************** // Copyright (C) 2022 Ericsson and others. // // This program and the accompanying materials are made available under the // terms of the Eclipse Public License v. 2.0 which is available at // http://www.eclipse.org/legal/epl-2.0. // // This Source Code may also be made available under the following Secondary // Licenses when the conditions for such availability set forth in the Eclipse // Public License v. 2.0 are satisfied: GNU General Public License, version 2 // with the GNU Classpath Exception which is available at // https://www.gnu.org/software/classpath/license.html. // // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 // ***************************************************************************** import { __decorate } from "tslib"; import { inject, injectable, postConstruct } from 'inversify'; import { PreferenceProxyOptions } from './preference-proxy.js'; import { PreferenceService } from './preference-service.js'; import { isObject } from '../types.js'; import { DisposableCollection } from '../disposable.js'; import { Emitter } from '../event.js'; import { PreferenceScope } from './preference-scope.js'; export const PreferenceProxySchema = Symbol('PreferenceProxySchema'); export const PreferenceProxyFactory = Symbol('PreferenceProxyFactory'); let InjectablePreferenceProxy = class InjectablePreferenceProxy { constructor() { this.toDispose = new DisposableCollection(); } get prefix() { return this.options.prefix ?? ''; } get style() { return this.options.style ?? 'flat'; } get resourceUri() { return this.options.resourceUri; } get overrideIdentifier() { return this.options.overrideIdentifier; } get isDeep() { const { style } = this; return style === 'deep' || style === 'both'; } get isFlat() { const { style } = this; return style === 'flat' || style === 'both'; } get onPreferenceChangedEmitter() { if (!this._onPreferenceChangedEmitter) { this._onPreferenceChangedEmitter = new Emitter(); this.subscribeToChangeEvents(); this.toDispose.push(this._onPreferenceChangedEmitter); } return this._onPreferenceChangedEmitter; } get onPreferenceChanged() { return this.onPreferenceChangedEmitter.event; } init() { const schema = this.promisedSchema(); if (schema instanceof Promise) { schema.then(resolvedSchema => this.schema = resolvedSchema); } else { this.schema = schema; } } get(target, property, receiver) { // React 19 dev mode calls Object.prototype.toString when handling prop diffs, which // probes Symbol.toStringTag. Throwing should be avoided in this case, it crashes the React DOM. if (property === Symbol.toStringTag) { return undefined; } if (typeof property !== 'string') { throw new Error(`Unexpected property: ${String(property)}`); } const preferenceName = this.prefix + property; if (this.schema && (this.isFlat || !property.includes('.')) && this.schema.properties[preferenceName]) { return this.getValue(preferenceName, undefined); } switch (property) { case 'onPreferenceChanged': return this.onPreferenceChanged; case 'dispose': return this.dispose.bind(this); case 'ready': return Promise.all([this.preferences.ready, this.promisedSchema]).then(() => undefined); case 'get': return this.getValue.bind(this); case 'toJSON': return this.toJSON.bind(this); case 'ownKeys': return this.ownKeys.bind(this); } if (this.schema && this.isDeep) { const prefix = `${preferenceName}.`; if (Object.keys(this.schema.properties).some(key => key.startsWith(prefix))) { const { style, resourceUri, overrideIdentifier } = this; return this.factory(this.schema, { prefix, resourceUri, style, overrideIdentifier }); } let value; // eslint-disable-line @typescript-eslint/no-explicit-any let parentSegment = preferenceName; const segments = []; do { const index = parentSegment.lastIndexOf('.'); segments.push(parentSegment.substring(index + 1)); parentSegment = parentSegment.substring(0, index); if (parentSegment in this.schema.properties) { value = this.get(target, parentSegment, receiver); } } while (parentSegment && value === undefined); let segment; while (isObject(value) && (segment = segments.pop())) { value = value[segment]; } return segments.length ? undefined : value; } } set(target, property, value, receiver) { if (typeof property !== 'string') { throw new Error(`Unexpected property: ${String(property)}`); } const { style, schema, prefix, resourceUri, overrideIdentifier } = this; if (style === 'deep' && property.indexOf('.') !== -1) { return false; } if (schema) { const fullProperty = prefix ? prefix + property : property; if (schema.properties[fullProperty]) { this.preferences.set(fullProperty, value, PreferenceScope.Default); return true; } const newPrefix = fullProperty + '.'; for (const p of Object.keys(schema.properties)) { if (p.startsWith(newPrefix)) { const subProxy = this.factory(schema, { prefix: newPrefix, resourceUri, overrideIdentifier, style }); // eslint-disable-line @typescript-eslint/no-explicit-any const valueAsContainer = value; for (const k of Object.keys(valueAsContainer)) { subProxy[k] = valueAsContainer[k]; } } } } return false; } ownKeys() { const properties = []; if (this.schema) { const { isDeep, isFlat, prefix } = this; for (const property of Object.keys(this.schema.properties)) { if (property.startsWith(prefix)) { const idx = property.indexOf('.', prefix.length); if (idx !== -1 && isDeep) { const pre = property.substring(prefix.length, idx); if (properties.indexOf(pre) === -1) { properties.push(pre); } } const prop = property.substring(prefix.length); if (isFlat || prop.indexOf('.') === -1) { properties.push(prop); } } } } return properties; } getOwnPropertyDescriptor(target, property) { if (typeof property === 'string' && this.ownKeys().includes(property)) { return { enumerable: true, configurable: true }; } return {}; } deleteProperty() { throw new Error('Unsupported operation'); } defineProperty() { throw new Error('Unsupported operation'); } toJSON() { const result = {}; for (const key of this.ownKeys()) { result[key] = this.get(undefined, key, undefined); } return result; } ; subscribeToChangeEvents() { this.toDispose.push(this.preferences.onPreferencesChanged(changes => this.handlePreferenceChanges(changes))); } handlePreferenceChanges(changes) { if (this.schema) { for (const change of changes) { if (this.isRelevantChange(change)) { this.fireChangeEvent(change); } } } } isRelevantChange(change) { const preferenceName = change.preferenceName; return preferenceName.startsWith(this.prefix) && (!this.overrideIdentifier || change.affectedOverrides.includes(this.overrideIdentifier)) && Boolean(this.schema?.properties[preferenceName]); } fireChangeEvent(change) { this.onPreferenceChangedEmitter.fire(change); } getValue(arg, defaultValue, resourceUri) { const preferenceName = typeof arg === 'object' ? arg.preferenceName : arg; const override = this.overrideIdentifier ?? (typeof arg === 'object' ? arg.overrideIdentifier : undefined); const resource = resourceUri ?? this.resourceUri; return this.preferences.get(preferenceName, { fallback: defaultValue, resource, override, }); } dispose() { if (this.options.isDisposable) { this.toDispose.dispose(); } } }; __decorate([ inject(PreferenceProxyOptions) ], InjectablePreferenceProxy.prototype, "options", void 0); __decorate([ inject(PreferenceService) ], InjectablePreferenceProxy.prototype, "preferences", void 0); __decorate([ inject(PreferenceProxySchema) ], InjectablePreferenceProxy.prototype, "promisedSchema", void 0); __decorate([ inject(PreferenceProxyFactory) ], InjectablePreferenceProxy.prototype, "factory", void 0); __decorate([ postConstruct() ], InjectablePreferenceProxy.prototype, "init", null); InjectablePreferenceProxy = __decorate([ injectable() ], InjectablePreferenceProxy); export { InjectablePreferenceProxy }; //# sourceMappingURL=injectable-preference-proxy.js.map