summaryrefslogtreecommitdiff
path: root/lib/ot/server.js
diff options
context:
space:
mode:
authorWu Cheng-Han2015-07-11 12:43:08 +0800
committerWu Cheng-Han2015-07-11 12:43:08 +0800
commit556338a9c6964d110c1351a402b425c71c2571fa (patch)
treed5b6d2071e554e65c7bfaa4f2c84ddb034598e01 /lib/ot/server.js
parent4702b83adc35f384e214a2a6e9199d08e4494093 (diff)
Added support of operational transformation
Diffstat (limited to 'lib/ot/server.js')
-rw-r--r--lib/ot/server.js46
1 files changed, 46 insertions, 0 deletions
diff --git a/lib/ot/server.js b/lib/ot/server.js
new file mode 100644
index 00000000..608d434b
--- /dev/null
+++ b/lib/ot/server.js
@@ -0,0 +1,46 @@
+if (typeof ot === 'undefined') {
+ var ot = {};
+}
+
+ot.Server = (function (global) {
+ 'use strict';
+
+ // Constructor. Takes the current document as a string and optionally the array
+ // of all operations.
+ function Server (document, operations) {
+ this.document = document;
+ this.operations = operations || [];
+ }
+
+ // Call this method whenever you receive an operation from a client.
+ Server.prototype.receiveOperation = function (revision, operation) {
+ if (revision < 0 || this.operations.length < revision) {
+ throw new Error("operation revision not in history");
+ }
+ // Find all operations that the client didn't know of when it sent the
+ // operation ...
+ var concurrentOperations = this.operations.slice(revision);
+
+ // ... and transform the operation against all these operations ...
+ var transform = operation.constructor.transform;
+ for (var i = 0; i < concurrentOperations.length; i++) {
+ operation = transform(operation, concurrentOperations[i])[0];
+ }
+
+ // ... and apply that on the document.
+ this.document = operation.apply(this.document);
+ // Store operation in history.
+ this.operations.push(operation);
+
+ // It's the caller's responsibility to send the operation to all connected
+ // clients and an acknowledgement to the creator.
+ return operation;
+ };
+
+ return Server;
+
+}(this));
+
+if (typeof module === 'object') {
+ module.exports = ot.Server;
+} \ No newline at end of file