speed up stats computation

This commit is contained in:
Nazar Kanaev 2020-09-08 22:24:30 +01:00
parent 8d83d608cd
commit da0626df4e

View File

@ -143,6 +143,11 @@ var vm = new Vue({
'itemSearch': '', 'itemSearch': '',
'itemSortNewestFirst': null, 'itemSortNewestFirst': null,
'itemListWidth': null, 'itemListWidth': null,
'filteredFeedStats': {},
'filteredFolderStats': {},
'filteredTotalStats': null,
'settings': 'create', 'settings': 'create',
'loading': { 'loading': {
'feeds': false, 'feeds': false,
@ -181,35 +186,6 @@ var vm = new Vue({
itemsById: function() { itemsById: function() {
return this.items.reduce(function(acc, item) { acc[item.id] = item; return acc }, {}) return this.items.reduce(function(acc, item) { acc[item.id] = item; return acc }, {})
}, },
filteredFeedStats: function() {
var filter = this.filterSelected
if (filter != 'unread' && filter != 'starred') return {}
var feedStats = this.feedStats
return this.feeds.reduce(function(acc, feed) {
if (feedStats[feed.id]) acc[feed.id] = vm.feedStats[feed.id][filter]
return acc
}, {})
},
filteredFolderStats: function() {
var filter = this.filterSelected
if (filter != 'unread' && filter != 'starred') return {}
var feedStats = this.filteredFeedStats
return this.feeds.reduce(function(acc, feed) {
if (!acc[feed.folder_id]) acc[feed.folder_id] = 0
if (feedStats[feed.id]) acc[feed.folder_id] += feedStats[feed.id]
return acc
}, {})
},
filteredTotalStats: function() {
var filter = this.filterSelected
if (filter != 'unread' && filter != 'starred') return ''
return Object.values(this.feedStats).reduce(function(acc, stat) {
return acc + stat[filter]
}, 0)
},
itemSelectedContent: function() { itemSelectedContent: function() {
if (!this.itemSelected) return '' if (!this.itemSelected) return ''
@ -248,12 +224,14 @@ var vm = new Vue({
title += ' ('+unreadCount+')' title += ' ('+unreadCount+')'
} }
document.title = title document.title = title
this.computeStats()
}, 500), }, 500),
}, },
'filterSelected': function(newVal, oldVal) { 'filterSelected': function(newVal, oldVal) {
if (oldVal === null) return // do nothing, initial setup if (oldVal === null) return // do nothing, initial setup
api.settings.update({filter: newVal}).then(this.refreshItems.bind(this)) api.settings.update({filter: newVal}).then(this.refreshItems.bind(this))
this.itemSelected = null this.itemSelected = null
this.computeStats()
}, },
'feedSelected': function(newVal, oldVal) { 'feedSelected': function(newVal, oldVal) {
if (oldVal === null) return // do nothing, initial setup if (oldVal === null) return // do nothing, initial setup
@ -540,5 +518,33 @@ var vm = new Vue({
fetchAllFeeds: function() { fetchAllFeeds: function() {
api.feeds.refresh().then(this.refreshStats.bind(this)) api.feeds.refresh().then(this.refreshStats.bind(this))
}, },
computeStats: function() {
var filter = this.filterSelected
if (!filter) {
this.filteredFeedStats = {}
this.filteredFolderStats = {}
this.filteredTotalStats = null
return
}
var statsFeeds = {}, statsFolders = {}, statsTotal = 0
for (var i = 0; i < this.feeds.length; i++) {
var feed = this.feeds[i]
if (!this.feedStats[feed.id]) continue
var n = vm.feedStats[feed.id][filter] || 0
if (!statsFolders[feed.folder_id]) statsFolders[feed.folder_id] = 0
statsFeeds[feed.id] = n
statsFolders[feed.folder_id] += n
statsTotal += n
}
this.filteredFeedStats = statsFeeds
this.filteredFolderStats = statsFolders
this.filteredTotalStats = statsTotal
},
} }
}) })