This repository has been archived by the owner on Jul 13, 2018. It is now read-only.
forked from eclipse-theia/theia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
opener-service.ts
107 lines (95 loc) · 3.46 KB
/
opener-service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
* Copyright (C) 2017 TypeFox and others.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*/
import { named, injectable, inject } from "inversify";
import URI from "../common/uri";
import { ContributionProvider, Prioritizeable, MaybePromise } from "../common";
export interface OpenerOptions {
}
export const OpenHandler = Symbol("OpenHandler");
/**
* `OpenHandler` should be implemented to provide a new opener.
*/
export interface OpenHandler {
/**
* A unique id of this handler.
*/
readonly id: string;
/**
* A human-readable name of this handler.
*/
readonly label?: string;
/**
* A css icon class of this handler.
*/
readonly iconClass?: string;
/**
* Test whether this handler can open the given URI for given options.
* Return a positive number if this handler can open; otherwise it cannot.
* Never reject.
*
* A returned value indicating a priorify of this handler.
*/
canHandle(uri: URI, options?: OpenerOptions): MaybePromise<number>;
/**
* Open a widget for the given URI and options.
* Resolve to an opened widget or undefined, e.g. if a page is opened.
* Never reject if `canHandle` return a positive number; otherwise should reject.
*/
open(uri: URI, options?: OpenerOptions): MaybePromise<object | undefined>;
}
export const OpenerService = Symbol("OpenerService");
/**
* `OpenerService` provide an access to existing openers.
*/
export interface OpenerService {
/**
* Return all registered openers.
* Never reject.
*/
getOpeners(): Promise<OpenHandler[]>;
/**
* Return all openers able to open the given URI for given options
* ordered according their priority.
* Never reject.
*/
getOpeners(uri: URI, options?: OpenerOptions): Promise<OpenHandler[]>;
/**
* Return an opener with the higher priority for the given URI.
* Reject if such does not exist.
*/
getOpener(uri: URI, options?: OpenerOptions): Promise<OpenHandler>;
}
export async function open(openerService: OpenerService, uri: URI, options?: OpenerOptions): Promise<object | undefined> {
const opener = await openerService.getOpener(uri);
return await opener.open(uri, options);
}
@injectable()
export class DefaultOpenerService implements OpenerService {
constructor(
@inject(ContributionProvider) @named(OpenHandler)
protected readonly handlersProvider: ContributionProvider<OpenHandler>
) { }
async getOpener(uri: URI, options?: OpenerOptions): Promise<OpenHandler> {
const handlers = await this.prioritize(uri, options);
if (handlers.length >= 1) {
return handlers[0];
}
return Promise.reject(`There is no opener for ${uri}.`);
}
async getOpeners(uri?: URI, options?: OpenerOptions): Promise<OpenHandler[]> {
return uri ? this.prioritize(uri, options) : this.getHandlers();
}
protected async prioritize(uri: URI, options?: OpenerOptions): Promise<OpenHandler[]> {
const prioritized = await Prioritizeable.prioritizeAll(this.getHandlers(), handler =>
handler.canHandle(uri, options)
);
return prioritized.map(p => p.value);
}
protected getHandlers(): OpenHandler[] {
return this.handlersProvider.getContributions();
}
}