Skip to content
This repository was archived by the owner on Dec 11, 2019. It is now read-only.

Commit 0218896

Browse files
committed
Fix importer to import in order
1 parent 24ba49f commit 0218896

File tree

3 files changed

+79
-41
lines changed

3 files changed

+79
-41
lines changed

app/importer.js

+28-19
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ const locale = require('./locale')
3232
const tabMessageBox = require('./browser/tabMessageBox')
3333
const {makeImmutable} = require('./common/state/immutableUtil')
3434
const bookmarkFoldersUtil = require('./common/lib/bookmarkFoldersUtil')
35+
const FunctionBuffer = require('../js/lib/functionBuffer')
3536

3637
let isImportingBookmarks = false
3738
let hasBookmarks
@@ -121,36 +122,44 @@ importer.on('add-bookmarks', (e, importedBookmarks, topLevelFolder) => {
121122
let folders = []
122123
let bookmarks = []
123124
let topLevelFolderId = nextFolderIdObject.id++
125+
const functionBuffer = new FunctionBuffer((args) => makeImmutable(args), this)
124126

125-
folders.push({
127+
const importTopLevelFolder = {
126128
title: bookmarkFoldersUtil.getNextFolderName(bookmarkFolders, topLevelFolder),
127129
folderId: topLevelFolderId,
128130
parentFolderId: 0
129-
})
131+
}
132+
functionBuffer.buffer(appActions.addBookmarkFolder, importTopLevelFolder)
133+
folders.push(importTopLevelFolder)
130134

131135
for (let i = 0; i < importedBookmarks.length; ++i) {
132-
let path = importedBookmarks[i].path
133-
let parentFolderId = getParentFolderId(path, pathMap, folders, topLevelFolderId, nextFolderIdObject)
134-
if (importedBookmarks[i].is_folder) {
136+
const importedBookmark = importedBookmarks[i]
137+
const path = importedBookmark.path
138+
const title = importedBookmark.title
139+
const parentFolderId = getParentFolderId(path, pathMap, folders, topLevelFolderId, nextFolderIdObject)
140+
if (importedBookmark.is_folder) {
135141
const folderId = nextFolderIdObject.id++
136-
pathMap[importedBookmarks[i].title] = folderId
137-
folders.push({
138-
title: importedBookmarks[i].title,
139-
folderId: folderId,
140-
parentFolderId: parentFolderId
141-
})
142+
pathMap[title] = folderId
143+
const folder = {
144+
title,
145+
folderId,
146+
parentFolderId
147+
}
148+
functionBuffer.buffer(appActions.addBookmarkFolder, folder)
149+
folders.push(folder)
142150
} else {
143-
bookmarks.push({
144-
title: importedBookmarks[i].title,
145-
location: importedBookmarks[i].url,
146-
parentFolderId: parentFolderId
147-
})
151+
const location = importedBookmark.url
152+
const bookmark = {
153+
title,
154+
location,
155+
parentFolderId
156+
}
157+
functionBuffer.buffer(appActions.addBookmark, bookmark)
158+
bookmarks.push(bookmark)
148159
}
149160
}
150-
161+
functionBuffer.flush()
151162
bookmarkList = makeImmutable(bookmarks)
152-
appActions.addBookmarkFolder(makeImmutable(folders))
153-
appActions.addBookmark(bookmarkList)
154163
})
155164

156165
importer.on('add-favicons', (e, detail) => {

js/lib/functionBuffer.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
3+
* You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
5+
/**
6+
* Perform a sequence of function calls, grouping together contiguous function
7+
* calls for the same function different args.
8+
* See syncUtil.applySyncRecords() for a usage example.
9+
*/
10+
module.exports = class FunctionBuffer {
11+
/**
12+
* @param {function=} prepareArguments Before calling a function, run this function to prepare the buffered arguments.
13+
*/
14+
constructor (prepareArguments) {
15+
this.argumentsBuffer = []
16+
this.previousFunction = () => {}
17+
this.prepareArguments = prepareArguments || ((args) => args)
18+
}
19+
20+
/**
21+
* @param {function} fn Function to call. NOTE: this supports only calling with the first argument.
22+
* @param {any} arg Arg to buffer
23+
*/
24+
buffer (fn, arg) {
25+
if (fn !== this.previousFunction) {
26+
this.flush()
27+
this.previousFunction = fn
28+
}
29+
this.argumentsBuffer.push(arg)
30+
}
31+
32+
/**
33+
* Call previousFunction with buffered arguments and empty buffer.
34+
*/
35+
flush () {
36+
if (!this.argumentsBuffer.length) { return }
37+
const preparedArgs = this.prepareArguments(this.argumentsBuffer)
38+
const returnValue = this.previousFunction(preparedArgs)
39+
this.argumentsBuffer = []
40+
return returnValue
41+
}
42+
}

js/state/syncUtil.js

+9-22
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const crypto = require('crypto')
99
const writeActions = require('../constants/sync/proto').actions
1010
const {getSetting} = require('../settings')
1111
const {isDataUrl} = require('../lib/urlutil')
12+
const FunctionBuffer = require('../lib/functionBuffer')
1213
const bookmarkUtil = require('../../app/common/lib/bookmarkUtil')
1314
const bookmarkFoldersState = require('../../app/common/state/bookmarkFoldersState')
1415
const bookmarkFoldersUtil = require('../../app/common/lib/bookmarkFoldersUtil')
@@ -282,21 +283,7 @@ module.exports.applySyncRecords = (records) => {
282283
* bookmark sites), then when the apply action changes (e.g. to folders) we
283284
* first flush the buffer (e.g. apply the first sequence of sites).
284285
*/
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))
300287
records.forEach((record) => {
301288
if (!record) {
302289
return true
@@ -305,32 +292,32 @@ module.exports.applySyncRecords = (records) => {
305292
const siteData = module.exports.getSiteDataFromRecord(record, appState, records).set('skipSync', true)
306293
if (shouldAddRecord(record)) {
307294
if (record.bookmark.isFolder) {
308-
bufferAppAction(appActions.addBookmarkFolder, siteData)
295+
functionBuffer.buffer(appActions.addBookmarkFolder, siteData)
309296
} else {
310-
bufferAppAction(appActions.addBookmark, siteData)
297+
functionBuffer.buffer(appActions.addBookmark, siteData)
311298
}
312299
} else if (shouldRemoveRecord(record)) {
313300
if (record.bookmark.isFolder) {
314301
const folderKey = bookmarkFoldersUtil.getKey(siteData)
315-
bufferAppAction(appActions.removeBookmarkFolder, folderKey)
302+
functionBuffer.buffer(appActions.removeBookmarkFolder, folderKey)
316303
} else {
317304
const siteKey = bookmarkUtil.getKey(siteData)
318-
bufferAppAction(appActions.removeBookmark, siteKey)
305+
functionBuffer.buffer(appActions.removeBookmark, siteKey)
319306
}
320307
}
321308
} else if (record.objectData === 'historySite') {
322309
const siteData = module.exports.getSiteDataFromRecord(record, appState, records)
323310
if (shouldAddRecord(record)) {
324-
bufferAppAction(appActions.addHistorySite, siteData)
311+
functionBuffer.buffer(appActions.addHistorySite, siteData)
325312
} else if (shouldRemoveRecord(record)) {
326313
const siteKey = historyUtil.getKey(siteData)
327-
bufferAppAction(appActions.removeHistorySite, siteKey)
314+
functionBuffer.buffer(appActions.removeHistorySite, siteKey)
328315
}
329316
} else {
330317
otherRecords.push(record)
331318
}
332319
})
333-
flushBufferedAppActions()
320+
functionBuffer.flush()
334321
applyNonBatchedRecords(otherRecords)
335322
}
336323

0 commit comments

Comments
 (0)