calendar: Do less work in hasEvents()

getEvents() filters all events for the given range and sorts the result.

That's more than we need when checking whether there are any events,
where we only care that there's at least one event in the range.

Address this by splitting out the event filtering into a generator
function, so hasEvents() can return after at most one iteration.

https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/1192
This commit is contained in:
Florian Müllner 2020-04-02 23:46:48 +02:00 committed by Florian Müllner
parent 8d79f6f4c8
commit d94d0f60c8

View File

@ -323,14 +323,16 @@ class DBusEventSource extends EventSourceBase {
}
}
getEvents(begin, end) {
let result = [];
for (let n = 0; n < this._events.length; n++) {
let event = this._events[n];
*_getFilteredEvents(begin, end) {
for (const event of this._events) {
if (_dateIntervalsOverlap(event.date, event.end, begin, end))
result.push(event);
yield event;
}
}
getEvents(begin, end) {
let result = [...this._getFilteredEvents(begin, end)];
result.sort((event1, event2) => {
// sort events by end time on ending day
let d1 = event1.date < begin && event1.end <= end ? event1.end : event1.date;
@ -344,12 +346,8 @@ class DBusEventSource extends EventSourceBase {
let dayBegin = _getBeginningOfDay(day);
let dayEnd = _getEndOfDay(day);
let events = this.getEvents(dayBegin, dayEnd);
if (events.length == 0)
return false;
return true;
const { done } = this._getFilteredEvents(dayBegin, dayEnd).next();
return !done;
}
});