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

Commit 57ff66e

Browse files
committed
Reduce redundant siteSort to sort once and used everywhere
fix #7240 Auditors: @bbondy, @bsclifton Test Plan: 1. Import bunch of bookmarks 2. Open bookmarks of menu shouldn't affect performance 3. Open about:bookmarks shouldn't affect performance
1 parent b097dcb commit 57ff66e

File tree

8 files changed

+25
-16
lines changed

8 files changed

+25
-16
lines changed

app/browser/bookmarksExporter.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ function createBookmarkArray (sites, parentFolderId, first = true, depth = 1) {
5252

5353
if (first) payload.push(`${indentFirst}<DL><p>`)
5454

55-
filteredBookmarks.toList().sort(siteUtil.siteSort).forEach((site) => {
55+
filteredBookmarks.forEach((site) => {
5656
if (site.get('tags').includes(siteTags.BOOKMARK) && site.get('location')) {
5757
title = site.get('customTitle') || site.get('title') || site.get('location')
5858
payload.push(`${indentNext}<DT><A HREF="${site.get('location')}">${title}</A>`)

app/browser/menu.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const menuUtil = require('../common/lib/menuUtil')
2626
const {getByTabId} = require('../common/state/tabState')
2727
const getSetting = require('../../js/settings').getSetting
2828
const locale = require('../locale')
29-
const {isLocationBookmarked, siteSort} = require('../../js/state/siteUtil')
29+
const {isLocationBookmarked} = require('../../js/state/siteUtil')
3030
const tabState = require('../../app/common/state/tabState')
3131
const isDarwin = process.platform === 'darwin'
3232
const isLinux = process.platform === 'linux'
@@ -381,7 +381,7 @@ const createBookmarksSubmenu = () => {
381381
CommonMenu.exportBookmarksMenuItem()
382382
]
383383

384-
const bookmarks = menuUtil.createBookmarkTemplateItems(appStore.getState().get('sites').toList().sort(siteSort))
384+
const bookmarks = menuUtil.createBookmarkTemplateItems(appStore.getState().get('sites'))
385385
if (bookmarks.length > 0) {
386386
submenu.push(CommonMenu.separatorMenuItem)
387387
submenu = submenu.concat(bookmarks)

app/browser/reducers/sitesReducer.js

+3
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ const sitesReducer = (state, action, immutableAction) => {
6969
sourceKey, destinationKey, false, false, true)
7070
}
7171
}
72+
state = state.set('sites', state.get('sites').sort(siteUtil.siteSort))
7273
if (syncEnabled()) {
7374
state = syncUtil.updateSiteCache(state, action.destinationDetail || action.siteDetail)
7475
}
@@ -77,6 +78,7 @@ const sitesReducer = (state, action, immutableAction) => {
7778
case appConstants.APP_REMOVE_SITE:
7879
const removeSiteSyncCallback = action.skipSync ? undefined : syncActions.removeSite
7980
state = siteUtil.removeSite(state, action.siteDetail, action.tag, true, removeSiteSyncCallback)
81+
state = state.set('sites', state.get('sites').sort(siteUtil.siteSort))
8082
if (syncEnabled()) {
8183
state = syncUtil.updateSiteCache(state, action.siteDetail)
8284
}
@@ -86,6 +88,7 @@ const sitesReducer = (state, action, immutableAction) => {
8688
state = siteUtil.moveSite(state,
8789
action.sourceKey, action.destinationKey, action.prepend,
8890
action.destinationIsParent, false)
91+
state = state.set('sites', state.get('sites').sort(siteUtil.siteSort))
8992
if (syncEnabled()) {
9093
const destinationDetail = state.getIn(['sites', action.destinationKey])
9194
state = syncUtil.updateSiteCache(state, destinationDetail)

app/browser/tabs.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ const settings = require('../../js/constants/settings')
1818
const {getBaseUrl, aboutUrls} = require('../../js/lib/appUrlUtil')
1919
const siteSettings = require('../../js/state/siteSettings')
2020
const messages = require('../../js/constants/messages')
21-
const siteUtil = require('../../js/state/siteUtil')
2221
const aboutHistoryState = require('../common/state/aboutHistoryState')
2322
const appStore = require('../../js/stores/appStore')
2423
const appConfig = require('../../js/constants/appConfig')
@@ -135,8 +134,8 @@ ipcMain.on(messages.ABOUT_COMPONENT_INITIALIZED, (e) => {
135134
})
136135

137136
const getBookmarksData = function (state) {
138-
let bookmarkSites = new Immutable.Map()
139-
let bookmarkFolderSites = new Immutable.Map()
137+
let bookmarkSites = new Immutable.OrderedMap()
138+
let bookmarkFolderSites = new Immutable.OrderedMap()
140139
state.get('sites').forEach((site, siteKey) => {
141140
const tags = site.get('tags')
142141
if (tags.includes(siteTags.BOOKMARK)) {
@@ -146,8 +145,8 @@ const getBookmarksData = function (state) {
146145
bookmarkFolderSites = bookmarkFolderSites.set(siteKey, site)
147146
}
148147
})
149-
const bookmarks = bookmarkSites.toList().sort(siteUtil.siteSort).toJS()
150-
const bookmarkFolders = bookmarkFolderSites.toList().sort(siteUtil.siteSort).toJS()
148+
const bookmarks = bookmarkSites.toList().toJS()
149+
const bookmarkFolders = bookmarkFolderSites.toList().toJS()
151150
return {bookmarks, bookmarkFolders}
152151
}
153152

app/browser/windows.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ const LocalShortcuts = require('../localShortcuts')
1212
const {getPinnedSiteProps} = require('../common/lib/windowsUtil')
1313
const {makeImmutable} = require('../common/state/immutableUtil')
1414
const {getPinnedTabsByWindowId} = require('../common/state/tabState')
15-
const {siteSort} = require('../../js/state/siteUtil')
1615
const messages = require('../../js/constants/messages')
1716
const settings = require('../../js/constants/settings')
1817
const siteTags = require('../../js/constants/siteTags')
@@ -84,7 +83,7 @@ const updatePinnedTabs = (win) => {
8483
const sitesToAdd = pinnedSites.filter((site) =>
8584
!win.__alreadyPinnedSites.find((pinned) => pinned.equals(site)))
8685

87-
sitesToAdd.sort(siteSort).forEach((site) => {
86+
sitesToAdd.forEach((site) => {
8887
win.__alreadyPinnedSites = win.__alreadyPinnedSites.add(site)
8988
appActions.createTabRequested({
9089
url: site.get('location'),

app/index.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ const privacy = require('../js/state/privacy')
7676
const async = require('async')
7777
const settings = require('../js/constants/settings')
7878
const BookmarksExporter = require('./browser/bookmarksExporter')
79+
const siteUtil = require('../js/state/siteUtil')
7980

8081
app.commandLine.appendSwitch('enable-features', 'BlockSmallPluginContent,PreferHtmlOverPlugins')
8182

@@ -321,7 +322,10 @@ app.on('ready', () => {
321322
// For tests we always want to load default app state
322323
const loadedPerWindowState = initialState.perWindowState
323324
delete initialState.perWindowState
324-
appActions.setState(Immutable.fromJS(initialState))
325+
// Retore map order after load
326+
let state = Immutable.fromJS(initialState)
327+
state = state.set('sites', state.get('sites').sort(siteUtil.siteSort))
328+
appActions.setState(state)
325329
setImmediate(() => perWindowStateLoaded(loadedPerWindowState))
326330
})
327331

app/renderer/components/bookmarks/bookmarksToolbar.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ class BookmarksToolbar extends ImmutableComponent {
108108
contextMenus.onShowBookmarkFolderMenu(this.bookmarks, bookmark, this.activeFrame, e)
109109
}
110110
updateBookmarkData (props) {
111-
this.bookmarks = siteUtil.getBookmarks(props.sites).toList().sort(siteUtil.siteSort)
111+
// TODO(darkdh): Remove siteSort when we have #9030 landed
112+
this.bookmarks = siteUtil.getBookmarks(props.sites).sort(siteUtil.siteSort)
112113

113114
const noParentItems = this.bookmarks
114115
.filter((bookmark) => !bookmark.get('parentFolderId'))
@@ -153,9 +154,9 @@ class BookmarksToolbar extends ImmutableComponent {
153154
break
154155
}
155156
}
156-
this.bookmarksForToolbar = noParentItems.take(i).sort(siteUtil.siteSort)
157+
this.bookmarksForToolbar = noParentItems.take(i)
157158
// Show at most 100 items in the overflow menu
158-
this.overflowBookmarkItems = noParentItems.skip(i).take(100).sort(siteUtil.siteSort)
159+
this.overflowBookmarkItems = noParentItems.skip(i).take(100)
159160
}
160161
componentWillMount () {
161162
this.updateBookmarkData(this.props)

test/unit/app/browser/reducers/sitesReducerTest.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ describe('sitesReducerTest', function () {
139139
newState = sitesReducer(newState, addAction)
140140
assert.equal(Object.keys(newState.get('sites').toJS()).length, 3)
141141

142+
// sites are sorted after #8075 landed
143+
// sites[0] will be order 0, sites[1] will be order 1...etc
144+
142145
// Move the site to the 2nd position
143146
newState = sitesReducer(newState, moveAction).toJS()
144147
assert.equal(Object.keys(newState.sites).length, 3)
@@ -149,8 +152,8 @@ describe('sitesReducerTest', function () {
149152
moveAction.prepend = true
150153
newState = sitesReducer(Immutable.fromJS(newState), moveAction).toJS()
151154
assert.equal(Object.keys(newState.sites).length, 3)
152-
assert.equal(Object.values(newState.sites)[2].location, url)
153-
assert.equal(Object.values(newState.sites)[2].order, 1)
155+
assert.equal(Object.values(newState.sites)[1].location, url)
156+
assert.equal(Object.values(newState.sites)[1].order, 1)
154157
})
155158
})
156159
})

0 commit comments

Comments
 (0)