From 65478f4b5e1cbfe7d887f5c6cc8952b92cf3bf37 Mon Sep 17 00:00:00 2001 From: Joao Moreno Date: Fri, 13 Jan 2017 16:10:10 +0100 Subject: [PATCH] git: autofetch --- extensions/git/src/autofetch.ts | 63 +++++++++++++++++++++++++++++++++ extensions/git/src/main.ts | 6 +++- extensions/git/src/model.ts | 5 +++ 3 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 extensions/git/src/autofetch.ts diff --git a/extensions/git/src/autofetch.ts b/extensions/git/src/autofetch.ts new file mode 100644 index 00000000000..39ac8dc1e34 --- /dev/null +++ b/extensions/git/src/autofetch.ts @@ -0,0 +1,63 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ + +'use strict'; + +import { workspace, Disposable } from 'vscode'; +import { GitErrorCodes } from './git'; +import { Model } from './model'; +import { throttle } from './util'; +import { decorate } from 'core-decorators'; + +export class AutoFetcher { + + private static Period = 3 * 60 * 1000 /* three minutes */; + private disposables: Disposable[] = []; + private timer: NodeJS.Timer; + + constructor(private model: Model) { + workspace.onDidChangeConfiguration(this.onConfiguration, this, this.disposables); + this.onConfiguration(); + } + + private onConfiguration(): void { + const gitConfig = workspace.getConfiguration('git'); + + if (gitConfig.get('autofetch') === false) { + this.disable(); + } else { + this.enable(); + } + } + + enable(): void { + if (this.timer) { + return; + } + + this.fetch(); + this.timer = setInterval(() => this.fetch(), AutoFetcher.Period); + } + + disable(): void { + clearInterval(this.timer); + } + + @decorate(throttle) + private async fetch(): Promise { + try { + await this.model.fetch(); + } catch (err) { + if (err.gitErrorCode === GitErrorCodes.AuthenticationFailed) { + this.disable(); + } + } + } + + dispose(): void { + this.disable(); + this.disposables.forEach(d => d.dispose()); + } +} diff --git a/extensions/git/src/main.ts b/extensions/git/src/main.ts index 22b6b2292fb..10eba2775f9 100644 --- a/extensions/git/src/main.ts +++ b/extensions/git/src/main.ts @@ -13,6 +13,7 @@ import { CommandCenter } from './commands'; import { CheckoutStatusBar, SyncStatusBar } from './statusbar'; import { filterEvent, anyEvent, throttle } from './util'; import { GitContentProvider } from './contentProvider'; +import { AutoFetcher } from './autofetch'; import * as nls from 'vscode-nls'; import { decorate, debounce } from 'core-decorators'; @@ -73,6 +74,8 @@ async function init(disposables: Disposable[]): Promise { const checkoutStatusBar = new CheckoutStatusBar(model); const syncStatusBar = new SyncStatusBar(model); + const autoFetcher = new AutoFetcher(model); + disposables.push( commandCenter, provider, @@ -81,7 +84,8 @@ async function init(disposables: Disposable[]): Promise { fsWatcher, watcher, checkoutStatusBar, - syncStatusBar + syncStatusBar, + autoFetcher ); } diff --git a/extensions/git/src/model.ts b/extensions/git/src/model.ts index 91df39977b0..242e9114ebe 100644 --- a/extensions/git/src/model.ts +++ b/extensions/git/src/model.ts @@ -332,6 +332,11 @@ export class Model { await this.update(); } + async fetch(): Promise { + await this.repository.fetch(); + await this.update(); + } + async sync(): Promise { await this.repository.sync(); await this.update();