UNPKG

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