I am writing an electron app that first searches for a given Google calendar event, and, if one isn't found, it then submits a new calendar event. If I forego the initial search, the submission appears to work fine. In this case, the code looks like:
// app.js
for (var key in this.mGames) {
if (this.mGames.hasOwnProperty(key)) {
var game = this.mGames[key];
self.mGoogleClient.submitGameToCalendar(aCalendarId, game)
.then(() => {
console.log("Game added to calendar!");
}
});
}
}
// GoogleClient.js
submitGameToCalendar: function(aCalendarId, aGame) {
var that = this;
return new Promise((resolve, reject) => {
that.getToken().then(() => {
var eventToInsert = aGame.getEventJSON();
var cal = google.calendar({
version: 'v3',
auth: that.client
});
cal.events.insert({
'calendarId' : aCalendarId,
'resource' : eventToInsert
}, {}, function (err, result) {
if (!err) {
resolve();
} else {
reject(err);
}
});
});
});
},
As expected, in this case, I see the "Game added to calendar!" message in the console, and the event appears on my calendar.
However, if I now run this same code within the promise callback for a function called 'findGamesInCalendar', as in the following code, it hangs after attempting to submit the game to the calendar.
// app.js
for (var key in this.mGames) {
if (this.mGames.hasOwnProperty(key)) {
var game = this.mGames[key];
this.mGoogleClient.findGameInCalendar(aCalendarId, game)
.then((foundEventId) => {
if (!foundEventId) {
self.mGoogleClient.submitGameToCalendar(aCalendarId, game)
.then(() => {
console.log("Game added to calendar!");
}
});
} else {
console.log("Game found with event id: " + foundEventId);
}
});
}
}
// GoogleClient.js
// same as above
submitGameToCalendar: function(aCalendarId, aGame) {
var that = this;
return new Promise((resolve, reject) => {
that.getToken().then(() => {
var eventToInsert = aGame.getEventJSON();
var cal = google.calendar({
version: 'v3',
auth: that.client
});
cal.events.insert({
'calendarId' : aCalendarId,
'resource' : eventToInsert
}, {}, function (err, result) {
if (!err) {
resolve();
} else {
reject(err);
}
});
});
});
},
findGameInCalendar: function(aCalendarId, aGame) {
return new Promise((resolve, reject) => {
var that = this;
that.getToken().then(() => {
var cal = google.calendar({
version: 'v3',
auth: that.client
});
var searchString = "{ArbitratorHash: " + aGame.getHash() + "}";
cal.events.list({
'calendarId' : aCalendarId
}, function (err, result) {
if (err) {
reject(err);
}
var results = result.items;
var foundEvent = false;
for (var i = 0; i < results.length; i++) {
var calEvent = results[i];
if (calEvent.description
&& calEvent.description.indexOf(searchString) > 0) {
resolve(calEvent);
return;
}
}
if (!foundEvent) {
resolve();
}
});
});
});
},
I've traced the problem to the submitGameToCalendar
function using console.log
calls, and it appears that the cal.events.insert()
function is being called, but it never executes the callback.
I'm not quite sure how to trace this problem back to the actual cause. It seems to be something getting hung up in multiple Promise objects, or perhaps a Google api client not being terminated correctly after executing it's functionality.