Skip to content

Commit

Permalink
Add and remove underscores so that all the public methods of RGA are …
Browse files Browse the repository at this point in the history
…now safe to call on an AceEditorRGA.
  • Loading branch information
jorendorff committed Apr 9, 2016
1 parent fc8ae14 commit 911ee55
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 32 deletions.
20 changes: 6 additions & 14 deletions lib/rga.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ RGA.prototype = {
throw new Error("insertion point is removed from the array");

var node = {timestamp: this._timestamp(), atom: a};
this._downstream(this.downstream, {type: "addRight", t: t, w: node});
this.downstream(this.downstream, {type: "addRight", t: t, w: node});
return node.timestamp;
},

Expand Down Expand Up @@ -141,7 +141,7 @@ RGA.prototype = {
remove: function (t) {
if (!this._lookup(t))
throw new Error("can't remove node that doesn't exist");
this._downstream(this.downstream, {type: "remove", t: t});
this.downstream(this.downstream, {type: "remove", t: t});
},

_downstream_remove: function (op) {
Expand Down Expand Up @@ -234,7 +234,7 @@ RGA.prototype = {
var u = this.getNodeAt(line, column).timestamp;
for (var i = 0; i < text.length; i++) {
var node = {atom: text[i], timestamp: this._timestamp()};
this._downstream(source, {type: "addRight", t: u, w: node});
this.downstream(source, {type: "addRight", t: u, w: node});
u = node.timestamp;
}
},
Expand All @@ -247,14 +247,14 @@ RGA.prototype = {
node = node.next;
if (!node)
throw new Error("tried to remove more characters than exist in document");
this._downstream(source, {type: "remove", t: node.timestamp});
this.downstream(source, {type: "remove", t: node.timestamp});
}
},

// Convenience method to apply a patch. The structure of `delta` is the same
// as a Quill delta, just because it was a JSON patch format I knew about --
// RGA doesn't actually use any Quill code.
applyDelta: function (delta) {
_applyDelta: function (delta) {
var source = this.downstream;
var lastNode = this.left, node = lastNode.next;
var ops = delta.ops;
Expand Down Expand Up @@ -458,14 +458,6 @@ RGA.AceEditorRGA = function AceEditorRGA(id, editor, history, queue) {

RGA.AceEditorRGA.prototype = Object.create(RGA.prototype);
Object.assign(RGA.AceEditorRGA.prototype, {
addRight: function () {
throw new Error("calling addRight on an AceEditorRGA is not supported");
},

remove: function () {
throw new Error("calling remove on an AceEditorRGA is not supported");
},

_assertInSync: function () {
let erText = this.text();
if (this._lastText != erText) {
Expand All @@ -484,7 +476,7 @@ Object.assign(RGA.AceEditorRGA.prototype, {

var changes = RGA.diff(this._lastText, currentText);
//this._log(changes);
this.applyDelta(changes);
this._applyDelta(changes);
var savedLastText = this._lastText;
this._lastText = currentText;

Expand Down
26 changes: 8 additions & 18 deletions test/test_editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,9 @@ describe("RGA.AceEditorRGA", () => {
it("propagates inserts from the RGA to the editor", () => {
let q = new MockEventQueue;
let e = new RGA.AceEditorRGA(0, new MockAceEditor(q));
let a = new RGA(1, undefined, q);
RGA.tie(a, e);
var cursor = a.left.timestamp;
cursor = a.addRight(cursor, "h");
cursor = a.addRight(cursor, "i");
var cursor = e.left.timestamp;
cursor = e.addRight(cursor, "h");
cursor = e.addRight(cursor, "i");
q.drain();
assert.strictEqual(e.editor.getValue(), "hi");
});
Expand All @@ -108,26 +106,18 @@ describe("RGA.AceEditorRGA", () => {

it("propagates deletes from the RGA to the editor", () => {
let q = new MockEventQueue;
let x = new RGA(1, undefined, q);
let y = new RGA.AceEditorRGA(0, new MockAceEditor(q), undefined, q);
RGA.tie(x, y);
let x = new RGA.AceEditorRGA(0, new MockAceEditor(q), undefined, q);
var c = x.addRight(x.left.timestamp, "c");
var b = x.addRight(x.left.timestamp, "b");
var a = x.addRight(x.left.timestamp, "a");
q.drain();
assert.strictEqual(y.editor.getValue(), "abc");
assert.strictEqual(x.editor.getValue(), "abc");

x.remove(b);
q.drain();
assert.strictEqual(y.editor.getValue(), "ac");

assert.strictEqual(x.editor.getValue(), "ac");
x.remove(a);
q.drain();
assert.strictEqual(y.editor.getValue(), "c");

assert.strictEqual(x.editor.getValue(), "c");
x.remove(c);
q.drain();
assert.strictEqual(y.editor.getValue(), "");
assert.strictEqual(x.editor.getValue(), "");
});

it("propagates inserts from the editor to the RGA", () => {
Expand Down

0 comments on commit 911ee55

Please sign in to comment.