diff --git a/app/js/services.js b/app/js/services.js index 60aab0b3..df664869 100644 --- a/app/js/services.js +++ b/app/js/services.js @@ -684,10 +684,12 @@ angular.module('myApp.services', []) var sendFilePromise = $q.when(); var tempID = -1; - var dialogsIndex = SearchIndexManager.createIndex(), cachedResults = {query: false}; + var lastSearchFilter = {}, + lastSearchResults = []; + NotificationsManager.start(); function getDialogs (query, maxID, limit) { @@ -888,7 +890,14 @@ angular.module('myApp.services', []) } function getSearch (inputPeer, query, inputFilter, maxID, limit) { - var foundMsgs = []; + var foundMsgs = [], + useSearchCache = !query, + sameSearchCache = useSearchCache && angular.equals(lastSearchFilter, inputFilter); + + if (useSearchCache && !sameSearchCache) { + lastSearchFilter = inputFilter; + lastSearchResults = []; + } if (!maxID && !query) { var peerID = AppPeersManager.getPeerID(inputPeer), @@ -927,9 +936,27 @@ angular.module('myApp.services', []) } } } + + // console.log(dT(), sameSearchCache, foundMsgs, lastSearchResults); + if (foundMsgs.length < neededLimit && lastSearchResults.length && sameSearchCache) { + var minID = foundMsgs.length ? foundMsgs[foundMsgs.length - 1] : 0xFFFFFFFF; + for (var i = 0; i < lastSearchResults.length; i++) { + if (lastSearchResults[i] < minID) { + foundMsgs.push(lastSearchResults[i]); + if (foundMsgs.length >= neededLimit) { + break; + } + } + } + } + // console.log(dT(), foundMsgs); } if (foundMsgs.length || limit == 1000) { + if (useSearchCache) { + lastSearchResults = listMergeSorted(lastSearchResults, foundMsgs); + } + return $q.when({ count: null, history: foundMsgs @@ -958,6 +985,10 @@ angular.module('myApp.services', []) foundMsgs.push(message.id); }); + if (useSearchCache) { + lastSearchResults = listMergeSorted(lastSearchResults, foundMsgs); + } + return { count: foundCount, history: foundMsgs diff --git a/app/js/util.js b/app/js/util.js index 33aef342..91d07162 100644 --- a/app/js/util.js +++ b/app/js/util.js @@ -77,3 +77,33 @@ function safeReplaceObject (wasObject, newObject) { } } +function listMergeSorted (list1, list2) { + list1 = list1 || []; + list2 = list2 || []; + + var result = angular.copy(list1); + + var minID = list1.length ? list1[list1.length - 1] : 0xFFFFFFFF; + for (var i = 0; i < list2.length; i++) { + if (list2[i] < minID) { + result.push(list2[i]); + } + } + + return result; +} + +function listUniqSorted (list) { + list = list || []; + var resultList = [], + prev = false; + for (var i = 0; i < list.length; i++) { + if (list[i] !== prev) { + resultList.push(list[i]) + } + prev = list[i]; + } + + return resultList; +} +