@@ -87,6 +87,10 @@ export default class SyncProcess {
8787 protected cancelPromise : Promise < void >
8888 protected cancelCb : ( error : any ) => void
8989
90+ // We're counting the number of calls to addMappings so we can conditionally yieldToEventLoop every 1000th call
91+ protected mappingIterations = 0
92+ protected executeIterations = 0
93+
9094 constructor (
9195 mappings :Mappings ,
9296 localTree :TLocalTree ,
@@ -1038,7 +1042,9 @@ export default class SyncProcess {
10381042 donePlan : PlanStage3 < TOppositeLocation < L1 > , TItemLocation , L1 >
10391043 ) : Promise < void > {
10401044 // defer execution of actions to allow the this.canceled check below to work when cancelling in interrupt tests
1041- await yieldToEventLoop ( )
1045+ if ( ++ this . executeIterations % 1000 === 0 ) {
1046+ await yieldToEventLoop ( )
1047+ }
10421048 Logger . log ( 'Executing action ' , action )
10431049
10441050 if ( this . canceled ) {
@@ -1245,7 +1251,9 @@ export default class SyncProcess {
12451251 donePlan : PlanStage3 < TOppositeLocation < L1 > , TItemLocation , L1 >
12461252 ) : Promise < void > {
12471253 // defer execution of actions to allow the this.canceled check below to work when cancelling in interrupt tests
1248- await yieldToEventLoop ( )
1254+ if ( ++ this . executeIterations % 1000 === 0 ) {
1255+ await yieldToEventLoop ( )
1256+ }
12491257 Logger . log ( 'Executing action ' , action )
12501258
12511259 if ( this . canceled ) {
@@ -1268,7 +1276,9 @@ export default class SyncProcess {
12681276 diff : Diff < L1 , TItemLocation , UpdateAction < L1 , TItemLocation > | MoveAction < L1 , TItemLocation > > ,
12691277 donePlan : PlanStage3 < TItemLocation , TItemLocation , L1 > ) : Promise < void > {
12701278 // defer execution of actions to allow the this.canceled check below to work when cancelling in interrupt tests
1271- await yieldToEventLoop ( )
1279+ if ( ++ this . executeIterations % 1000 === 0 ) {
1280+ await yieldToEventLoop ( )
1281+ }
12721282 Logger . log ( 'Executing action ' , action )
12731283
12741284 if ( this . canceled ) {
@@ -1392,8 +1402,11 @@ export default class SyncProcess {
13921402
13931403 const isUsingTabs = await this . localTree . isUsingBrowserTabs ?.( )
13941404
1405+ let iterations = 0
13951406 await Parallel . each ( reorderings . getActions ( ) , async ( action ) => {
1396- await yieldToEventLoop ( )
1407+ if ( ++ iterations % 1000 === 0 ) {
1408+ await yieldToEventLoop ( )
1409+ }
13971410 Logger . log ( 'Executing reorder action' , `${ action . type } Payload: #${ action . payload . id } [${ action . payload . title } ]${ 'url' in action . payload ? `(${ action . payload . url } )` : '' } parentId: ${ action . payload . parentId } ` )
13981411 const item = action . payload
13991412
@@ -1433,7 +1446,9 @@ export default class SyncProcess {
14331446 }
14341447
14351448 async addMapping ( resource :TResource < TItemLocation > , item :TItem < TItemLocation > , newId :string | number ) :Promise < void > {
1436- await yieldToEventLoop ( )
1449+ if ( ++ this . mappingIterations % 1000 === 0 ) {
1450+ await yieldToEventLoop ( )
1451+ }
14371452 let localId , remoteId
14381453 if ( resource === this . server ) {
14391454 localId = item . id
@@ -1568,6 +1583,7 @@ export default class SyncProcess {
15681583 }
15691584 }
15701585 const membersToPersist = this . getMembersToPersist ( )
1586+ let iterations = 0
15711587 return {
15721588 strategy : 'default' ,
15731589 ...this . staticContinuation ,
@@ -1602,7 +1618,9 @@ export default class SyncProcess {
16021618 return [ key , await diff . toJSONAsync ( ) ]
16031619 }
16041620 if ( diff && diff . toJSON ) {
1605- await yieldToEventLoop ( )
1621+ if ( ++ iterations % 1000 === 0 ) {
1622+ await yieldToEventLoop ( )
1623+ }
16061624 return [ key , diff . toJSON ( ) ]
16071625 }
16081626 return [ key , diff ]
@@ -1615,7 +1633,9 @@ export default class SyncProcess {
16151633 return [ key , await value . toJSONAsync ( ) ]
16161634 }
16171635 if ( value && value . toJSON ) {
1618- await yieldToEventLoop ( )
1636+ if ( ++ iterations % 1000 === 0 ) {
1637+ await yieldToEventLoop ( )
1638+ }
16191639 return [ key , value . toJSON ( ) ]
16201640 }
16211641 return [ key , value ]
0 commit comments