Search code examples
google-drive-realtime-api

Is there any way to start a compound operation in response to an event in another compound operation?


In the code below, an item is added to a collaborative list within a compound operation. In a VALUES_ADDED event handler on the list, another compound operation is started. When I run the code, I get the following error message: Exception: Uncaught Error: gapi.drive.realtime.Error: {type: invalid_compound_operation, message: "Already committed this local change.", isFatal: true}

It seems like this would not be an uncommon occurrence and arose naturally in my application, although in a more complicated way, so I can't just move the map assignment into the original compound operation. I also couldn't find any reference to such a restriction in the realtime api guide or reference documentation. Is there any way to begin a new compound operation in a call stack above a compound operation ending?

var va = function(event) {
  doc.getModel().beginCompoundOperation('b');
  doc.getModel().getRoot().get('map').set('key', 'value');
  doc.getModel().endCompoundOperation();
};
doc.getModel().getRoot().get('list').addEventListener(gapi.drive.realtime.EventType.VALUES_ADDED, va);
doc.getModel().beginCompoundOperation('a');
doc.getModel().getRoot().get('list').push(100);
doc.getModel().endCompoundOperation();

Solution

  • In general you should never write to the Realtime model in an event listener. Doing so can result in unnecessary writes. For example, in your code, every single collaborator on the document will write whenever the list is modified, which is probably not what you wanted. It's also easy to inadvertently create write cycles among multiple clients.

    We don't have any plans to remove this restriction, although I will ask our documentation team to see if they can add a better explanation.

    Generally our customers have not had issues with moving the writes into the original compound op - could you provide me with more detail on why this is impractical for your application?

    Thanks, Brian