@@ -51,8 +51,6 @@ export default class Scanner<L1 extends TItemLocation, L2 extends TItemLocation>
5151 }
5252
5353 async diffItem ( oldItem :TItem < L1 > , newItem :TItem < L2 > ) :Promise < void > {
54- // give the browser time to breathe
55- await yieldToEventLoop ( )
5654 if ( oldItem . type === 'folder' && newItem . type === 'folder' ) {
5755 return this . diffFolder ( oldItem , newItem )
5856 } else if ( oldItem . type === 'bookmark' && newItem . type === 'bookmark' ) {
@@ -63,6 +61,8 @@ export default class Scanner<L1 extends TItemLocation, L2 extends TItemLocation>
6361 }
6462
6563 async diffFolder ( oldFolder :Folder < L1 > , newFolder :Folder < L2 > ) :Promise < void > {
64+ // give the browser time to breathe
65+ await yieldToEventLoop ( )
6666 if ( this . checkHashes ) {
6767 const hasChanged = await this . folderHasChanged ( oldFolder , newFolder )
6868 if ( ! hasChanged ) {
@@ -153,6 +153,7 @@ export default class Scanner<L1 extends TItemLocation, L2 extends TItemLocation>
153153 let createActions
154154 let removeActions
155155 let reconciled = true
156+ let iterations = 0
156157
157158 // As soon as one match is found, action list is updated and search is started with the new list
158159 // repeat until no rewrites happen anymore
@@ -163,26 +164,31 @@ export default class Scanner<L1 extends TItemLocation, L2 extends TItemLocation>
163164 // First find direct matches (avoids glitches when folders and their contents have been moved)
164165 createActions = this . result . CREATE . getActions ( )
165166 while ( ! reconciled && ( createAction = createActions . shift ( ) ) ) {
166- // give the browser time to breathe
167- await Promise . resolve ( )
167+ if ( ++ iterations % 1000 === 0 ) {
168+ // give the browser time to breathe
169+ await yieldToEventLoop ( )
170+ }
168171 const createdItem = createAction . payload
169172 removeActions = this . result . REMOVE . getActions ( )
170173 while ( ! reconciled && ( removeAction = removeActions . shift ( ) ) ) {
171174 // give the browser time to breathe
172175 await Promise . resolve ( )
173176 const removedItem = removeAction . payload
174177
175- if ( this . mergeable ( removedItem , createdItem ) &&
178+ if (
179+ this . mergeable ( removedItem , createdItem ) &&
176180 ( removedItem . type !== 'folder' ||
177- ( ! this . hasCache && removedItem . childrenSimilarity ( createdItem ) > 0.8 ) ) ) {
181+ ( ! this . hasCache &&
182+ removedItem . childrenSimilarity ( createdItem ) > 0.8 ) )
183+ ) {
178184 this . result . CREATE . retract ( createAction )
179185 this . result . REMOVE . retract ( removeAction )
180186 this . result . MOVE . commit ( {
181187 type : ActionType . MOVE ,
182188 payload : createdItem ,
183189 oldItem : removedItem ,
184190 index : createAction . index ,
185- oldIndex : removeAction . index
191+ oldIndex : removeAction . index ,
186192 } )
187193 reconciled = true
188194 // Don't use the items from the action, but the ones in the actual tree to avoid using tree parts mutated by this algorithm (see below)
@@ -194,8 +200,10 @@ export default class Scanner<L1 extends TItemLocation, L2 extends TItemLocation>
194200 // Then find descendant matches
195201 createActions = this . result . CREATE . getActions ( )
196202 while ( ! reconciled && ( createAction = createActions . shift ( ) ) ) {
197- // give the browser time to breathe
198- await Promise . resolve ( )
203+ if ( ++ iterations % 1000 === 0 ) {
204+ // give the browser time to breathe
205+ await yieldToEventLoop ( )
206+ }
199207 const createdItem = createAction . payload
200208 removeActions = this . result . REMOVE . getActions ( )
201209 while ( ! reconciled && ( removeAction = removeActions . shift ( ) ) ) {
@@ -230,7 +238,7 @@ export default class Scanner<L1 extends TItemLocation, L2 extends TItemLocation>
230238 oldIndex : oldIndex || removeAction . index
231239 } )
232240 reconciled = true
233- if ( oldItem . type === ItemType . FOLDER ) { // TODO: Is this necessary?
241+ if ( oldItem . type === ItemType . FOLDER ) {
234242 await this . diffItem ( oldItem , createdItem )
235243 }
236244 } else {
0 commit comments