@@ -59,23 +59,49 @@ class DB extends Dexie {
59
59
}
60
60
61
61
async loadFromStorage ( ) {
62
- try {
63
- const result = await browser . storage . local . get ( [
64
- "allowances" ,
65
- "payments" ,
66
- ] ) ;
67
- console . log ( "Loading DB data from storage" ) ;
68
- if ( result . allowances ) {
69
- await this . allowances . bulkAdd ( result . allowances ) ;
70
- }
71
- if ( result . payments ) {
72
- await this . payments . bulkAdd ( result . payments ) ;
73
- }
74
- return true ;
75
- } catch ( e ) {
76
- console . log ( "Failed to load DB data from storage" ) ;
77
- console . log ( e ) ;
78
- }
62
+ console . log ( "Loading DB data from storage" ) ;
63
+ return browser . storage . local
64
+ . get ( [ "allowances" , "payments" ] )
65
+ . then ( ( result ) => {
66
+ const allowancePromise = this . allowances . count ( ) . then ( ( count ) => {
67
+ // if the DB already has entries we do not need to add the data from the browser storage. We then already have the data in the indexeddb
68
+ if ( count > 0 ) {
69
+ console . log ( `Found ${ count } allowances already in the DB` ) ;
70
+ return ;
71
+ } else if ( result . allowances && result . allowances . length > 0 ) {
72
+ // adding the data from the browser storage
73
+ return this . allowances
74
+ . bulkAdd ( result . allowances )
75
+ . catch ( Dexie . BulkError , function ( e ) {
76
+ console . log ( "Failed to add allowances; ignoring" ) ;
77
+ console . error ( e ) ;
78
+ } ) ;
79
+ }
80
+ } ) ;
81
+
82
+ const paymentsPromise = this . payments . count ( ) . then ( ( count ) => {
83
+ // if the DB already has entries we do not need to add the data from the browser storage. We then already have the data in the indexeddb
84
+ if ( count > 0 ) {
85
+ console . log ( `Found ${ count } payments already in the DB` ) ;
86
+ return ;
87
+ } else if ( result . payments && result . payments . length > 0 ) {
88
+ // adding the data from the browser storage
89
+ return this . payments
90
+ . bulkAdd ( result . payments )
91
+ . catch ( Dexie . BulkError , function ( e ) {
92
+ console . log ( "Failed to add payments; ignoring" ) ;
93
+ console . error ( e ) ;
94
+ } ) ;
95
+ }
96
+ } ) ;
97
+
98
+ // wait for all allowances and payments to be loaded
99
+ return Promise . all ( [ allowancePromise , paymentsPromise ] ) ;
100
+ } )
101
+ . catch ( ( e ) => {
102
+ console . log ( "Failed to load DB data from storage" ) ;
103
+ console . error ( e ) ;
104
+ } ) ;
79
105
}
80
106
}
81
107
0 commit comments