UNPKG

5.31 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 WITH Classpath-exception-2.0
16// *****************************************************************************
17var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
18 var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
19 if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
20 else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
21 return c > 3 && r && Object.defineProperty(target, key, r), r;
22};
23var __metadata = (this && this.__metadata) || function (k, v) {
24 if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
25};
26Object.defineProperty(exports, "__esModule", { value: true });
27exports.DefaultJsonSchemaContribution = exports.JsonSchemaStore = exports.JsonSchemaContribution = void 0;
28const inversify_1 = require("inversify");
29const contribution_provider_1 = require("../common/contribution-provider");
30const endpoint_1 = require("./endpoint");
31const promise_util_1 = require("../common/promise-util");
32const request_1 = require("@theia/request");
33exports.JsonSchemaContribution = Symbol('JsonSchemaContribution');
34let JsonSchemaStore = class JsonSchemaStore {
35 constructor() {
36 this._schemas = new promise_util_1.Deferred();
37 }
38 get schemas() {
39 return this._schemas.promise;
40 }
41 onStart() {
42 const pendingRegistrations = [];
43 const schemas = [];
44 const freeze = () => {
45 Object.freeze(schemas);
46 this._schemas.resolve(schemas);
47 };
48 const registerTimeout = this.getRegisterTimeout();
49 const frozenErrorCode = 'JsonSchemaRegisterContext.frozen';
50 const context = {
51 registerSchema: schema => {
52 if (Object.isFrozen(schemas)) {
53 throw new Error(frozenErrorCode);
54 }
55 schemas.push(schema);
56 }
57 };
58 for (const contribution of this.contributions.getContributions()) {
59 const result = contribution.registerSchemas(context);
60 if (result) {
61 pendingRegistrations.push(result.then(() => { }, e => {
62 if (e instanceof Error && e.message === frozenErrorCode) {
63 console.error(`${contribution.constructor.name}.registerSchemas is taking more than ${registerTimeout.toFixed(1)} ms, new schemas are ignored.`);
64 }
65 else {
66 console.error(e);
67 }
68 }));
69 }
70 }
71 if (pendingRegistrations.length) {
72 let pending = Promise.all(pendingRegistrations).then(() => { });
73 if (registerTimeout) {
74 pending = Promise.race([pending, (0, promise_util_1.timeout)(registerTimeout)]);
75 }
76 pending.then(freeze);
77 }
78 else {
79 freeze();
80 }
81 }
82 getRegisterTimeout() {
83 return 500;
84 }
85};
86__decorate([
87 (0, inversify_1.inject)(contribution_provider_1.ContributionProvider),
88 (0, inversify_1.named)(exports.JsonSchemaContribution),
89 __metadata("design:type", Object)
90], JsonSchemaStore.prototype, "contributions", void 0);
91JsonSchemaStore = __decorate([
92 (0, inversify_1.injectable)()
93], JsonSchemaStore);
94exports.JsonSchemaStore = JsonSchemaStore;
95let DefaultJsonSchemaContribution = class DefaultJsonSchemaContribution {
96 async registerSchemas(context) {
97 const url = `${new endpoint_1.Endpoint().httpScheme}//schemastore.azurewebsites.net/api/json/catalog.json`;
98 const response = await this.requestService.request({ url });
99 const schemas = request_1.RequestContext.asJson(response).schemas;
100 for (const s of schemas) {
101 if (s.fileMatch) {
102 context.registerSchema({
103 fileMatch: s.fileMatch,
104 url: s.url
105 });
106 }
107 }
108 }
109};
110__decorate([
111 (0, inversify_1.inject)(request_1.RequestService),
112 __metadata("design:type", Object)
113], DefaultJsonSchemaContribution.prototype, "requestService", void 0);
114DefaultJsonSchemaContribution = __decorate([
115 (0, inversify_1.injectable)()
116], DefaultJsonSchemaContribution);
117exports.DefaultJsonSchemaContribution = DefaultJsonSchemaContribution;
118//# sourceMappingURL=json-schema-store.js.map
\No newline at end of file