@@ -47,8 +47,13 @@ export async function decryptNote(note: Note, userId: string): Promise<Note> {
4747 }
4848}
4949
50+ // Yield to main thread to prevent UI blocking
51+ const yieldToMain = ( ) : Promise < void > => {
52+ return new Promise ( resolve => setTimeout ( resolve , 0 ) ) ;
53+ } ;
54+
5055/**
51- * Decrypts an array of notes
56+ * Decrypts an array of notes in batches to prevent UI blocking
5257 */
5358export async function decryptNotes (
5459 notes : Note [ ] ,
@@ -61,22 +66,35 @@ export async function decryptNotes(
6166 const decryptStart = performance . now ( ) ;
6267 console . log ( `[PERF] Starting decryption of ${ notes . length } notes...` ) ;
6368
69+ const BATCH_SIZE = 10 ; // Process 10 notes at a time
70+ const result : Note [ ] = [ ] ;
71+
6472 try {
65- // Decrypt notes individually to handle failures gracefully
66- const result = await Promise . all (
67- notes . map ( async ( note ) => {
68- try {
69- return await decryptNote ( note , userId ) ;
70- } catch {
71- // Return note with fallback content if decryption fails
72- return {
73- ...note ,
74- title : note . title || '[Encrypted - Unable to decrypt]' ,
75- content : note . content || '[This note could not be decrypted]' ,
76- } ;
77- }
78- } )
79- ) ;
73+ // Process notes in batches to prevent UI blocking
74+ for ( let i = 0 ; i < notes . length ; i += BATCH_SIZE ) {
75+ const batch = notes . slice ( i , i + BATCH_SIZE ) ;
76+
77+ const decryptedBatch = await Promise . all (
78+ batch . map ( async ( note ) => {
79+ try {
80+ return await decryptNote ( note , userId ) ;
81+ } catch {
82+ return {
83+ ...note ,
84+ title : note . title || '[Encrypted - Unable to decrypt]' ,
85+ content : note . content || '[This note could not be decrypted]' ,
86+ } ;
87+ }
88+ } )
89+ ) ;
90+
91+ result . push ( ...decryptedBatch ) ;
92+
93+ // Yield to main thread between batches to keep UI responsive
94+ if ( i + BATCH_SIZE < notes . length ) {
95+ await yieldToMain ( ) ;
96+ }
97+ }
8098
8199 const decryptEnd = performance . now ( ) ;
82100 console . log ( `[PERF] Decryption completed in ${ ( decryptEnd - decryptStart ) . toFixed ( 2 ) } ms for ${ notes . length } notes` ) ;
@@ -86,7 +104,7 @@ export async function decryptNotes(
86104 if ( __DEV__ ) {
87105 console . error ( 'Error during note decryption batch:' , error ) ;
88106 }
89- return notes ; // Return original notes if batch decryption fails
107+ return notes ;
90108 }
91109}
92110
0 commit comments