diff --git a/src/client.js b/src/client.js index f8bd1453..abaa0bd6 100644 --- a/src/client.js +++ b/src/client.js @@ -488,6 +488,8 @@ Client.prototype.quit = function() { if (network.irc) { network.irc.quit("Page closed"); } + + network.destroy(); }); }; diff --git a/src/models/chan.js b/src/models/chan.js index 07a2e229..2fa194a2 100644 --- a/src/models/chan.js +++ b/src/models/chan.js @@ -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) => { diff --git a/src/models/network.js b/src/models/network.js index 6c7bc9a8..71a7dd7d 100644 --- a/src/models/network.js +++ b/src/models/network.js @@ -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( diff --git a/src/plugins/inputs/part.js b/src/plugins/inputs/part.js index 6a837271..1dd11906 100644 --- a/src/plugins/inputs/part.js +++ b/src/plugins/inputs/part.js @@ -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 }); diff --git a/src/plugins/inputs/quit.js b/src/plugins/inputs/quit.js index be41378a..50594037 100644 --- a/src/plugins/inputs/quit.js +++ b/src/plugins/inputs/quit.js @@ -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 diff --git a/src/plugins/irc-events/part.js b/src/plugins/irc-events/part.js index 70376cdf..09053687 100644 --- a/src/plugins/irc-events/part.js +++ b/src/plugins/irc-events/part.js @@ -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 diff --git a/src/plugins/storage.js b/src/plugins/storage.js index 2d889f1b..b96bf7af 100644 --- a/src/plugins/storage.js +++ b/src/plugins/storage.js @@ -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) {