Correctly dereference stored images when leaving channels

Fixes #1404
This commit is contained in:
Pavel Djundik 2017-08-11 15:02:58 +03:00
parent d39e8ba9f8
commit 6f32e7ae75
7 changed files with 26 additions and 12 deletions

View File

@ -488,6 +488,8 @@ Client.prototype.quit = function() {
if (network.irc) {
network.irc.quit("Page closed");
}
network.destroy();
});
};

View File

@ -30,6 +30,10 @@ function Chan(attr) {
});
}
Chan.prototype.destroy = function() {
this.dereferencePreviews(this.messages);
};
Chan.prototype.pushMessage = function(client, msg, increasesUnread) {
var obj = {
chan: this.id,
@ -56,12 +60,10 @@ Chan.prototype.pushMessage = function(client, msg, increasesUnread) {
if (Helper.config.maxHistory >= 0 && this.messages.length > Helper.config.maxHistory) {
const deleted = this.messages.splice(0, this.messages.length - Helper.config.maxHistory);
if (Helper.config.prefetch && Helper.config.prefetchStorage) {
deleted.forEach((deletedMessage) => {
if (deletedMessage.preview && deletedMessage.preview.thumb) {
storage.dereference(deletedMessage.preview.thumb);
}
});
// If maxHistory is 0, image would be dereferenced before client had a chance to retrieve it,
// so for now, just don't implement dereferencing for this edge case.
if (Helper.config.prefetch && Helper.config.prefetchStorage && Helper.config.maxHistory > 0) {
this.dereferencePreviews(deleted);
}
}
@ -76,6 +78,15 @@ Chan.prototype.pushMessage = function(client, msg, increasesUnread) {
}
};
Chan.prototype.dereferencePreviews = function(messages) {
messages.forEach((message) => {
if (message.preview && message.preview.thumb) {
storage.dereference(message.preview.thumb);
message.preview.thumb = null;
}
});
};
Chan.prototype.sortUsers = function(irc) {
var userModeSortPriority = {};
irc.network.options.PREFIX.forEach((prefix, index) => {

View File

@ -42,6 +42,10 @@ function Network(attr) {
);
}
Network.prototype.destroy = function() {
this.channels.forEach((channel) => channel.destroy());
};
Network.prototype.setNick = function(nick) {
this.nick = nick;
this.highlightRegex = new RegExp(

View File

@ -17,6 +17,7 @@ exports.input = function(network, chan, cmd, args) {
}
network.channels = _.without(network.channels, chan);
chan.destroy();
this.emit("part", {
chan: chan.id
});

View File

@ -11,6 +11,7 @@ exports.input = function(network, chan, cmd, args) {
var quitMessage = args[0] ? args.join(" ") : "";
client.networks = _.without(client.networks, network);
network.destroy();
client.save();
client.emit("quit", {
network: network.id

View File

@ -13,6 +13,7 @@ module.exports = function(irc, network) {
var from = data.nick;
if (from === irc.user.nick) {
network.channels = _.without(network.channels, chan);
chan.destroy();
client.save();
client.emit("part", {
chan: chan.id

View File

@ -17,12 +17,6 @@ class Storage {
}
dereference(url) {
// If maxHistory is 0, image would be dereferenced before client had a chance to retrieve it,
// so for now, just don't implement dereferencing for this edge case.
if (helper.maxHistory === 0) {
return;
}
const references = (this.references.get(url) || 0) - 1;
if (references < 0) {