55
66'use strict' ;
77
8- import { workspace , Disposable } from 'vscode' ;
8+ import { workspace , Disposable , EventEmitter } from 'vscode' ;
99import { GitErrorCodes } from './git' ;
1010import { Repository } from './repository' ;
11- import { throttle } from './decorators ' ;
11+ import { eventToPromise , filterEvent } from './util ' ;
1212
1313export class AutoFetcher {
1414
1515 private static Period = 3 * 60 * 1000 /* three minutes */ ;
16+
17+ private _onDidChange = new EventEmitter < boolean > ( ) ;
18+ private onDidChange = this . _onDidChange . event ;
19+
20+ private _enabled : boolean = false ;
21+ get enabled ( ) : boolean { return this . _enabled ; }
22+ set enabled ( enabled : boolean ) { this . _enabled = enabled ; this . _onDidChange . fire ( enabled ) ; }
23+
1624 private disposables : Disposable [ ] = [ ] ;
17- private timer : NodeJS . Timer ;
1825
1926 constructor ( private repository : Repository ) {
2027 workspace . onDidChangeConfiguration ( this . onConfiguration , this , this . disposables ) ;
@@ -32,26 +39,41 @@ export class AutoFetcher {
3239 }
3340
3441 enable ( ) : void {
35- if ( this . timer ) {
42+ if ( this . enabled ) {
3643 return ;
3744 }
3845
39- this . fetch ( ) ;
40- this . timer = setInterval ( ( ) => this . fetch ( ) , AutoFetcher . Period ) ;
46+ this . enabled = true ;
47+ this . run ( ) ;
4148 }
4249
4350 disable ( ) : void {
44- clearInterval ( this . timer ) ;
51+ this . enabled = false ;
4552 }
4653
47- @throttle
48- private async fetch ( ) : Promise < void > {
49- try {
50- await this . repository . fetch ( ) ;
51- } catch ( err ) {
52- if ( err . gitErrorCode === GitErrorCodes . AuthenticationFailed ) {
53- this . disable ( ) ;
54+ private async run ( ) : Promise < void > {
55+ while ( this . enabled ) {
56+ await this . repository . whenIdleAndFocused ( ) ;
57+
58+ if ( ! this . enabled ) {
59+ return ;
5460 }
61+
62+ try {
63+ await this . repository . fetch ( ) ;
64+ } catch ( err ) {
65+ if ( err . gitErrorCode === GitErrorCodes . AuthenticationFailed ) {
66+ this . disable ( ) ;
67+ }
68+ }
69+
70+ if ( ! this . enabled ) {
71+ return ;
72+ }
73+
74+ const timeout = new Promise ( c => setTimeout ( c , AutoFetcher . Period ) ) ;
75+ const whenDisabled = eventToPromise ( filterEvent ( this . onDidChange , enabled => ! enabled ) ) ;
76+ await Promise . race ( [ timeout , whenDisabled ] ) ;
5577 }
5678 }
5779
0 commit comments