@@ -7,6 +7,8 @@ export class WebStorageService {
77 private static readonly SETTINGS_KEY = 'sync_settings' ;
88 private static readonly DELETE_RETENTION_DAYS = 1 ;
99 private static crypto : CryptoService | null = null ;
10+ private static lastSyncTime : number = 0 ;
11+ private static readonly MIN_SYNC_INTERVAL = 5000 ; // 5 seconds between syncs
1012
1113 static async initializeCrypto ( seedPhrase : string ) {
1214 this . crypto = await CryptoService . new ( seedPhrase ) ;
@@ -106,6 +108,15 @@ export class WebStorageService {
106108 throw new Error ( 'Crypto not initialized' ) ;
107109 }
108110
111+ // Check if we've synced recently
112+ const now = Date . now ( ) ;
113+ const timeSinceLastSync = now - this . lastSyncTime ;
114+
115+ if ( timeSinceLastSync < this . MIN_SYNC_INTERVAL ) {
116+ console . log ( `Throttling sync request. Last sync was ${ timeSinceLastSync } ms ago.` ) ;
117+ await new Promise ( resolve => setTimeout ( resolve , this . MIN_SYNC_INTERVAL - timeSinceLastSync ) ) ;
118+ }
119+
109120 let lastError : Error | null = null ;
110121
111122 for ( let attempt = 0 ; attempt < retries ; attempt ++ ) {
@@ -159,12 +170,24 @@ export class WebStorageService {
159170
160171 localStorage . setItem ( this . NOTES_KEY , JSON . stringify ( mergedNotes ) ) ;
161172 await this . purgeDeletedNotes ( ) ;
173+
174+ // Update last sync time on success
175+ this . lastSyncTime = Date . now ( ) ;
162176 return ;
163177
164178 } catch ( error ) {
165179 console . error ( `Sync attempt ${ attempt + 1 } failed:` , error ) ;
166180 lastError = error as Error ;
167181
182+ const errorMessage = error instanceof Error ? error . message : String ( error ) ;
183+
184+ if ( errorMessage . includes ( 'Too many requests' ) ) {
185+ const waitTime = Math . pow ( 2 , attempt + 2 ) * 1000 ;
186+ console . log ( `Rate limited. Waiting ${ waitTime / 1000 } seconds before retry...` ) ;
187+ await new Promise ( resolve => setTimeout ( resolve , waitTime ) ) ;
188+ continue ;
189+ }
190+
168191 if ( attempt < retries - 1 ) {
169192 await new Promise ( resolve =>
170193 setTimeout ( resolve , Math . pow ( 2 , attempt ) * 1000 )
0 commit comments