[ticket/13713] Improve client-side caching

PHPBB3-13713
This commit is contained in:
lavigor 2018-07-13 07:24:35 +03:00 committed by Marc Alexander
parent 0aadd52014
commit 0269d53c5d
No known key found for this signature in database
GPG key ID: 50E0D2423696F995

View file

@ -387,15 +387,35 @@ function getCaretPosition(txtarea) {
(function($) {
function Mentions() {
let $mentionDataContainer = $('[data-mention-url]:first');
let mentionURL = $mentionDataContainer.data('mentionUrl');
let mentionBatchSize = $mentionDataContainer.data('mentionBatchSize');
let mentionNamesLimit = $mentionDataContainer.data('mentionNamesLimit');
let mentionTopicId = $mentionDataContainer.data('topicId');
let queryInProgress = null;
let cachedNames = null;
let cachedFor = null;
let cachedNames = [];
let cachedSearchKey = 'name';
function defaultAvatar(type) {
return (type === 'group') ? '<svg class="mention-media-avatar" xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24"><path fill-rule="evenodd" d="M16 11c1.66 0 2.99-1.34 2.99-3S17.66 5 16 5c-1.66 0-3 1.34-3 3s1.34 3 3 3zm-8 0c1.66 0 2.99-1.34 2.99-3S9.66 5 8 5C6.34 5 5 6.34 5 8s1.34 3 3 3zm0 2c-2.33 0-7 1.17-7 3.5V19h14v-2.5c0-2.33-4.67-3.5-7-3.5zm8 0c-.29 0-.62.02-.97.05 1.16.84 1.97 1.97 1.97 3.45V19h6v-2.5c0-2.33-4.67-3.5-7-3.5z"/></svg>' : '<svg class="mention-media-avatar" xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24"><path fill-rule="evenodd" d="M12,19.2C9.5,19.2 7.29,17.92 6,16C6.03,14 10,12.9 12,12.9C14,12.9 17.97,14 18,16C16.71,17.92 14.5,19.2 12,19.2M12,5A3,3 0 0,1 15,8A3,3 0 0,1 12,11A3,3 0 0,1 9,8A3,3 0 0,1 12,5M12,2A10,10 0 0,0 2,12A10,10 0 0,0 12,22A10,10 0 0,0 22,12C22,6.47 17.5,2 12,2Z"/></svg>';
}
function getCachedNames(query) {
if (!cachedNames) {
return null;
}
let i;
for (i = query.length; i > 0; i--) {
let startStr = query.substr(0, i);
if (cachedNames[startStr]) {
return cachedNames[startStr];
}
}
return cachedNames[''];
}
function getMatchedNames(query, items, searchKey) {
let i;
let len;
@ -413,15 +433,50 @@ function getCaretPosition(txtarea) {
return getMatchedNames(query, cachedNames, cachedSearchKey).length;
}
function remoteFilter(query, callback) {
/*
* Do not make a new request until the previous one for the same query is returned
* This fixes duplicate server queries e.g. when arrow keys are pressed
*/
if (queryInProgress === query) {
setTimeout(function() {
remoteFilter(query, callback);
}, 1000);
return;
}
let cachedNamesForQuery = getCachedNames(query);
/*
* Use cached values when we can:
* 1) There are some names in the cache relevant for the query
* (cache for the query with the same first characters cointains some data)
* 2) We have enough names to display OR
* all relevant names have been fetched from the server
*/
if (cachedNamesForQuery &&
(getNumberOfMatchedCachedNames(query) >= mentionNamesLimit ||
cachedNamesForQuery.length < mentionBatchSize)) {
callback(cachedNamesForQuery);
return;
}
queryInProgress = query;
let params = {keyword: query, topic_id: mentionTopicId, _referer: location.href};
$.getJSON(mentionURL, params, function(data) {
cachedNames[query] = data;
callback(data);
}).always(function() {
queryInProgress = null;
});
}
this.isEnabled = function() {
return $mentionDataContainer.length;
};
this.handle = function(txtarea) {
let mentionURL = $mentionDataContainer.data('mentionUrl');
let mentionBatchSize = $mentionDataContainer.data('mentionBatchSize');
let mentionNamesLimit = $mentionDataContainer.data('mentionNamesLimit');
let mentionTopicId = $mentionDataContainer.data('topicId');
$(txtarea).atwho({
at: "@",
acceptSpaceBar: true,
@ -434,40 +489,7 @@ function getCaretPosition(txtarea) {
insertTpl: "[mention=${type}:${id}]${name}[/mention]",
limit: mentionNamesLimit,
callbacks: {
remoteFilter: function(query, callback) {
/*
* Use cached values when we can:
* 1) There are some names in the cache
* 2) The cache contains relevant data for the query
* (it was made for the query with the same first characters)
* 3) We have enough names to display OR
* all relevant names have been fetched from the server
*/
if (cachedNames &&
query.indexOf(cachedFor) === 0 &&
(getNumberOfMatchedCachedNames(query) >= mentionNamesLimit ||
cachedNames.length < mentionBatchSize)) {
callback(cachedNames);
return;
}
/*
* Do not make a new request until the previous one for the same query is returned
* This fixes duplicate server queries e.g. when arrow keys are pressed
*/
if (queryInProgress === query) {
return;
}
queryInProgress = query;
let params = {keyword: query, topic_id: mentionTopicId, _referer: location.href};
$.getJSON(mentionURL, params, function (data) {
cachedNames = data;
cachedFor = query;
queryInProgress = null;
callback(data);
});
},
remoteFilter: remoteFilter,
sorter: function(query, items, searchKey) {
let i;
let len;