forked from microsoft/vscode-pull-request-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvslsguest.ts
More file actions
151 lines (128 loc) · 4.82 KB
/
Copy pathvslsguest.ts
File metadata and controls
151 lines (128 loc) · 4.82 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*---------------------------------------------------------------------------------------------
* 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, SharedServiceProxy } from 'vsls/vscode.js';
import { Model } from './model';
import { VSLS_GIT_PR_SESSION_NAME, VSLS_REQUEST_NAME, VSLS_REPOSITORY_INITIALIZATION_NAME, VSLS_STATE_CHANGE_NOFITY_NAME } from '../constants';
import { RepositoryState, Commit, Branch, Ref, Remote, Submodule, Change } from '../typings/git';
export class VSLSGuest implements vscode.Disposable {
private _sharedServiceProxy?: SharedServiceProxy;
private _disposables: vscode.Disposable[];
constructor(private _api: LiveShare, private _model: Model) {
this._disposables = [];
}
public async initialize() {
this._sharedServiceProxy = await this._api.getSharedService(VSLS_GIT_PR_SESSION_NAME) || undefined;
if (!this._sharedServiceProxy) {
return;
}
if (this._sharedServiceProxy.isServiceAvailable) {
await this._refreshWorkspaces(true);
}
this._disposables.push(this._sharedServiceProxy.onDidChangeIsServiceAvailable(async e => {
await this._refreshWorkspaces(e);
}));
this._disposables.push(vscode.workspace.onDidChangeWorkspaceFolders(this._onDidChangeWorkspaceFolders.bind(this)));
}
private async _onDidChangeWorkspaceFolders(e: vscode.WorkspaceFoldersChangeEvent) {
e.added.forEach(async folder => {
if (folder.uri.scheme === 'vsls' && this._sharedServiceProxy && this._sharedServiceProxy.isServiceAvailable) {
await this.openVSLSRepository(folder);
}
});
e.removed.forEach(async folder => {
if (folder.uri.scheme === 'vsls' && this._sharedServiceProxy && this._sharedServiceProxy.isServiceAvailable) {
await this.closeVSLSRepository(folder);
}
});
}
private async _refreshWorkspaces(available: boolean) {
if (vscode.workspace.workspaceFolders) {
vscode.workspace.workspaceFolders.forEach(async (folder) => {
if (folder.uri.scheme === 'vsls') {
if (available) {
await this.openVSLSRepository(folder);
} else {
await this.closeVSLSRepository(folder);
}
}
});
}
}
public async openVSLSRepository(folder: vscode.WorkspaceFolder): Promise<void> {
let existingRepository = this._model.getRepository(folder);
if (existingRepository) {
return;
}
const liveShareRepository = new LiveShareRepository(folder, this._sharedServiceProxy!);
const repositoryProxyHandler = new LiveShareRepositoryProxyHandler();
const repository = new Proxy(liveShareRepository, repositoryProxyHandler);
await repository.initialize();
this._model.openRepository(repository);
}
public async closeVSLSRepository(folder: vscode.WorkspaceFolder): Promise<void> {
let existingRepository = this._model.getRepository(folder);
if (!existingRepository) {
return;
}
this._model.closeRepository(existingRepository);
}
public dispose() {
this._sharedServiceProxy = undefined;
this._disposables.forEach(d => d.dispose());
this._disposables = [];
}
}
class LiveShareRepositoryProxyHandler {
constructor() { }
get (obj: any, prop: any) {
if (prop in obj) {
return obj[prop];
}
return function () {
return obj.proxy.request(VSLS_REQUEST_NAME, [prop, obj.workspaceFolder.uri.toString(), ...arguments]);
};
}
}
class LiveShareRepositoryState implements RepositoryState {
HEAD: Branch | undefined;
refs: Ref[];
remotes: Remote[];
submodules: Submodule[];
rebaseCommit: Commit;
mergeChanges: Change[];
indexChanges: Change[];
workingTreeChanges: Change[];
_onDidChange = new vscode.EventEmitter<void>();
onDidChange = this._onDidChange.event;
constructor(state: RepositoryState) {
this.HEAD = state.HEAD;
this.remotes = state.remotes;
this.refs = state.refs;
}
public update(state: RepositoryState) {
this.HEAD = state.HEAD;
this.remotes = state.remotes;
this.refs = state.refs;
this._onDidChange.fire();
}
}
class LiveShareRepository {
rootUri: vscode.Uri;
state: LiveShareRepositoryState;
constructor(
public workspaceFolder: vscode.WorkspaceFolder,
public proxy: SharedServiceProxy
) { }
public async initialize() {
let result = await this.proxy.request(VSLS_REQUEST_NAME, [VSLS_REPOSITORY_INITIALIZATION_NAME, this.workspaceFolder.uri.toString()]);
this.state = new LiveShareRepositoryState(result);
this.rootUri = vscode.Uri.parse(result.rootUri);
this.proxy.onNotify(VSLS_STATE_CHANGE_NOFITY_NAME, this._notifyHandler.bind(this));
}
private _notifyHandler(args: any) {
this.state.update(args);
}
}