Codebeat
Codebeat

Reputation: 6610

Google Drive Javascript API: Detect drive changes - return changes only

I try to detect changes on the users drive to update the contents in a list but when I request for changes I receive all entries on the drive.

To explain why I want to do this: This is a handy feature when an user has two tabs opened, one with the google drive environment and one with an application that uses the drive (doesn't need to reload the app to see content changes made in the drive environment).

I'm following the guide listed here: https://developers.google.com/drive/v2/reference/changes/list

A strange thing is that I need a largestChangeId+1, but how do I know this value? I do not know the largestChangeId and set it to null. No matter what I'm doing with this value I will get always all the content.

I made the following code:

o.getCloudFileUpdates = function(fCallback, sFolderId )
{
  var oDefQ = {q:'trashed=false '+(typeof sFolderId == 'string'?('and "'+sFolderId+'" in parents'):''), largestChangeId:null, orderby:'title', maxResults:1000},      
      fGetFiles = function(request, result) 
      {
        request.execute(function(resp) 
        {
            //$d(resp); 
            if( resp.items instanceof Array )   
            {
              result = result.concat(resp.items);
              if( resp.nextPageToken ) 
              { // Get next page and 'break' this function 
                 return fGetFiles(gapi.client.drive.changes.list($j.extend(oDefQ,{'pageToken':resp.nextPageToken})), result); 
              } 
            }

            if( result.length )
            {
              result.sort(function(a,b) 
              {
                if (a.title < b.title)
                 { return -1; }
                if (a.title > b.title)
                 { return 1; }
                return 0;
              });

              fCallback(result);
              return;
            }
            fCallback(false);
        });
      };

  try {
    fGetFiles(gapi.client.drive.changes.list(oDefQ), []);
  } catch(e) { fCallback(false); return false; }
  return true;
};

How do I get the latest changes whithout knowing the largestChangeId?

Upvotes: 0

Views: 328

Answers (1)

pinoyyid
pinoyyid

Reputation: 22306

Start with zero on your first call. Within the response is the current largest change id, which you need to store and use on the next request. In your code, it will materialise as "resp.largestChangeId".

Upvotes: 1

Related Questions