forked from microsoft/vscode-pull-request-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi1.ts
More file actions
82 lines (65 loc) · 2.45 KB
/
Copy pathapi1.ts
File metadata and controls
82 lines (65 loc) · 2.45 KB
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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as vscode from 'vscode';
import { API, IGit, Repository } from './api';
import { TernarySearchTree } from '../common/utils';
export class ApiImpl implements API, IGit, vscode.Disposable {
private static _handlePool: number = 0;
private _providers = new Map<number, IGit>();
public get repositories(): Repository[] {
let ret: Repository[] = [];
this._providers.forEach(provider => {
if (provider.repositories) {
ret.push(...provider.repositories);
}
});
return ret;
}
private _onDidOpenRepository = new vscode.EventEmitter<Repository>();
readonly onDidOpenRepository: vscode.Event<Repository> = this._onDidOpenRepository.event;
private _onDidCloseRepository = new vscode.EventEmitter<Repository>();
readonly onDidCloseRepository: vscode.Event<Repository> = this._onDidCloseRepository.event;
private _disposables: vscode.Disposable[];
constructor() {
this._disposables = [];
}
registerGitProvider(provider: IGit): vscode.Disposable {
const handler = this._nextHandle();
this._providers.set(handler, provider);
this._disposables.push(provider.onDidCloseRepository(e => this._onDidCloseRepository.fire(e)));
this._disposables.push(provider.onDidOpenRepository(e => this._onDidOpenRepository.fire(e)));
provider.repositories.forEach(repository => {
this._onDidOpenRepository.fire(repository);
});
return {
dispose: () => {
if (provider && provider.repositories) {
provider.repositories.forEach(repository => {
this._onDidCloseRepository.fire(repository);
});
}
this._providers.delete(handler);
}
};
}
getGitProvider(uri: vscode.Uri): IGit | undefined {
let foldersMap = TernarySearchTree.forPaths<IGit>();
this._providers.forEach(provider => {
if (provider.repositories) {
let repositories = provider.repositories;
for (const repository of repositories) {
foldersMap.set(repository.rootUri.toString(), provider);
}
}
});
return foldersMap.findSubstr(uri.toString());
}
private _nextHandle(): number {
return ApiImpl._handlePool++;
}
dispose() {
this._disposables.forEach(disposable => disposable.dispose());
}
}