forked from microsoft/vscode-pull-request-github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvslshost.ts
86 lines (76 loc) · 2.78 KB
/
vslshost.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
/*---------------------------------------------------------------------------------------------
* 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 { LiveShare, SharedService } from 'vsls/vscode.js';
import { API } from '../api/api';
import {
VSLS_GIT_PR_SESSION_NAME,
VSLS_REPOSITORY_INITIALIZATION_NAME,
VSLS_REQUEST_NAME,
VSLS_STATE_CHANGE_NOTIFY_NAME,
} from '../constants';
export class VSLSHost implements vscode.Disposable {
private _sharedService?: SharedService;
private _disposables: vscode.Disposable[];
constructor(private _liveShareAPI: LiveShare, private _api: API) {
this._disposables = [];
}
public async initialize() {
this._sharedService = (await this._liveShareAPI!.shareService(VSLS_GIT_PR_SESSION_NAME)) || undefined;
if (this._sharedService) {
this._sharedService.onRequest(VSLS_REQUEST_NAME, this._gitHandler.bind(this));
}
}
private async _gitHandler(args: any[]) {
const type = args[0];
const workspaceFolderPath = args[1];
const workspaceFolderUri = vscode.Uri.parse(workspaceFolderPath);
const localWorkSpaceFolderUri = this._liveShareAPI.convertSharedUriToLocal(workspaceFolderUri);
const gitProvider = this._api.getGitProvider(localWorkSpaceFolderUri);
if (!gitProvider) {
return;
}
const localRepository: any = gitProvider.repositories.filter(
repository => repository.rootUri.toString() === localWorkSpaceFolderUri.toString(),
)[0];
if (localRepository) {
const commandArgs = args.slice(2);
if (type === VSLS_REPOSITORY_INITIALIZATION_NAME) {
this._disposables.push(
localRepository.state.onDidChange(_ => {
this._sharedService!.notify(VSLS_STATE_CHANGE_NOTIFY_NAME, {
HEAD: localRepository.state.HEAD,
remotes: localRepository.state.remotes,
refs: localRepository.state.refs,
});
}),
);
return {
HEAD: localRepository.state.HEAD,
remotes: localRepository.state.remotes,
refs: localRepository.state.refs,
rootUri: workspaceFolderUri.toString(), // file: --> vsls:/
};
}
if (type === 'show') {
const path = commandArgs[1];
const vslsFileUri = workspaceFolderUri.with({ path: path });
const localFileUri = this._liveShareAPI.convertSharedUriToLocal(vslsFileUri);
commandArgs[1] = localFileUri.fsPath;
return localRepository[type](...commandArgs);
}
if (localRepository[type]) {
return localRepository[type](...commandArgs);
}
} else {
return null;
}
}
public dispose() {
this._disposables.forEach(d => d.dispose());
this._sharedService = undefined;
this._disposables = [];
}
}