Skip to content

Commit

Permalink
Finish refactoring to OO style.
Browse files Browse the repository at this point in the history
  • Loading branch information
jorendorff committed Apr 9, 2016
1 parent f44c2d6 commit 0a381e3
Showing 1 changed file with 22 additions and 19 deletions.
41 changes: 22 additions & 19 deletions lib/rga.js
Original file line number Diff line number Diff line change
Expand Up @@ -443,29 +443,15 @@ RGA.AceEditorRGA = function AceEditorRga(id, editor, history, queue) {
editor.setValue(this._lastText, -1);

// The flow of operations is (unavoidably) bidirectional. First, when Ace
// notifies us of an edit, use _takeUserEdits to fold those changes into the
// RGA.
// notifies us of an edit, fold those changes into the RGA.
var self = this;
this._changeCallback = function () { self._takeUserEdits() };
editor.getSession().on("change", this._changeCallback);

// Now for the other direction. Here we replace the callback that
// receives changes from other RGAs.
this.downstream = function rgaToEditor(source, op) {
// Always check for new user edits *before* accepting ops from the internet.
// That way, _takeUserEdits() knows that all differences between
// `_lastText` and `editor.getValue()` are the result of new user input.
self._takeUserEdits();

// 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
// already been inserted/deleted, we have to call that first,
// before modifying the RGA.
self._applyOpToEditor(op); // first update the editor
self._downstream.call(self, source, op); // then update the RGA
self._lastText = self.editor.getValue();

self._assertInSync({op: op});
// Now for the other direction. Replace the callback that receives changes
// from other RGAs with a new one that also updates the editor.
this.downstream = function (source, op) {
self._customDownstream(source, op);
};
this.downstream._id = id;
};
Expand Down Expand Up @@ -551,6 +537,23 @@ Object.assign(RGA.AceEditorRGA.prototype, {
});
break;
}
},

_customDownstream: function (source, op) {
// Always check for new user edits *before* accepting ops from the internet.
// That way, _takeUserEdits() knows that all differences between
// `_lastText` and `editor.getValue()` are the result of new user input.
this._takeUserEdits();

// 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
// already been inserted/deleted, we have to call that first,
// before modifying the RGA.
this._applyOpToEditor(op); // first update the editor
this._downstream(source, op); // then update the RGA
this._lastText = this.editor.getValue();

this._assertInSync({op: op});
}
});

Expand Down

0 comments on commit 0a381e3

Please sign in to comment.