11import { injectable , inject } from "inversify" ;
22import { v4 } from "uuid" ;
3- import { FSNode , FSDocumentNode , FSCollectionNode , FSToolbarNode , FSConnectionNode , FSItemNode , FSSecurityNode , FSUsersNode , FSGroupsNode , FSUserNode , FSGroupNode , FSContainerNode , FSIndexesNode , FSIndexNode , FSRestNode , FSRestURINode , FSRestMethodNode } from "../classes/node" ;
3+ import { FSNode , FSDocumentNode , FSCollectionNode , FSToolbarNode , FSConnectionNode , FSItemNode , FSSecurityNode , FSUsersNode , FSGroupsNode , FSUserNode , FSGroupNode , FSContainerNode , FSIndexesNode , FSIndexNode , FSRestNode , FSRestURINode , FSRestMethodNode , FSLoadEvent } from "../classes/node" ;
44import { open , TreeNode , CompositeTreeNode , ConfirmDialog , SingleTextInputDialog , OpenerService , StatusBar , StatusBarAlignment , WidgetManager } from "@theia/core/lib/browser" ;
55import { WorkspaceService } from "@theia/workspace/lib/browser" ;
66import { OpenFileDialogProps , FileDialogService } from "@theia/filesystem/lib/browser" ;
@@ -65,6 +65,7 @@ export class FSCore {
6565 updating = false ;
6666 renaming = '' ;
6767 dict : Record < string , FSNode > = { } ;
68+ loadEvents : Record < string , ( FSLoadEvent ) [ ] > = { } ;
6869
6970 setLabelProvider ( labelProvider : FSLabelProviderContribution ) {
7071 this . _labelProvider = labelProvider ;
@@ -192,6 +193,9 @@ export class FSCore {
192193 }
193194
194195 protected removeNode ( child : FSNode ) {
196+ if ( this . loadEvents [ child . id ] ) {
197+ delete ( this . loadEvents [ child . id ] ) ;
198+ }
195199 const removeNodeId = ( node : FSNode ) => {
196200 delete ( this . dict [ node . nodeId ] ) ;
197201 if ( CompositeTreeNode . is ( node ) ) {
@@ -254,8 +258,29 @@ export class FSCore {
254258 }
255259 }
256260
257- public expand ( node : CompositeTreeNode ) {
258- this . model && this . model . expandNode ( node as any ) ;
261+ public async expand ( node : CompositeTreeNode ) {
262+ this . model && await this . model . expandNode ( node as any ) ;
263+ }
264+
265+ public expandAndWait ( node : FSContainerNode ) {
266+ return new Promise ( resolve => {
267+ if ( node . loaded ) {
268+ this . expand ( node ) ;
269+ resolve ( null ) ;
270+ } else {
271+ this . addLoadEvent ( node , node => {
272+ resolve ( null ) ;
273+ return true ;
274+ } ) ;
275+ this . expand ( node ) ;
276+ }
277+ } )
278+ }
279+
280+ public async ensureExpanded ( node : FSContainerNode ) {
281+ if ( ! node . expanded ) {
282+ await this . expandAndWait ( node ) ;
283+ }
259284 }
260285
261286 public getNode ( id : string ) : FSNode | undefined {
@@ -360,10 +385,54 @@ export class FSCore {
360385 return false ;
361386 }
362387
363- protected endLoading ( node : TreeNode ) : void {
388+ protected async endLoading ( node : TreeNode ) {
364389 if ( FSNode . is ( node ) ) {
365390 node . loading = false ;
366- this . refresh ( ) ;
391+ await this . refresh ( ) ;
392+ if ( this . loadEvents [ node . id ] ) {
393+ // const a =
394+ this . loadEvents [ node . id ] = await ( await Promise . all ( this . loadEvents [ node . id ] . map ( async event => {
395+ const result = event ( node ) ;
396+ if ( ! result ) {
397+ return event ;
398+ }
399+ if ( result === true ) {
400+ return null ;
401+ }
402+ return await result ? null : event
403+ } ) ) ) . filter ( event => event != null ) as FSLoadEvent [ ] ;
404+ if ( this . loadEvents [ node . id ] . length < 1 ) {
405+ delete ( this . loadEvents [ node . id ] ) ;
406+ }
407+ }
408+ }
409+ ( window as any ) . loadEvents = this . loadEvents ;
410+ }
411+
412+ protected addLoadEvent ( node : TreeNode , event : FSLoadEvent ) : void {
413+ if ( FSNode . is ( node ) ) {
414+ if ( ! this . loadEvents [ node . id ] ) {
415+ this . loadEvents [ node . id ] = [ event ] ;
416+ } else {
417+ if ( ! this . loadEvents [ node . id ] . find ( value => value === event ) ) {
418+ this . loadEvents [ node . id ] . push ( event ) ;
419+ }
420+ }
421+ }
422+ }
423+
424+ protected removeLoadEvent ( node : TreeNode , event ?: FSLoadEvent ) : void {
425+ if ( FSNode . is ( node ) ) {
426+ if ( this . loadEvents [ node . id ] ) {
427+ if ( event ) {
428+ this . loadEvents [ node . id ] = this . loadEvents [ node . id ] . filter ( value => value != event ) ;
429+ if ( this . loadEvents [ node . id ] . length < 1 ) {
430+ delete ( this . loadEvents [ node . id ] ) ;
431+ }
432+ } else {
433+ delete ( this . loadEvents [ node . id ] ) ;
434+ }
435+ }
367436 }
368437 }
369438
@@ -373,7 +442,7 @@ export class FSCore {
373442 if ( FSItem . isCollection ( result ) ) {
374443 const collection = result ;
375444 // refresh collections
376- let collectionsOld = node . children . filter ( child => FSNode . isCollection ( child ) ) as FSCollectionNode [ ] ;
445+ let collectionsOld = node . children . filter ( child => FSNode . isCollection ( child ) && ! child . isNew ) as FSCollectionNode [ ] ;
377446 const collectionsNew = collection . collections . filter ( subCollection => {
378447 const collectionNode = this . getNode ( this . itemID ( node . connectionNode . connection , subCollection ) ) ;
379448 if ( FSNode . isCollection ( collectionNode ) && collectionNode . parent === node ) {
@@ -386,7 +455,7 @@ export class FSCore {
386455 collectionsOld . forEach ( node => this . removeNode ( node ) ) ;
387456 collectionsNew . forEach ( collection => this . addCollection ( node , collection ) ) ;
388457 // refresh documents
389- let documentsOld = node . children . filter ( child => FSNode . isDocument ( child ) ) as FSDocumentNode [ ] ;
458+ let documentsOld = node . children . filter ( child => FSNode . isDocument ( child ) && ! child . isNew ) as FSDocumentNode [ ] ;
390459 const documentsNew = collection . documents . filter ( subDocument => {
391460 const documentNode = this . getNode ( this . itemID ( node . connectionNode . connection , subDocument ) ) ;
392461 if ( FSNode . isDocument ( documentNode ) && documentNode . parent === node ) {
@@ -1448,6 +1517,7 @@ export class FSCore {
14481517 return false ;
14491518 }
14501519 const collection = this . node as FSCollectionNode ;
1520+ await this . ensureExpanded ( collection ) ;
14511521 const validator = ( input : string ) => input !== '' && ! this . fileExists ( input ) ;
14521522 let initialName = this . newName ( validator ) ;
14531523 if ( extension ) {
@@ -1543,6 +1613,7 @@ export class FSCore {
15431613 }
15441614 }
15451615 if ( FSNode . isCollection ( collection ) ) {
1616+ await this . ensureExpanded ( collection ) ;
15461617 const validator = ( input : string ) => input !== '' && ! this . fileExists ( input ) ;
15471618 const dialog = new SingleTextInputDialog ( {
15481619 initialValue : this . newName ( validator ) ,
0 commit comments