Dealing with doc.create races

29 views
Skip to first unread message

Jonathan Reem

unread,
Mar 24, 2016, 9:53:40 PM3/24/16
to ShareJS
Hello!

We are attempting to use ShareJS with the share-codemirror integration to create a collaborative editor for files on a remote system. We are running into some issues, specifically around initializing the document with data from the server. Effectively, we would like a 'create if not exists' atomic operation.

Our current code looks like this (very similar to the share examples):

```
var file_path = '....';
var codemirror = CodeMirror(..);
var contents = get_contents_from_server(file_path);

var doc = sjs.get('files', file_path);
doc.subscribe(function (err) { if (err) console.error(err); });

doc.whenReady(function () {
  if (!doc.type) {
    doc.create('text', contents, function (err) { if (err) console.error(err); });
  }

  doc.attachCodeMirror(codemirror);
});
```

When two clients both try to open a file with the same name (launching a collaborative session), both clients will read that `doc.type` is not set and attempt to call create. On the server, one create will succeed and the other is rejected with "Operation was rejected (Invalid opData version)". This causes the clients to enter into a bad state, and from there lots of bugs occur. Is there a good way to say "create with this content unless it already exists, in which case do nothing"?

We can hack around this by using our own external synchronization to ensure that only one client ends up calling create, but this is very not ideal for our use case.

Thanks! Jonathan Reem
Reply all
Reply to author
Forward
0 new messages