Add execEmotesEfficient behind feature flag

For #645.  Disabled by default, I'll selectively enable it to be sure it
works and then remove the old implementation.
This commit is contained in:
Calvin Montgomery 2017-03-01 21:16:55 -08:00
parent d4db459ff9
commit 20326194f7
4 changed files with 53 additions and 1 deletions

View File

@ -2,7 +2,7 @@
"author": "Calvin Montgomery", "author": "Calvin Montgomery",
"name": "CyTube", "name": "CyTube",
"description": "Online media synchronizer and chat", "description": "Online media synchronizer and chat",
"version": "3.30.0", "version": "3.30.1",
"repository": { "repository": {
"url": "http://github.com/calzoneman/sync" "url": "http://github.com/calzoneman/sync"
}, },

View File

@ -997,9 +997,22 @@ Callbacks = {
break; break;
} }
} }
for (var i = 0; i < CHANNEL.badEmotes.length; i++) {
if (CHANNEL.badEmotes[i].name === data.name) {
CHANNEL.badEmotes[i] = data;
break;
}
}
if (!found) { if (!found) {
CHANNEL.emotes.push(data); CHANNEL.emotes.push(data);
if (/\s/g.test(data.name)) {
CHANNEL.badEmotes.push(data);
} else {
CHANNEL.emoteMap[data.name] = data;
}
} else {
CHANNEL.emoteMap[data.name] = data;
} }
EMOTELIST.handleChange(); EMOTELIST.handleChange();
@ -1019,6 +1032,13 @@ Callbacks = {
var row = $("code:contains('" + data.name + "')").parent().parent(); var row = $("code:contains('" + data.name + "')").parent().parent();
row.hide("fade", row.remove.bind(row)); row.hide("fade", row.remove.bind(row));
CHANNEL.emotes.splice(i, 1); CHANNEL.emotes.splice(i, 1);
delete CHANNEL.emoteMap[data.name];
for (var i = 0; i < CHANNEL.badEmotes.length; i++) {
if (CHANNEL.badEmotes[i].name === data.name) {
CHANNEL.badEmotes.splice(i, 1);
break;
}
}
} }
}, },

View File

@ -70,6 +70,9 @@ var HAS_CONNECTED_BEFORE = false;
var IMAGE_MATCH = /<img\s[^>]*?src\s*=\s*['\"]([^'\"]*?)['\"][^>]*?>/gi; var IMAGE_MATCH = /<img\s[^>]*?src\s*=\s*['\"]([^'\"]*?)['\"][^>]*?>/gi;
var CyTube = {}; var CyTube = {};
CyTube.ui = {}; CyTube.ui = {};
CyTube.featureFlag = {
efficientEmotes: false
};
function getOpt(k) { function getOpt(k) {
var v = NO_STORAGE ? readCookie(k) : localStorage.getItem(k); var v = NO_STORAGE ? readCookie(k) : localStorage.getItem(k);

View File

@ -2649,10 +2649,18 @@ function formatUserPlaylistList() {
function loadEmotes(data) { function loadEmotes(data) {
CHANNEL.emotes = []; CHANNEL.emotes = [];
CHANNEL.emoteMap = {};
CHANNEL.badEmotes = [];
data.forEach(function (e) { data.forEach(function (e) {
if (e.image && e.name) { if (e.image && e.name) {
e.regex = new RegExp(e.source, "gi"); e.regex = new RegExp(e.source, "gi");
CHANNEL.emotes.push(e); CHANNEL.emotes.push(e);
if (/\s/g.test(e.name)) {
// Emotes with spaces can't be hashmapped
CHANNEL.badEmotes.push(e);
} else {
CHANNEL.emoteMap[e.name] = e.image;
}
} else { } else {
console.error("Rejecting invalid emote: " + JSON.stringify(e)); console.error("Rejecting invalid emote: " + JSON.stringify(e));
} }
@ -2664,6 +2672,11 @@ function execEmotes(msg) {
return msg; return msg;
} }
if (CyTube.featureFlag && CyTube.featureFlag.efficientEmotes) {
execEmotesEfficient(msg);
return;
}
CHANNEL.emotes.forEach(function (e) { CHANNEL.emotes.forEach(function (e) {
msg = msg.replace(e.regex, '$1<img class="channel-emote" src="' + msg = msg.replace(e.regex, '$1<img class="channel-emote" src="' +
e.image + '" title="' + e.name + '">'); e.image + '" title="' + e.name + '">');
@ -2672,6 +2685,22 @@ function execEmotes(msg) {
return msg; return msg;
} }
function execEmotesEfficient(msg) {
CHANNEL.badEmotes.forEach(function (e) {
msg = msg.replace(e.regex, '$1<img class="channel-emote" src="' +
e.image + '" title="' + e.name + '">');
});
msg = msg.replace(/[^\s]+/g, function (m) {
if (CHANNEL.emoteMap.hasOwnProperty(m)) {
var e = CHANNEL.emoteMap[m];
return '<img class="channel-emote" src="' + e.image + '" title="' + e.name + '">';
} else {
return m;
}
});
return msg;
}
function initPm(user) { function initPm(user) {
if ($("#pm-" + user).length > 0) { if ($("#pm-" + user).length > 0) {
return $("#pm-" + user); return $("#pm-" + user);