1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | Object.defineProperty(exports, "__esModule", { value: true });
|
18 | exports.DefaultJsonSchemaContribution = exports.JsonSchemaStore = exports.JsonSchemaContribution = void 0;
|
19 | const tslib_1 = require("tslib");
|
20 | const inversify_1 = require("inversify");
|
21 | const contribution_provider_1 = require("../common/contribution-provider");
|
22 | const endpoint_1 = require("./endpoint");
|
23 | const promise_util_1 = require("../common/promise-util");
|
24 | const request_1 = require("@theia/request");
|
25 | exports.JsonSchemaContribution = Symbol('JsonSchemaContribution');
|
26 | let 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);
|
83 | JsonSchemaStore = (0, tslib_1.__decorate)([
|
84 | (0, inversify_1.injectable)()
|
85 | ], JsonSchemaStore);
|
86 | exports.JsonSchemaStore = JsonSchemaStore;
|
87 | let 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);
|
108 | DefaultJsonSchemaContribution = (0, tslib_1.__decorate)([
|
109 | (0, inversify_1.injectable)()
|
110 | ], DefaultJsonSchemaContribution);
|
111 | exports.DefaultJsonSchemaContribution = DefaultJsonSchemaContribution;
|
112 |
|
\ | No newline at end of file |