Skip to content

Commit

Permalink
Fix up logging in rga.js.
Browse files Browse the repository at this point in the history
  • Loading branch information
jorendorff committed Apr 9, 2016
1 parent cf48620 commit 1d4d125
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions lib/rga.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@ function RGA(id, history, queue) {
this.downstream = function (sender, op) {
self._downstream(sender, op);
};
this.downstream._id = id;

if (history !== undefined) {
for (var i = 0; i < history.length; i++)
this._downstream(this.downstream, history[i]);
}
}

RGA._logging = false;

RGA._browserQueue = {
defer: function (cb) { setTimeout(cb, 0); }
};
Expand All @@ -48,6 +51,7 @@ RGA.prototype = {

// Apply an operation and broadcast it to other replicas.
_downstream: function (sender, op) {
//this._log("replica " + this.id + " received " + JSON.stringify(op) + " from " + sender._id);
var self = this.downstream;
this["_downstream_" + op.type].call(this, op);
var queue = this._queue;
Expand Down Expand Up @@ -256,7 +260,7 @@ RGA.prototype = {
var ops = delta.ops;
for (var i = 0; i < ops.length; i++) {
var op = ops[i];
//console.log("* applying", op);
//this._log("* applying", op);
if ("retain" in op) {
for (var j = 0; j < op.retain;) {
if (!node.removed) {
Expand All @@ -271,7 +275,7 @@ RGA.prototype = {
if (!node.removed) {
lastNode = node;
j++;
//console.log(" - removing character:", node.atom);
//this._log(" - removing character:", node.atom);
this._downstream(source, {type: "remove", t: node.timestamp});
}
node = next;
Expand All @@ -280,7 +284,7 @@ RGA.prototype = {
var t = lastNode.timestamp;
var str = op.insert;
for (var j = 0; j < str.length; j++) {
//console.log(" - inserting character:", str[j]);
//this._log(" - inserting character:", str[j]);
var tnext = this._timestamp();
var next = {atom: str[j], timestamp: tnext};
this._downstream(source, {type: "addRight", t: t, w: next});
Expand All @@ -299,6 +303,11 @@ RGA.prototype = {
offset++;
}
return offset;
},

_log: function () {
if (RGA._logging)
console.log.apply(console, arguments);
}
};

Expand Down Expand Up @@ -435,7 +444,7 @@ RGA.AceEditorRGA = function AceEditorRga(id, editor, history, queue) {
if (lastText != erText) {
infodump.lastText = lastText;
infodump.rgaText = erText;
console.error(rga.id, "lastText and rga are out of sync", infodump);
console.error(self.id, "lastText and rga are out of sync", infodump);
throw new Error("editor and RGA data structure got out of sync");
}
}
Expand All @@ -448,12 +457,12 @@ RGA.AceEditorRGA = function AceEditorRga(id, editor, history, queue) {
// First, propagate user edits from Ace to the RGA.
function takeUserEdits() {
var currentText = editor.getValue();
//console.log("takeUserEdits!! <" + currentText + "> <" + lastText + ">");
//self._log("takeUserEdits!! <" + currentText + "> <" + lastText + ">");
if (currentText != lastText) {
assertInSync({currentEditorState: currentText});

var changes = RGA.diff(lastText, currentText);
//console.log(changes);
//self._log(changes);
self.applyDelta(changes);
var savedLastText = lastText;
lastText = currentText;
Expand Down Expand Up @@ -481,14 +490,14 @@ RGA.AceEditorRGA = function AceEditorRga(id, editor, history, queue) {
}

var loc = self.getRowColumnAfter(op.t, op.w.timestamp);
//console.log("inserting character", op.w.atom, "at", loc);
//self._log("inserting character", op.w.atom, "at", loc);
withEditorCallbacksDisabled(function () {
editorSession.insert(loc, op.w.atom);
});
break;

case "remove":
//console.log("remove:", op.t, " from:", self);
//self._log("remove:", op.t, " from:", self);
if (self._index.get(op.t).removed) {
// This character has already been removed. Nothing to do.
break;
Expand All @@ -502,7 +511,7 @@ RGA.AceEditorRGA = function AceEditorRga(id, editor, history, queue) {
? {row: loc.row + 1, column: 0}
: {row: loc.row, column: loc.column + 1}
};
//console.log("removing from editor:", whatToRemove);
//self._log("removing from editor:", whatToRemove);
withEditorCallbacksDisabled(function () {
editorSession.remove(whatToRemove);
});
Expand All @@ -517,7 +526,7 @@ RGA.AceEditorRGA = function AceEditorRga(id, editor, history, queue) {
// `lastText` and `editor.getValue()` are the result of new user input.
takeUserEdits();

//console.log("editorReplica.downstream: received op (from socket):", op);
//self._log("editorReplica.downstream: received op (from socket):", op);

// Since applyOpToEditor uses the RGA to look up the location of the
// inserted/deleted character in the document, and determine whether it has in fact
Expand All @@ -527,7 +536,8 @@ RGA.AceEditorRGA = function AceEditorRga(id, editor, history, queue) {
_base_downstream.call(this, source, op); // then update the RGA
lastText = self.editor.getValue();
assertInSync({op: op});
}
};
this.downstream._id = id;
};

RGA.AceEditorRGA.prototype = Object.create(RGA.prototype);
Expand Down

0 comments on commit 1d4d125

Please sign in to comment.