@@ -241,8 +241,6 @@ const App: React.FC = () => {
241
241
const [ storage , setStorage ] = useState < commonStorage . Storage | null > ( null ) ;
242
242
const [ mostRecentModulePath , setMostRecentModulePath ] = useState < string | null > ( null ) ;
243
243
const [ shownPythonToolboxCategories , setShownPythonToolboxCategories ] = useState < Set < string > > ( new Set ( ) ) ;
244
- const [ triggerListModules , setTriggerListModules ] = useState ( 0 ) ;
245
- const afterListModulesSuccess = useRef < ( ) => void > ( ( ) => { } ) ;
246
244
const [ modules , setModules ] = useState < commonStorage . Project [ ] > ( [ ] ) ;
247
245
const [ treeData , setTreeData ] = useState < TreeDataNode [ ] > ( [ ] ) ;
248
246
const [ treeExpandedKeys , setTreeExpandedKeys ] = useState < React . Key [ ] > ( [ ] ) ;
@@ -327,6 +325,7 @@ const App: React.FC = () => {
327
325
}
328
326
fetchMostRecentModulePath ( ) ;
329
327
initializeShownPythonToolboxCategories ( ) ;
328
+ initializeModules ( ) ;
330
329
} , [ storage ] ) ;
331
330
332
331
const fetchMostRecentModulePath = async ( ) => {
@@ -350,42 +349,30 @@ const App: React.FC = () => {
350
349
}
351
350
} ;
352
351
353
- // Fetch the list of modules from storage.
354
- useEffect ( ( ) => {
355
- if ( ignoreEffect ( ) ) {
356
- return ;
352
+ const initializeModules = async ( ) => {
353
+ const array = await fetchListOfModules ( ) ;
354
+ if ( array . length === 0 ) {
355
+ setNewProjectNameModalPurpose ( PURPOSE_NEW_PROJECT ) ;
356
+ setNewProjectNameModalInitialValue ( '' ) ;
357
+ setNewProjectNameModalTitle ( 'Welcome to WPILib Blocks!' ) ;
358
+ setNewProjectNameModalIsOpen ( true ) ;
357
359
}
358
- // mostRecentModulePath hasn't been fetched yet. Try again in a bit.
359
- if ( mostRecentModulePath == null ) {
360
- setTimeout ( ( ) => {
361
- setTriggerListModules ( Date . now ( ) ) ;
362
- } , 50 ) ;
363
- return ;
364
- }
365
-
366
- fetchListOfModules ( ) ;
367
- } , [ triggerListModules ] ) ;
360
+ } ;
368
361
369
- const fetchListOfModules = async ( ) => {
370
- try {
371
- const array = await storage . listModules ( ) ;
372
- setModules ( array )
373
- const callback = afterListModulesSuccess . current ;
374
- afterListModulesSuccess . current = ( ) => { } ;
375
- callback ( ) ;
376
-
377
- if ( array . length === 0 ) {
378
- setNewProjectNameModalPurpose ( PURPOSE_NEW_PROJECT ) ;
379
- setNewProjectNameModalInitialValue ( '' ) ;
380
- setNewProjectNameModalTitle ( 'Welcome to WPILib Blocks!' ) ;
381
- setNewProjectNameModalIsOpen ( true ) ;
362
+ const fetchListOfModules = async ( ) : Promise < commonStorage . Project [ ] > => {
363
+ return new Promise ( async ( resolve , reject ) => {
364
+ try {
365
+ const array = await storage . listModules ( ) ;
366
+ setModules ( array )
367
+ resolve ( array ) ;
368
+ } catch ( e ) {
369
+ console . log ( 'Failed to load the list of modules. Caught the following error...' ) ;
370
+ console . log ( e ) ;
371
+ setAlertErrorMessage ( 'Failed to load the list of modules.' ) ;
372
+ setAlertErrorVisible ( true ) ;
373
+ reject ( new Error ( 'Failed to load the list of modules.' ) ) ;
382
374
}
383
- } catch ( e ) {
384
- console . log ( 'Failed to load the list of modules. Caught the following error...' ) ;
385
- console . log ( e ) ;
386
- setAlertErrorMessage ( 'Failed to load the list of modules.' ) ;
387
- setAlertErrorVisible ( true ) ;
388
- }
375
+ } ) ;
389
376
} ;
390
377
391
378
// When the list of modules is set, update the treeData and treeExpandedKeys.
@@ -621,10 +608,8 @@ const App: React.FC = () => {
621
608
try {
622
609
await storage . createModule (
623
610
commonStorage . MODULE_TYPE_PROJECT , newProjectPath , projectContent ) ;
624
- afterListModulesSuccess . current = ( ) => {
625
- setCurrentModulePath ( newProjectPath ) ;
626
- } ;
627
- setTriggerListModules ( Date . now ( ) ) ;
611
+ await fetchListOfModules ( ) ;
612
+ setCurrentModulePath ( newProjectPath ) ;
628
613
} catch ( e ) {
629
614
console . log ( 'Failed to create a new project. Caught the following error...' ) ;
630
615
console . log ( e ) ;
@@ -636,10 +621,8 @@ const App: React.FC = () => {
636
621
await storage . renameModule (
637
622
currentModule . moduleType , currentModule . projectName ,
638
623
currentModule . moduleName , newProjectName ) ;
639
- afterListModulesSuccess . current = ( ) => {
640
- setCurrentModulePath ( newProjectPath ) ;
641
- } ;
642
- setTriggerListModules ( Date . now ( ) ) ;
624
+ await fetchListOfModules ( ) ;
625
+ setCurrentModulePath ( newProjectPath ) ;
643
626
} catch ( e ) {
644
627
console . log ( 'Failed to rename the project. Caught the following error...' ) ;
645
628
console . log ( e ) ;
@@ -651,10 +634,8 @@ const App: React.FC = () => {
651
634
await storage . copyModule (
652
635
currentModule . moduleType , currentModule . projectName ,
653
636
currentModule . moduleName , newProjectName ) ;
654
- afterListModulesSuccess . current = ( ) => {
655
- setCurrentModulePath ( newProjectPath ) ;
656
- } ;
657
- setTriggerListModules ( Date . now ( ) ) ;
637
+ await fetchListOfModules ( ) ;
638
+ setCurrentModulePath ( newProjectPath ) ;
658
639
} catch ( e ) {
659
640
console . log ( 'Failed to copy the project. Caught the following error...' ) ;
660
641
console . log ( e ) ;
@@ -714,10 +695,8 @@ const App: React.FC = () => {
714
695
try {
715
696
await storage . createModule (
716
697
commonStorage . MODULE_TYPE_MECHANISM , newModulePath , mechanismContent ) ;
717
- afterListModulesSuccess . current = ( ) => {
718
- setCurrentModulePath ( newModulePath ) ;
719
- } ;
720
- setTriggerListModules ( Date . now ( ) ) ;
698
+ await fetchListOfModules ( ) ;
699
+ setCurrentModulePath ( newModulePath ) ;
721
700
} catch ( e ) {
722
701
console . log ( 'Failed to create a new mechanism. Caught the following error...' ) ;
723
702
console . log ( e ) ;
@@ -729,10 +708,8 @@ const App: React.FC = () => {
729
708
try {
730
709
await storage . createModule (
731
710
commonStorage . MODULE_TYPE_OPMODE , newModulePath , opModeContent ) ;
732
- afterListModulesSuccess . current = ( ) => {
733
- setCurrentModulePath ( newModulePath ) ;
734
- } ;
735
- setTriggerListModules ( Date . now ( ) ) ;
711
+ await fetchListOfModules ( ) ;
712
+ setCurrentModulePath ( newModulePath ) ;
736
713
} catch ( e ) {
737
714
console . log ( 'Failed to create a new OpMode. Caught the following error...' ) ;
738
715
console . log ( e ) ;
@@ -744,10 +721,8 @@ const App: React.FC = () => {
744
721
await storage . renameModule (
745
722
currentModule . moduleType , currentModule . projectName ,
746
723
currentModule . moduleName , newModuleName ) ;
747
- afterListModulesSuccess . current = ( ) => {
748
- setCurrentModulePath ( newModulePath ) ;
749
- } ;
750
- setTriggerListModules ( Date . now ( ) ) ;
724
+ await fetchListOfModules ( ) ;
725
+ setCurrentModulePath ( newModulePath ) ;
751
726
} catch ( e ) {
752
727
console . log ( 'Failed to rename the module. Caught the following error...' ) ;
753
728
console . log ( e ) ;
@@ -759,10 +734,8 @@ const App: React.FC = () => {
759
734
await storage . copyModule (
760
735
currentModule . moduleType , currentModule . projectName ,
761
736
currentModule . moduleName , newModuleName ) ;
762
- afterListModulesSuccess . current = ( ) => {
763
- setCurrentModulePath ( newModulePath ) ;
764
- } ;
765
- setTriggerListModules ( Date . now ( ) ) ;
737
+ await fetchListOfModules ( ) ;
738
+ setCurrentModulePath ( newModulePath ) ;
766
739
} catch ( e ) {
767
740
console . log ( 'Failed to copy the module. Caught the following error...' ) ;
768
741
console . log ( e ) ;
@@ -888,7 +861,7 @@ const App: React.FC = () => {
888
861
}
889
862
try {
890
863
await storage . deleteModule ( moduleTypeToDelete , modulePathToDelete ) ;
891
- setTriggerListModules ( Date . now ( ) ) ;
864
+ await fetchListOfModules ( ) ;
892
865
} catch ( e ) {
893
866
console . log ( 'Failed to delete the project. Caught the following error...' ) ;
894
867
console . log ( e ) ;
@@ -906,7 +879,7 @@ const App: React.FC = () => {
906
879
setCurrentModulePath ( projectPath ) ;
907
880
try {
908
881
await storage . deleteModule ( moduleTypeToDelete , modulePathToDelete ) ;
909
- setTriggerListModules ( Date . now ( ) ) ;
882
+ await fetchListOfModules ( ) ;
910
883
} catch ( e ) {
911
884
console . log ( 'Failed to delete the module. Caught the following error...' ) ;
912
885
console . log ( e ) ;
@@ -940,11 +913,9 @@ const App: React.FC = () => {
940
913
try {
941
914
await storage . uploadProject ( uploadProjectName , dataUrl ) ;
942
915
onSuccess ( 'Upload successful' ) ;
943
- afterListModulesSuccess . current = ( ) => {
944
- const uploadProjectPath = commonStorage . makeProjectPath ( uploadProjectName ) ;
945
- setCurrentModulePath ( uploadProjectPath ) ;
946
- } ;
947
- setTriggerListModules ( Date . now ( ) ) ;
916
+ await fetchListOfModules ( ) ;
917
+ const uploadProjectPath = commonStorage . makeProjectPath ( uploadProjectName ) ;
918
+ setCurrentModulePath ( uploadProjectPath ) ;
948
919
} catch ( e ) {
949
920
console . log ( 'Failed to upload the project. Caught the following error...' ) ;
950
921
console . log ( e ) ;
0 commit comments