1
1
import * as vscode from 'vscode' ;
2
2
const path = require ( 'path' ) ;
3
3
4
+ import { createLogger } from '../logging' ;
4
5
import DocumentListTreeItem , {
5
6
CollectionTypes ,
6
7
MAX_DOCUMENTS_VISIBLE
@@ -10,6 +11,8 @@ import TreeItemParent from './treeItemParentInterface';
10
11
import SchemaTreeItem from './schemaTreeItem' ;
11
12
import { getImagesPath } from '../extensionConstants' ;
12
13
14
+ const log = createLogger ( 'tree view collection folder' ) ;
15
+
13
16
type CollectionModelType = {
14
17
name : string ;
15
18
type : CollectionTypes ;
@@ -36,9 +39,11 @@ export default class CollectionTreeItem extends vscode.TreeItem
36
39
collection : CollectionModelType ;
37
40
collectionName : string ;
38
41
databaseName : string ;
42
+ namespace : string ;
39
43
40
44
private _dataService : any ;
41
45
private _type : CollectionTypes ;
46
+ documentCount : number | null ;
42
47
43
48
isExpanded : boolean ;
44
49
@@ -50,6 +55,7 @@ export default class CollectionTreeItem extends vscode.TreeItem
50
55
dataService : any ,
51
56
isExpanded : boolean ,
52
57
cacheIsUpToDate : boolean ,
58
+ cachedDocumentCount : number | null ,
53
59
existingDocumentListChild ?: DocumentListTreeItem ,
54
60
existingSchemaChild ?: SchemaTreeItem ,
55
61
existingIndexListChild ?: IndexListTreeItem
@@ -64,12 +70,15 @@ export default class CollectionTreeItem extends vscode.TreeItem
64
70
this . collection = collection ;
65
71
this . collectionName = collection . name ;
66
72
this . databaseName = databaseName ;
73
+ this . namespace = `${ this . databaseName } .${ this . collectionName } ` ;
67
74
68
75
this . _type = collection . type ; // Type can be `collection` or `view`.
69
76
this . _dataService = dataService ;
70
77
71
78
this . isExpanded = isExpanded ;
72
79
80
+ this . documentCount = cachedDocumentCount ;
81
+
73
82
this . cacheIsUpToDate = cacheIsUpToDate ;
74
83
75
84
this . _documentListChild = existingDocumentListChild
@@ -81,7 +90,8 @@ export default class CollectionTreeItem extends vscode.TreeItem
81
90
this . _dataService ,
82
91
false , // Collapsed.
83
92
MAX_DOCUMENTS_VISIBLE ,
84
- false , // No more documents to show.
93
+ this . documentCount ,
94
+ this . refreshDocumentCount ,
85
95
false , // Cache is not up to date.
86
96
[ ] // Empty cache.
87
97
) ;
@@ -119,9 +129,13 @@ export default class CollectionTreeItem extends vscode.TreeItem
119
129
return element ;
120
130
}
121
131
122
- getChildren ( ) : Thenable < any [ ] > {
132
+ async getChildren ( ) : Promise < any [ ] > {
123
133
if ( ! this . isExpanded ) {
124
- return Promise . resolve ( [ ] ) ;
134
+ return [ ] ;
135
+ }
136
+
137
+ if ( this . documentCount === null ) {
138
+ await this . refreshDocumentCount ( ) ;
125
139
}
126
140
127
141
// Update cache if one of the children has been expanded/collapsed.
@@ -130,11 +144,7 @@ export default class CollectionTreeItem extends vscode.TreeItem
130
144
}
131
145
132
146
if ( this . cacheIsUpToDate ) {
133
- return Promise . resolve ( [
134
- this . _documentListChild ,
135
- this . _schemaChild ,
136
- this . _indexListChild
137
- ] ) ;
147
+ return [ this . _documentListChild , this . _schemaChild , this . _indexListChild ] ;
138
148
}
139
149
140
150
this . cacheIsUpToDate = true ;
@@ -143,11 +153,7 @@ export default class CollectionTreeItem extends vscode.TreeItem
143
153
// is ensure to be set by vscode.
144
154
this . rebuildChildrenCache ( ) ;
145
155
146
- return Promise . resolve ( [
147
- this . _documentListChild ,
148
- this . _schemaChild ,
149
- this . _indexListChild
150
- ] ) ;
156
+ return [ this . _documentListChild , this . _schemaChild , this . _indexListChild ] ;
151
157
}
152
158
153
159
rebuildDocumentListTreeItem ( ) : void {
@@ -158,7 +164,8 @@ export default class CollectionTreeItem extends vscode.TreeItem
158
164
this . _dataService ,
159
165
this . _documentListChild . isExpanded ,
160
166
this . _documentListChild . getMaxDocumentsToShow ( ) ,
161
- this . _documentListChild . hasMoreDocumentsToShow ,
167
+ this . documentCount ,
168
+ this . refreshDocumentCount ,
162
169
this . _documentListChild . cacheIsUpToDate ,
163
170
this . _documentListChild . getChildrenCache ( )
164
171
) ;
@@ -209,15 +216,18 @@ export default class CollectionTreeItem extends vscode.TreeItem
209
216
this . cacheIsUpToDate = false ;
210
217
}
211
218
212
- onDidExpand ( ) : Promise < boolean > {
219
+ async onDidExpand ( ) : Promise < boolean > {
213
220
this . isExpanded = true ;
214
221
this . cacheIsUpToDate = false ;
215
222
216
- return Promise . resolve ( true ) ;
223
+ await this . refreshDocumentCount ( ) ;
224
+
225
+ return true ;
217
226
}
218
227
219
228
resetCache ( ) : void {
220
229
this . cacheIsUpToDate = false ;
230
+ this . documentCount = null ;
221
231
222
232
this . _documentListChild = new DocumentListTreeItem (
223
233
this . collectionName ,
@@ -226,7 +236,8 @@ export default class CollectionTreeItem extends vscode.TreeItem
226
236
this . _dataService ,
227
237
false , // Collapsed.
228
238
MAX_DOCUMENTS_VISIBLE ,
229
- false , // No more documents to show.
239
+ this . documentCount ,
240
+ this . refreshDocumentCount ,
230
241
false , // Cache is not up to date.
231
242
[ ] // Empty cache.
232
243
) ;
@@ -268,6 +279,43 @@ export default class CollectionTreeItem extends vscode.TreeItem
268
279
return this . _documentListChild . getMaxDocumentsToShow ( ) ;
269
280
}
270
281
282
+ getCount ( ) : Promise < number > {
283
+ log . info ( `fetching document count from namespace ${ this . namespace } ` ) ;
284
+
285
+ return new Promise ( ( resolve , reject ) => {
286
+ this . _dataService . estimatedCount (
287
+ this . namespace ,
288
+ { } , // No options.
289
+ ( err : Error | undefined , count : number ) => {
290
+ if ( err ) {
291
+ return reject (
292
+ new Error (
293
+ `Unable to get collection document count: ${ err . message } `
294
+ )
295
+ ) ;
296
+ }
297
+
298
+ return resolve ( count ) ;
299
+ }
300
+ ) ;
301
+ } ) ;
302
+ }
303
+
304
+ refreshDocumentCount = async ( ) : Promise < boolean > => {
305
+ try {
306
+ // We fetch the document when we expand in order to show
307
+ // the document count in the document list tree item `description`.
308
+ this . documentCount = await this . getCount ( ) ;
309
+ } catch ( err ) {
310
+ vscode . window . showInformationMessage (
311
+ `Unable to fetch document count: ${ err } `
312
+ ) ;
313
+ return false ;
314
+ }
315
+
316
+ return true ;
317
+ } ;
318
+
271
319
async onDropCollectionClicked ( ) : Promise < boolean > {
272
320
const collectionName = this . collectionName ;
273
321
0 commit comments