MainThreadSCMProvider.$onChange

This commit is contained in:
Joao Moreno
2016-11-30 11:24:41 +01:00
parent 867f12a69c
commit 06b160e34e
5 changed files with 87 additions and 29 deletions

View File

@@ -6,29 +6,30 @@
import URI from 'vs/base/common/uri';
import { TPromise } from 'vs/base/common/winjs.base';
import Event, { Emitter } from 'vs/base/common/event';
import Event, { Emitter, debounceEvent } from 'vs/base/common/event';
import { index } from 'vs/base/common/arrays';
import { asWinJsPromise } from 'vs/base/common/async';
import { IThreadService } from 'vs/workbench/services/thread/common/threadService';
import { SCMProvider } from 'vscode';
import { Disposable } from 'vs/workbench/api/node/extHostTypes';
import { MainContext, MainThreadSCMShape } from './extHost.protocol';
import { MainContext, MainThreadSCMShape, SCMRawResource } from './extHost.protocol';
import * as vscode from 'vscode';
export class ExtHostSCM {
private _proxy: MainThreadSCMShape;
private _providers: { [id: string]: SCMProvider; } = Object.create(null);
private _providers: { [id: string]: vscode.SCMProvider; } = Object.create(null);
private _onDidChangeActiveProvider = new Emitter<SCMProvider>();
get onDidChangeActiveProvider(): Event<SCMProvider> { return this._onDidChangeActiveProvider.event; }
private _onDidChangeActiveProvider = new Emitter<vscode.SCMProvider>();
get onDidChangeActiveProvider(): Event<vscode.SCMProvider> { return this._onDidChangeActiveProvider.event; }
private _activeProvider: SCMProvider;
get activeProvider(): SCMProvider | undefined { return this._activeProvider; }
private _activeProvider: vscode.SCMProvider;
get activeProvider(): vscode.SCMProvider | undefined { return this._activeProvider; }
constructor(threadService: IThreadService) {
this._proxy = threadService.get(MainContext.MainThreadSCM);
}
registerSCMProvider(id: string, provider: SCMProvider): Disposable {
registerSCMProvider(id: string, provider: vscode.SCMProvider): Disposable {
if (this._providers[id]) {
throw new Error(`Provider ${id} already registered`);
}
@@ -36,14 +37,37 @@ export class ExtHostSCM {
// TODO@joao: should pluck all the things out of the provider
this._providers[id] = provider;
const resourceGroupsIds = provider.resourceGroups.map(g => g.id);
this._proxy.$register(id, {
commitCommand: provider.commitCommand,
clickCommand: provider.clickCommand,
dragCommand: provider.dragCommand,
resourceGroups: provider.resourceGroups,
supportsOriginalResource: !!provider.getOriginalResource
});
const onDidChange = debounceEvent<vscode.SCMResource[], vscode.SCMResource[]>(provider.onDidChange, (l, e) => e, 200);
const onDidChangeListener = onDidChange(resources => {
const resourceGroupsById = index(resourceGroupsIds, id => id, () => [] as SCMRawResource[]);
resources.forEach(resource => {
const resourceGroup = resourceGroupsById[resource.resourceGroup];
if (!resourceGroup) {
// TODO@Joao: ask Joh: should we warn? should we throw?
return;
}
resourceGroup.push({ uri: resource.uri.toString() });
});
const result = resourceGroupsIds.map(id => resourceGroupsById[id]);
this._proxy.$onChange(id, result);
});
return new Disposable(() => {
onDidChangeListener.dispose();
delete this._providers[id];
this._proxy.$unregister(id);
});