Skip to content

Commit 8af0d01

Browse files
committed
Google Calendar - New Created or Updated Event (Instant) - reduce API calls when many calendars subscribed
* When a notification is received, updates are fetched only for the calendar that triggered the notification This reduces calls to the Google Calendar API, preventing unnecessary rate limiting
1 parent a692a06 commit 8af0d01

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

components/google_calendar/sources/new-or-updated-event-instant/new-or-updated-event-instant.mjs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -199,16 +199,19 @@ export default {
199199
}
200200
return new Date(min);
201201
},
202-
getChannelIds() {
203-
const channelIds = [];
202+
getCalendarIdForChannelId(incomingChannelId) {
204203
for (const calendarId of this.calendarIds) {
205204
const channelId = this.db.get(`${calendarId}.channelId`);
206-
channelIds.push(channelId);
205+
if (this.db.get(`${calendarId}.channelId`) === incomingChannelId) {
206+
return calendarId;
207+
}
207208
}
208-
return channelIds;
209+
return null;
209210
},
210211
},
211212
async run(event) {
213+
let calendarId = null; // calendar ID matching incoming channel ID
214+
212215
// refresh watch
213216
if (event.interval_seconds) {
214217
// get time
@@ -224,9 +227,9 @@ export default {
224227
}
225228
} else {
226229
// Verify channel ID
227-
const channelIds = this.getChannelIds();
228230
const incomingChannelId = event?.headers?.["x-goog-channel-id"];
229-
if (!channelIds.includes(incomingChannelId)) {
231+
calendarId = this.getCalendarIdForChannelId(incomingChannelId);
232+
if (!calendarId) {
230233
console.log(
231234
`Unexpected channel ID ${incomingChannelId}. This likely means there are multiple, older subscriptions active.`,
232235
);
@@ -252,7 +255,8 @@ export default {
252255
}
253256

254257
// Fetch and emit events
255-
for (const calendarId of this.calendarIds) {
258+
const checkCalendarIds = calendarId ? [ calendarId ] : this.calendarIds;
259+
for (const calendarId of checkCalendarIds) {
256260
const syncToken = this.getNextSyncToken(calendarId);
257261
let nextSyncToken = null;
258262
let nextPageToken = null;

0 commit comments

Comments
 (0)