1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | var __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 | };
|
23 | var __metadata = (this && this.__metadata) || function (k, v) {
|
24 | if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
25 | };
|
26 | Object.defineProperty(exports, "__esModule", { value: true });
|
27 | exports.DefaultJsonSchemaContribution = exports.JsonSchemaStore = exports.JsonSchemaContribution = void 0;
|
28 | const inversify_1 = require("inversify");
|
29 | const contribution_provider_1 = require("../common/contribution-provider");
|
30 | const endpoint_1 = require("./endpoint");
|
31 | const promise_util_1 = require("../common/promise-util");
|
32 | const request_1 = require("@theia/request");
|
33 | exports.JsonSchemaContribution = Symbol('JsonSchemaContribution');
|
34 | let 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);
|
91 | JsonSchemaStore = __decorate([
|
92 | (0, inversify_1.injectable)()
|
93 | ], JsonSchemaStore);
|
94 | exports.JsonSchemaStore = JsonSchemaStore;
|
95 | let DefaultJsonSchemaContribution = class DefaultJsonSchemaContribution {
|
96 | constructor() {
|
97 | this.jsonSchemaUrl = `${new endpoint_1.Endpoint().httpScheme}//schemastore.org/api/json/catalog.json`;
|
98 | }
|
99 | async registerSchemas(context) {
|
100 | const response = await this.requestService.request({ url: this.jsonSchemaUrl });
|
101 | const schemas = request_1.RequestContext.asJson(response).schemas;
|
102 | for (const s of schemas) {
|
103 | if (s.fileMatch) {
|
104 | context.registerSchema({
|
105 | fileMatch: s.fileMatch,
|
106 | url: s.url
|
107 | });
|
108 | }
|
109 | }
|
110 | }
|
111 | };
|
112 | __decorate([
|
113 | (0, inversify_1.inject)(request_1.RequestService),
|
114 | __metadata("design:type", Object)
|
115 | ], DefaultJsonSchemaContribution.prototype, "requestService", void 0);
|
116 | DefaultJsonSchemaContribution = __decorate([
|
117 | (0, inversify_1.injectable)()
|
118 | ], DefaultJsonSchemaContribution);
|
119 | exports.DefaultJsonSchemaContribution = DefaultJsonSchemaContribution;
|
120 |
|
\ | No newline at end of file |