@theia/core
Version:
Theia is a cloud & desktop IDE framework implemented in TypeScript.
143 lines • 5.45 kB
JavaScript
// *****************************************************************************
// Copyright (C) 2018 TypeFox 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
// *****************************************************************************
var DefaultJsonSchemaContribution_1;
import { __decorate } from "tslib";
import { injectable, inject, named } from 'inversify';
import { ContributionProvider } from '../common/contribution-provider.js';
import { Emitter } from '../common/index.js';
import { timeout, Deferred } from '../common/promise-util.js';
import { ILogger } from '../common/logger.js';
export const JsonSchemaContribution = Symbol('JsonSchemaContribution');
let JsonSchemaStore = class JsonSchemaStore {
constructor() {
this._schemas = new Deferred();
}
get schemas() {
return this._schemas.promise;
}
onStart() {
const pendingRegistrations = [];
const schemas = [];
const freeze = () => {
Object.freeze(schemas);
this._schemas.resolve(schemas);
};
const registerTimeout = this.getRegisterTimeout();
const frozenErrorCode = 'JsonSchemaRegisterContext.frozen';
const context = {
registerSchema: schema => {
if (Object.isFrozen(schemas)) {
throw new Error(frozenErrorCode);
}
schemas.push(schema);
}
};
for (const contribution of this.contributions.getContributions()) {
const result = contribution.registerSchemas(context);
if (result) {
pendingRegistrations.push(result.then(() => { }, e => {
if (e instanceof Error && e.message === frozenErrorCode) {
this.logger.error(`${contribution.constructor.name}.registerSchemas is taking more than ${registerTimeout.toFixed(1)} ms, new schemas are ignored.`);
}
else {
this.logger.error(e);
}
}));
}
}
if (pendingRegistrations.length) {
let pending = Promise.all(pendingRegistrations).then(() => { });
if (registerTimeout) {
pending = Promise.race([pending, timeout(registerTimeout)]);
}
pending.then(freeze);
}
else {
freeze();
}
}
getRegisterTimeout() {
return 500;
}
};
__decorate([
inject(ContributionProvider),
named(JsonSchemaContribution)
], JsonSchemaStore.prototype, "contributions", void 0);
__decorate([
inject(ILogger),
named('core:JsonSchemaStore')
], JsonSchemaStore.prototype, "logger", void 0);
JsonSchemaStore = __decorate([
injectable()
], JsonSchemaStore);
export { JsonSchemaStore };
let JsonSchemaDataStore = class JsonSchemaDataStore {
constructor() {
this._schemas = new Map();
this.onDidSchemaUpdateEmitter = new Emitter();
this.onDidSchemaUpdate = this.onDidSchemaUpdateEmitter.event;
}
hasSchema(uri) {
return this._schemas.has(uri.toString());
}
getSchema(uri) {
return this._schemas.get(uri.toString());
}
setSchema(uri, schema) {
this._schemas.set(uri.toString(), typeof schema === 'string' ? schema : JSON.stringify(schema));
this.notifySchemaUpdate(uri);
}
deleteSchema(uri) {
if (this._schemas.delete(uri.toString())) {
this.notifySchemaUpdate(uri);
}
}
notifySchemaUpdate(uri) {
this.onDidSchemaUpdateEmitter.fire(uri);
}
};
JsonSchemaDataStore = __decorate([
injectable()
], JsonSchemaDataStore);
export { JsonSchemaDataStore };
let DefaultJsonSchemaContribution = class DefaultJsonSchemaContribution {
static { DefaultJsonSchemaContribution_1 = this; }
static { this.excludedSchemaUrls = [
'https://www.schemastore.org/task.json'
]; }
async registerSchemas(context) {
// `catalog.json` is downloaded into `lib` when the package is installed, so it is not known to the compiler.
// @ts-expect-error TS2307
const catalog = (await import('./catalog.json', { with: { type: 'json' } })).default;
for (const s of catalog.schemas) {
if (s.fileMatch && this.shouldRegisterSchema(s)) {
context.registerSchema({
fileMatch: s.fileMatch,
url: s.url
});
}
}
}
shouldRegisterSchema(s) {
return !DefaultJsonSchemaContribution_1.excludedSchemaUrls.includes(s.url);
}
};
DefaultJsonSchemaContribution = DefaultJsonSchemaContribution_1 = __decorate([
injectable()
], DefaultJsonSchemaContribution);
export { DefaultJsonSchemaContribution };
//# sourceMappingURL=json-schema-store.js.map