提交 daa8cce0 编写于 作者: J Joao Moreno

git: react to fs events

上级 29933d67
...@@ -11,7 +11,9 @@ import { findGit, Git } from './git'; ...@@ -11,7 +11,9 @@ import { findGit, Git } from './git';
import { Model } from './model'; import { Model } from './model';
import { GitSCMProvider } from './scmProvider'; import { GitSCMProvider } from './scmProvider';
import { registerCommands } from './commands'; import { registerCommands } from './commands';
import { anyEvent, throttle } from './util';
import * as nls from 'vscode-nls'; import * as nls from 'vscode-nls';
import { decorate, debounce } from 'core-decorators';
nls.config(); nls.config();
...@@ -36,6 +38,37 @@ class TextDocumentContentProvider { ...@@ -36,6 +38,37 @@ class TextDocumentContentProvider {
} }
} }
class Watcher {
private disposables: Disposable[];
constructor(private model: Model) {
const fsWatcher = workspace.createFileSystemWatcher('**');
const onFSChange = anyEvent(fsWatcher.onDidChange, fsWatcher.onDidCreate, fsWatcher.onDidDelete);
this.disposables = [
fsWatcher,
onFSChange(this.eventuallyUpdateModel, this)
];
}
@debounce(1000)
private eventuallyUpdateModel(): void {
this.updateModelAndWait();
}
@decorate(throttle)
private async updateModelAndWait(): Promise<void> {
console.log('UPDATE');
await this.model.update();
await new Promise(c => setTimeout(c, 8000));
}
dispose(): void {
this.disposables.forEach(d => d.dispose());
}
}
async function init(disposables: Disposable[]): Promise<void> { async function init(disposables: Disposable[]): Promise<void> {
const rootPath = workspace.rootPath; const rootPath = workspace.rootPath;
...@@ -55,11 +88,14 @@ async function init(disposables: Disposable[]): Promise<void> { ...@@ -55,11 +88,14 @@ async function init(disposables: Disposable[]): Promise<void> {
outputChannel.appendLine(`Using git ${info.version} from ${info.path}`); outputChannel.appendLine(`Using git ${info.version} from ${info.path}`);
git.onOutput(str => outputChannel.append(str), null, disposables); git.onOutput(str => outputChannel.append(str), null, disposables);
const watcher = new Watcher(model);
disposables.push( disposables.push(
registerCommands(model), registerCommands(model),
scm.registerSCMProvider('git', provider), scm.registerSCMProvider('git', provider),
workspace.registerTextDocumentContentProvider('git-index', new TextDocumentContentProvider(git, rootPath)), workspace.registerTextDocumentContentProvider('git-index', new TextDocumentContentProvider(git, rootPath)),
outputChannel outputChannel,
watcher
); );
} }
......
...@@ -203,21 +203,8 @@ export class Model { ...@@ -203,21 +203,8 @@ export class Model {
return this._remotes; return this._remotes;
} }
update(now = false): void {
if (now) {
this.updateNow();
} else {
this.eventuallyUpdate();
}
}
@debounce(500)
private eventuallyUpdate(): void {
this.updateNow();
}
@decorate(throttle) @decorate(throttle)
private async updateNow(): Promise<void> { async update(): Promise<void> {
const status = await this.repository.getStatus(); const status = await this.repository.getStatus();
let HEAD: IRef | undefined; let HEAD: IRef | undefined;
...@@ -286,13 +273,13 @@ export class Model { ...@@ -286,13 +273,13 @@ export class Model {
async stage(...resources: Resource[]): Promise<void> { async stage(...resources: Resource[]): Promise<void> {
const paths = resources.map(r => r.uri.fsPath); const paths = resources.map(r => r.uri.fsPath);
await this.repository.add(paths); await this.repository.add(paths);
await this.updateNow(); await this.update();
} }
async unstage(...resources: Resource[]): Promise<void> { async unstage(...resources: Resource[]): Promise<void> {
const paths = resources.map(r => r.uri.fsPath); const paths = resources.map(r => r.uri.fsPath);
await this.repository.revertFiles('HEAD', paths); await this.repository.revertFiles('HEAD', paths);
await this.updateNow(); await this.update();
} }
async commit(message: string, opts: { all?: boolean, amend?: boolean, signoff?: boolean } = Object.create(null)): Promise<void> { async commit(message: string, opts: { all?: boolean, amend?: boolean, signoff?: boolean } = Object.create(null)): Promise<void> {
...@@ -301,7 +288,7 @@ export class Model { ...@@ -301,7 +288,7 @@ export class Model {
} }
await this.repository.commit(message, opts); await this.repository.commit(message, opts);
await this.updateNow(); await this.update();
} }
async clean(...resources: Resource[]): Promise<void> { async clean(...resources: Resource[]): Promise<void> {
...@@ -332,6 +319,6 @@ export class Model { ...@@ -332,6 +319,6 @@ export class Model {
} }
await Promise.all(promises); await Promise.all(promises);
await this.updateNow(); await this.update();
} }
} }
\ No newline at end of file
...@@ -18,7 +18,7 @@ export class GitSCMProvider implements SCMProvider { ...@@ -18,7 +18,7 @@ export class GitSCMProvider implements SCMProvider {
get label(): string { return 'Git'; } get label(): string { return 'Git'; }
constructor(private model: Model) { constructor(private model: Model) {
model.update(true); model.update();
} }
commit(message: string): Thenable<void> { commit(message: string): Thenable<void> {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册