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
100 lines (80 loc) · 2.98 KB
/
Copy pathapi1.ts
File metadata and controls
100 lines (80 loc) · 2.98 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*---------------------------------------------------------------------------------------------
* 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';
import { APIState } from '../typings/git';
export class GitApiImpl implements API, IGit, vscode.Disposable {
private static _handlePool: number = 0;
private _providers = new Map<number, IGit>();
public get repositories(): Repository[] {
const ret: Repository[] = [];
this._providers.forEach(provider => {
if (provider.repositories) {
ret.push(...provider.repositories);
}
});
return ret;
}
public get state(): APIState | undefined {
let state: APIState | undefined;
this._providers.forEach(provider => {
if (provider.state) {
state = provider.state;
}
});
return state;
}
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 _onDidChangeState = new vscode.EventEmitter<APIState>();
readonly onDidChangeState: vscode.Event<APIState> = this._onDidChangeState.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)));
if (provider.onDidChangeState) {
this._disposables.push(provider.onDidChangeState(e => this._onDidChangeState.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 {
const foldersMap = TernarySearchTree.forPaths<IGit>();
this._providers.forEach(provider => {
if (provider.repositories) {
const repositories = provider.repositories;
for (const repository of repositories) {
foldersMap.set(repository.rootUri.toString(), provider);
}
}
});
return foldersMap.findSubstr(uri.toString());
}
private _nextHandle(): number {
return GitApiImpl._handlePool++;
}
dispose() {
this._disposables.forEach(disposable => disposable.dispose());
}
}