0

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.

jwir3
  • 6,019
  • 5
  • 47
  • 92
  • After turning on `NODE_DEBUG=request`, I've noticed that the first call (whether it be to retrieve a token or to actually make the calendar request) in `submitGameToCalendar` always seems to hang. The request appears to be sent, but it's simply never getting a response. – jwir3 Nov 06 '16 at 20:25
  • Also, it appears that running the query manually (through postman) seems to work, so I'm not quite sure what might be causing the request infrastructure within the library to hang. – jwir3 Nov 06 '16 at 20:33
  • I've also noticed that, if I continually click the button that should perform these actions (with the same input), sometimes it eventually does go through, but almost never on the first time. I'm wondering if I've set up some kind of weird race condition here. – jwir3 Nov 06 '16 at 20:42
  • By the way, I tried to make the example as concise as possible, but the full source of the project is available here: https://github.com/jwir3/arbitrator/tree/feature/%2364-electron-spike if it will give you more context. – jwir3 Nov 06 '16 at 20:46

0 Answers0