UNPKG

4.29 kBJavaScriptView Raw
1"use strict";
2// *****************************************************************************
3// Copyright (C) 2017 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// *****************************************************************************
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};
26var __param = (this && this.__param) || function (paramIndex, decorator) {
27 return function (target, key) { decorator(target, key, paramIndex); }
28};
29Object.defineProperty(exports, "__esModule", { value: true });
30exports.DefaultOpenerService = exports.open = exports.OpenerService = exports.OpenHandler = void 0;
31const inversify_1 = require("inversify");
32const common_1 = require("../common");
33exports.OpenHandler = Symbol('OpenHandler');
34exports.OpenerService = Symbol('OpenerService');
35async function open(openerService, uri, options) {
36 const opener = await openerService.getOpener(uri, options);
37 return opener.open(uri, options);
38}
39exports.open = open;
40let DefaultOpenerService = class DefaultOpenerService {
41 constructor(handlersProvider) {
42 this.handlersProvider = handlersProvider;
43 // Collection of open-handlers for custom-editor contributions.
44 this.customEditorOpenHandlers = [];
45 this.onDidChangeOpenersEmitter = new common_1.Emitter();
46 this.onDidChangeOpeners = this.onDidChangeOpenersEmitter.event;
47 }
48 addHandler(openHandler) {
49 this.customEditorOpenHandlers.push(openHandler);
50 this.onDidChangeOpenersEmitter.fire();
51 return common_1.Disposable.create(() => {
52 this.customEditorOpenHandlers.splice(this.customEditorOpenHandlers.indexOf(openHandler), 1);
53 this.onDidChangeOpenersEmitter.fire();
54 });
55 }
56 async getOpener(uri, options) {
57 const handlers = await this.prioritize(uri, options);
58 if (handlers.length >= 1) {
59 return handlers[0];
60 }
61 return Promise.reject(new Error(`There is no opener for ${uri}.`));
62 }
63 async getOpeners(uri, options) {
64 return uri ? this.prioritize(uri, options) : this.getHandlers();
65 }
66 async prioritize(uri, options) {
67 const prioritized = await common_1.Prioritizeable.prioritizeAll(this.getHandlers(), async (handler) => {
68 try {
69 return await handler.canHandle(uri, options);
70 }
71 catch {
72 return 0;
73 }
74 });
75 return prioritized.map(p => p.value);
76 }
77 getHandlers() {
78 return [
79 ...this.handlersProvider.getContributions(),
80 ...this.customEditorOpenHandlers
81 ];
82 }
83};
84DefaultOpenerService = __decorate([
85 (0, inversify_1.injectable)(),
86 __param(0, (0, inversify_1.inject)(common_1.ContributionProvider)),
87 __param(0, (0, inversify_1.named)(exports.OpenHandler)),
88 __metadata("design:paramtypes", [Object])
89], DefaultOpenerService);
90exports.DefaultOpenerService = DefaultOpenerService;
91//# sourceMappingURL=opener-service.js.map
\No newline at end of file