Add message_list.is_at_end() helper.

This commit is contained in:
Steve Howell 2018-05-28 22:18:27 +00:00 committed by Tim Abbott
parent bf9012a559
commit 26e1fca7ed
3 changed files with 33 additions and 0 deletions

View File

@ -130,6 +130,16 @@ run_test('basics', () => {
run_test('prev_next', () => { run_test('prev_next', () => {
var list = new MessageList({}); var list = new MessageList({});
assert.equal(list.prev(), undefined);
assert.equal(list.next(), undefined);
assert.equal(list.is_at_end(), false);
// try to confuse things with bogus selected id
list.data.set_selected_id(33);
assert.equal(list.prev(), undefined);
assert.equal(list.next(), undefined);
assert.equal(list.is_at_end(), false);
var messages = [{id: 30}, {id: 40}, {id: 50}, {id: 60}]; var messages = [{id: 30}, {id: 40}, {id: 50}, {id: 60}];
list.append(messages, true); list.append(messages, true);
assert.equal(list.prev(), undefined); assert.equal(list.prev(), undefined);
@ -139,6 +149,7 @@ run_test('prev_next', () => {
list.data.set_selected_id(45); list.data.set_selected_id(45);
assert.equal(list.prev(), undefined); assert.equal(list.prev(), undefined);
assert.equal(list.next(), undefined); assert.equal(list.next(), undefined);
assert.equal(list.is_at_end(), false);
list.data.set_selected_id(30); list.data.set_selected_id(30);
assert.equal(list.prev(), undefined); assert.equal(list.prev(), undefined);
@ -147,10 +158,12 @@ run_test('prev_next', () => {
list.data.set_selected_id(50); list.data.set_selected_id(50);
assert.equal(list.prev(), 40); assert.equal(list.prev(), 40);
assert.equal(list.next(), 60); assert.equal(list.next(), 60);
assert.equal(list.is_at_end(), false);
list.data.set_selected_id(60); list.data.set_selected_id(60);
assert.equal(list.prev(), 50); assert.equal(list.prev(), 50);
assert.equal(list.next(), undefined); assert.equal(list.next(), undefined);
assert.equal(list.is_at_end(), true);
}); });
run_test('message_range', () => { run_test('message_range', () => {

View File

@ -99,6 +99,10 @@ exports.MessageList.prototype = {
return this.data.next(); return this.data.next();
}, },
is_at_end: function () {
return this.data.is_at_end();
},
nth_most_recent_id: function (n) { nth_most_recent_id: function (n) {
return this.data.nth_most_recent_id(n); return this.data.nth_most_recent_id(n);
}, },

View File

@ -78,6 +78,22 @@ MessageListData.prototype = {
return this._items[i + 1].id; return this._items[i + 1].id;
}, },
is_at_end: function () {
if (this._selected_id === -1) {
return false;
}
var n = this._items.length;
if (n === 0) {
return false;
}
var last_msg = this._items[n-1];
return (last_msg.id === this._selected_id);
},
nth_most_recent_id: function (n) { nth_most_recent_id: function (n) {
var i = this._items.length - n; var i = this._items.length - n;
if (i < 0) { if (i < 0) {