|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +'use strict'; |
| 7 | + |
| 8 | +import { workspace, Disposable } from 'vscode'; |
| 9 | +import { GitErrorCodes } from './git'; |
| 10 | +import { Model } from './model'; |
| 11 | +import { throttle } from './util'; |
| 12 | +import { decorate } from 'core-decorators'; |
| 13 | + |
| 14 | +export class AutoFetcher { |
| 15 | + |
| 16 | + private static Period = 3 * 60 * 1000 /* three minutes */; |
| 17 | + private disposables: Disposable[] = []; |
| 18 | + private timer: NodeJS.Timer; |
| 19 | + |
| 20 | + constructor(private model: Model) { |
| 21 | + workspace.onDidChangeConfiguration(this.onConfiguration, this, this.disposables); |
| 22 | + this.onConfiguration(); |
| 23 | + } |
| 24 | + |
| 25 | + private onConfiguration(): void { |
| 26 | + const gitConfig = workspace.getConfiguration('git'); |
| 27 | + |
| 28 | + if (gitConfig.get<boolean>('autofetch') === false) { |
| 29 | + this.disable(); |
| 30 | + } else { |
| 31 | + this.enable(); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + enable(): void { |
| 36 | + if (this.timer) { |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + this.fetch(); |
| 41 | + this.timer = setInterval(() => this.fetch(), AutoFetcher.Period); |
| 42 | + } |
| 43 | + |
| 44 | + disable(): void { |
| 45 | + clearInterval(this.timer); |
| 46 | + } |
| 47 | + |
| 48 | + @decorate(throttle) |
| 49 | + private async fetch(): Promise<void> { |
| 50 | + try { |
| 51 | + await this.model.fetch(); |
| 52 | + } catch (err) { |
| 53 | + if (err.gitErrorCode === GitErrorCodes.AuthenticationFailed) { |
| 54 | + this.disable(); |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + dispose(): void { |
| 60 | + this.disable(); |
| 61 | + this.disposables.forEach(d => d.dispose()); |
| 62 | + } |
| 63 | +} |
0 commit comments