1
1
// Copyright (c) Microsoft Corporation. All rights reserved.
2
2
// Licensed under the MIT license.
3
3
4
+ import * as _ from "lodash" ;
4
5
import {
5
6
commands , Event , EventEmitter , ExtensionContext , ProviderResult , Range ,
6
7
Selection , TextEditorRevealType , TreeDataProvider , TreeItem , Uri , window , workspace ,
@@ -23,26 +24,46 @@ export class DependencyDataProvider implements TreeDataProvider<ExplorerNode> {
23
24
public onDidChangeTreeData : Event < null > = this . _onDidChangeTreeData . event ;
24
25
25
26
private _rootItems : ExplorerNode [ ] = null ;
27
+ private _refreshDelayTrigger : ( ( ) => void ) & _ . Cancelable ;
26
28
27
29
constructor ( public readonly context : ExtensionContext ) {
28
- context . subscriptions . push ( commands . registerCommand ( Commands . VIEW_PACKAGE_REFRESH , ( ) => this . refreshWithLog ( ) ) ) ;
30
+ context . subscriptions . push ( commands . registerCommand ( Commands . VIEW_PACKAGE_REFRESH , ( debounce ?: boolean ) => this . refreshWithLog ( debounce ) ) ) ;
29
31
context . subscriptions . push ( commands . registerCommand ( Commands . VIEW_PACKAGE_OPEN_FILE ,
30
32
instrumentOperation ( Commands . VIEW_PACKAGE_OPEN_FILE , ( _operationId , uri ) => this . openFile ( uri ) ) ) ) ;
31
33
context . subscriptions . push ( commands . registerCommand ( Commands . VIEW_PACKAGE_OUTLINE ,
32
34
instrumentOperation ( Commands . VIEW_PACKAGE_OUTLINE , ( _operationId , uri , range ) => this . goToOutline ( uri , range ) ) ) ) ;
35
+ Settings . registerConfigurationListener ( ( updatedConfig , dependencyConfig ) => {
36
+ if ( updatedConfig . refreshDelay !== dependencyConfig . refreshDelay ) {
37
+ this . setRefreshDelay ( updatedConfig . refreshDelay ) ;
38
+ }
39
+ } ) ;
40
+ this . setRefreshDelay ( ) ;
33
41
}
34
42
35
- public refreshWithLog ( ) {
43
+ public refreshWithLog ( debounce ?: boolean ) {
36
44
if ( Settings . autoRefresh ( ) ) {
37
- this . refresh ( ) ;
45
+ this . refresh ( debounce ) ;
38
46
} else {
39
- instrumentOperation ( Commands . VIEW_PACKAGE_REFRESH , ( ) => this . refresh ( ) ) ( ) ;
47
+ instrumentOperation ( Commands . VIEW_PACKAGE_REFRESH , ( ) => this . refresh ( debounce ) ) ( ) ;
40
48
}
41
49
}
42
50
43
- public refresh ( ) {
44
- this . _rootItems = null ;
45
- this . _onDidChangeTreeData . fire ( ) ;
51
+ public refresh ( debounce = false ) {
52
+ if ( debounce ) {
53
+ this . _refreshDelayTrigger ( ) ;
54
+ } else { // Immediately refresh
55
+ this . _refreshDelayTrigger . flush ( ) ;
56
+ }
57
+ }
58
+
59
+ public setRefreshDelay ( wait ?: number ) {
60
+ if ( ! wait ) {
61
+ wait = Settings . refreshDelay ( ) ;
62
+ }
63
+ if ( this . _refreshDelayTrigger ) {
64
+ this . _refreshDelayTrigger . cancel ( ) ;
65
+ }
66
+ this . _refreshDelayTrigger = _ . debounce ( ( ) => this . doRefresh ( ) , wait ) ;
46
67
}
47
68
48
69
public openFile ( uri : string ) {
@@ -83,6 +104,11 @@ export class DependencyDataProvider implements TreeDataProvider<ExplorerNode> {
83
104
return project ? project . revealPaths ( paths ) : null ;
84
105
}
85
106
107
+ private doRefresh ( ) : void {
108
+ this . _rootItems = null ;
109
+ this . _onDidChangeTreeData . fire ( ) ;
110
+ }
111
+
86
112
private async getRootProjects ( ) : Promise < ExplorerNode [ ] > {
87
113
const rootElements = this . _rootItems ? this . _rootItems : await this . getChildren ( ) ;
88
114
if ( rootElements [ 0 ] instanceof ProjectNode ) {
0 commit comments