From 2e8ade4da00882614d3da8c7d09b49d90ea1b0fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20M=C3=BCllner?= Date: Thu, 2 Apr 2020 23:46:48 +0200 Subject: [PATCH] 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 --- js/ui/calendar.js | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/js/ui/calendar.js b/js/ui/calendar.js index 8346056c3..89886f7b1 100644 --- a/js/ui/calendar.js +++ b/js/ui/calendar.js @@ -322,14 +322,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; @@ -343,12 +345,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; } });