@@ -9,6 +9,7 @@ const crypto = require('crypto')
9
9
const writeActions = require ( '../constants/sync/proto' ) . actions
10
10
const { getSetting} = require ( '../settings' )
11
11
const { isDataUrl} = require ( '../lib/urlutil' )
12
+ const FunctionBuffer = require ( '../lib/functionBuffer' )
12
13
const bookmarkUtil = require ( '../../app/common/lib/bookmarkUtil' )
13
14
const bookmarkFoldersState = require ( '../../app/common/state/bookmarkFoldersState' )
14
15
const bookmarkFoldersUtil = require ( '../../app/common/lib/bookmarkFoldersUtil' )
@@ -282,21 +283,7 @@ module.exports.applySyncRecords = (records) => {
282
283
* bookmark sites), then when the apply action changes (e.g. to folders) we
283
284
* first flush the buffer (e.g. apply the first sequence of sites).
284
285
*/
285
- let buffer = [ ]
286
- let previousAppAction = ( ) => { }
287
- const flushBufferedAppActions = ( ) => {
288
- if ( ! buffer . length ) { return }
289
- previousAppAction ( new Immutable . List ( buffer ) )
290
- buffer = [ ]
291
- }
292
- const bufferAppAction = ( appAction , item ) => {
293
- if ( previousAppAction !== appAction ) {
294
- flushBufferedAppActions ( )
295
- previousAppAction = appAction
296
- }
297
- buffer . push ( item )
298
- }
299
-
286
+ const functionBuffer = new FunctionBuffer ( ( args ) => new Immutable . List ( args ) )
300
287
records . forEach ( ( record ) => {
301
288
if ( ! record ) {
302
289
return true
@@ -305,32 +292,32 @@ module.exports.applySyncRecords = (records) => {
305
292
const siteData = module . exports . getSiteDataFromRecord ( record , appState , records ) . set ( 'skipSync' , true )
306
293
if ( shouldAddRecord ( record ) ) {
307
294
if ( record . bookmark . isFolder ) {
308
- bufferAppAction ( appActions . addBookmarkFolder , siteData )
295
+ functionBuffer . buffer ( appActions . addBookmarkFolder , siteData )
309
296
} else {
310
- bufferAppAction ( appActions . addBookmark , siteData )
297
+ functionBuffer . buffer ( appActions . addBookmark , siteData )
311
298
}
312
299
} else if ( shouldRemoveRecord ( record ) ) {
313
300
if ( record . bookmark . isFolder ) {
314
301
const folderKey = bookmarkFoldersUtil . getKey ( siteData )
315
- bufferAppAction ( appActions . removeBookmarkFolder , folderKey )
302
+ functionBuffer . buffer ( appActions . removeBookmarkFolder , folderKey )
316
303
} else {
317
304
const siteKey = bookmarkUtil . getKey ( siteData )
318
- bufferAppAction ( appActions . removeBookmark , siteKey )
305
+ functionBuffer . buffer ( appActions . removeBookmark , siteKey )
319
306
}
320
307
}
321
308
} else if ( record . objectData === 'historySite' ) {
322
309
const siteData = module . exports . getSiteDataFromRecord ( record , appState , records )
323
310
if ( shouldAddRecord ( record ) ) {
324
- bufferAppAction ( appActions . addHistorySite , siteData )
311
+ functionBuffer . buffer ( appActions . addHistorySite , siteData )
325
312
} else if ( shouldRemoveRecord ( record ) ) {
326
313
const siteKey = historyUtil . getKey ( siteData )
327
- bufferAppAction ( appActions . removeHistorySite , siteKey )
314
+ functionBuffer . buffer ( appActions . removeHistorySite , siteKey )
328
315
}
329
316
} else {
330
317
otherRecords . push ( record )
331
318
}
332
319
} )
333
- flushBufferedAppActions ( )
320
+ functionBuffer . flush ( )
334
321
applyNonBatchedRecords ( otherRecords )
335
322
}
336
323
0 commit comments