Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TimelineWindow.load: make the livetimeline case quicker #88

Merged
merged 1 commit into from
Feb 26, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 22 additions & 20 deletions lib/timeline-window.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,39 +88,41 @@ TimelineWindow.prototype.load = function(initialEventId, initialWindowSize) {
var self = this;
initialWindowSize = initialWindowSize || 20;

var prom;
// given an EventTimeline, and an event index within it, initialise our
// fields so that the event in question is in the middle of the window.
var initFields = function(timeline, eventIndex) {
var endIndex = Math.min(timeline.getEvents().length,
eventIndex + Math.ceil(initialWindowSize / 2));
var startIndex = Math.max(0, endIndex - initialWindowSize);
self._start = new TimelineIndex(timeline, startIndex - timeline.getBaseIndex());
self._end = new TimelineIndex(timeline, endIndex - timeline.getBaseIndex());
self._eventCount = endIndex - startIndex;
};

// We avoid delaying the resolution of the promise by a reactor tick if
// we already have the data we need, which is important to keep room-switching
// feeling snappy.
//
// TODO: ideally we'd spot getEventTimeline returning a resolved promise and
// skip straight to the find-event loop.
if (initialEventId) {
debuglog("TimelineWindow: initialising for event " + initialEventId);
prom = this._client.getEventTimeline(this._room, initialEventId)
return this._client.getEventTimeline(this._room, initialEventId)
.then(function(tl) {
// make sure that our window includes the event
for (var i = 0; i < tl.getEvents().length; i++) {
if (tl.getEvents()[i].getId() == initialEventId) {
return {timeline: tl, index: i};
initFields(tl, i);
return;
}
}
throw new Error("getEventTimeline result didn't include requested event");
});
} else {
debuglog("TimelineWindow: initialising with live timeline");

// start with the most recent events
var tl = this._room.getLiveTimeline();
prom = q({timeline: tl, index: tl.getEvents().length});
initFields(tl, tl.getEvents().length);
return q();
}

prom = prom.then(function(v) {
var tl = v.timeline, eventIndex = v.index;

var endIndex = Math.min(tl.getEvents().length,
eventIndex + Math.ceil(initialWindowSize / 2));
var startIndex = Math.max(0, endIndex - initialWindowSize);
self._start = new TimelineIndex(tl, startIndex - tl.getBaseIndex());
self._end = new TimelineIndex(tl, endIndex - tl.getBaseIndex());
self._eventCount = endIndex - startIndex;
});

return prom;
};

/**
Expand Down