UNPKG

4.72 kBJavaScriptView Raw
1"use strict";
2// *****************************************************************************
3// Copyright (C) 2018 TypeFox and others.
4//
5// This program and the accompanying materials are made available under the
6// terms of the Eclipse Public License v. 2.0 which is available at
7// http://www.eclipse.org/legal/epl-2.0.
8//
9// This Source Code may also be made available under the following Secondary
10// Licenses when the conditions for such availability set forth in the Eclipse
11// Public License v. 2.0 are satisfied: GNU General Public License, version 2
12// with the GNU Classpath Exception which is available at
13// https://www.gnu.org/software/classpath/license.html.
14//
15// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
16// *****************************************************************************
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.DefaultJsonSchemaContribution = exports.JsonSchemaStore = exports.JsonSchemaContribution = void 0;
19const tslib_1 = require("tslib");
20const inversify_1 = require("inversify");
21const contribution_provider_1 = require("../common/contribution-provider");
22const endpoint_1 = require("./endpoint");
23const promise_util_1 = require("../common/promise-util");
24const request_1 = require("@theia/request");
25exports.JsonSchemaContribution = Symbol('JsonSchemaContribution');
26let JsonSchemaStore = class JsonSchemaStore {
27 constructor() {
28 this._schemas = new promise_util_1.Deferred();
29 }
30 get schemas() {
31 return this._schemas.promise;
32 }
33 onStart() {
34 const pendingRegistrations = [];
35 const schemas = [];
36 const freeze = () => {
37 Object.freeze(schemas);
38 this._schemas.resolve(schemas);
39 };
40 const registerTimeout = this.getRegisterTimeout();
41 const frozenErrorCode = 'JsonSchemaRegisterContext.frozen';
42 const context = {
43 registerSchema: schema => {
44 if (Object.isFrozen(schemas)) {
45 throw new Error(frozenErrorCode);
46 }
47 schemas.push(schema);
48 }
49 };
50 for (const contribution of this.contributions.getContributions()) {
51 const result = contribution.registerSchemas(context);
52 if (result) {
53 pendingRegistrations.push(result.then(() => { }, e => {
54 if (e instanceof Error && e.message === frozenErrorCode) {
55 console.error(`${contribution.constructor.name}.registerSchemas is taking more than ${registerTimeout.toFixed(1)} ms, new schemas are ignored.`);
56 }
57 else {
58 console.error(e);
59 }
60 }));
61 }
62 }
63 if (pendingRegistrations.length) {
64 let pending = Promise.all(pendingRegistrations).then(() => { });
65 if (registerTimeout) {
66 pending = Promise.race([pending, (0, promise_util_1.timeout)(registerTimeout)]);
67 }
68 pending.then(freeze);
69 }
70 else {
71 freeze();
72 }
73 }
74 getRegisterTimeout() {
75 return 500;
76 }
77};
78(0, tslib_1.__decorate)([
79 (0, inversify_1.inject)(contribution_provider_1.ContributionProvider),
80 (0, inversify_1.named)(exports.JsonSchemaContribution),
81 (0, tslib_1.__metadata)("design:type", Object)
82], JsonSchemaStore.prototype, "contributions", void 0);
83JsonSchemaStore = (0, tslib_1.__decorate)([
84 (0, inversify_1.injectable)()
85], JsonSchemaStore);
86exports.JsonSchemaStore = JsonSchemaStore;
87let DefaultJsonSchemaContribution = class DefaultJsonSchemaContribution {
88 constructor() {
89 this.jsonSchemaUrl = `${new endpoint_1.Endpoint().httpScheme}//schemastore.org/api/json/catalog.json`;
90 }
91 async registerSchemas(context) {
92 const response = await this.requestService.request({ url: this.jsonSchemaUrl });
93 const schemas = request_1.RequestContext.asJson(response).schemas;
94 for (const s of schemas) {
95 if (s.fileMatch) {
96 context.registerSchema({
97 fileMatch: s.fileMatch,
98 url: s.url
99 });
100 }
101 }
102 }
103};
104(0, tslib_1.__decorate)([
105 (0, inversify_1.inject)(request_1.RequestService),
106 (0, tslib_1.__metadata)("design:type", Object)
107], DefaultJsonSchemaContribution.prototype, "requestService", void 0);
108DefaultJsonSchemaContribution = (0, tslib_1.__decorate)([
109 (0, inversify_1.injectable)()
110], DefaultJsonSchemaContribution);
111exports.DefaultJsonSchemaContribution = DefaultJsonSchemaContribution;
112//# sourceMappingURL=json-schema-store.js.map
\No newline at end of file