diff --git a/lib/models/room.js b/lib/models/room.js index 3abf8f580..eb6ff3ae9 100644 --- a/lib/models/room.js +++ b/lib/models/room.js @@ -199,8 +199,8 @@ Room.prototype.addEventsToTimeline = function(events, toStartOfTimeline) { // Done after adding the event because otherwise the app would get a read receipt // pointing to an event that wasn't yet in the timeline - // This is really ugly because JS has no way to express an object literal where the - // name of a key comes from an expression + // This is really ugly because JS has no way to express an object literal + // where the name of a key comes from an expression if (events[i].sender) { var fakeReceipt = {content: {}}; fakeReceipt.content[events[i].getId()] = { @@ -284,14 +284,17 @@ Room.prototype.addEvents = function(events, duplicateStrategy) { * @param {String} event_ids A list of event_ids to remove. */ Room.prototype.removeEvents = function(event_ids) { + // avoids defining a function in the loop, which is a lint error + function eq(a, b) { + return a === b; + } + for (var i = 0; i < event_ids.length; ++i) { // NB. we supply reverse to search from the end, // on the assumption that recents events are much // more likley to be removed than older ones. var removed = utils.removeElement( - this.timeline, function(e) { - return e.getId() == event_ids[i]; - }, true + this.timeline, eq.bind(event_ids[i]), true ); if (removed !== false) { this.emit("Room.timeline", removed, this, undefined, true); diff --git a/lib/utils.js b/lib/utils.js index 141aef734..955903fff 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -152,10 +152,11 @@ module.exports.findElement = function(array, fn, reverse) { */ module.exports.removeElement = function(array, fn, reverse) { var i; + var removed; if (reverse) { for (i = array.length - 1; i >= 0; i--) { if (fn(array[i], i, array)) { - var removed = array[i]; + removed = array[i]; array.splice(i, 1); return removed; } @@ -164,7 +165,7 @@ module.exports.removeElement = function(array, fn, reverse) { else { for (i = 0; i < array.length; i++) { if (fn(array[i], i, array)) { - var removed = array[i]; + removed = array[i]; array.splice(i, 1); return removed; }