summaryrefslogtreecommitdiff
path: root/lib/ot/client.js
blob: 7ee19fdc28876f0805102db92060b3925d0d973a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
// translation of https://github.com/djspiewak/cccp/blob/master/agent/src/main/scala/com/codecommit/cccp/agent/state.scala

if (typeof ot === 'undefined') {
  var ot = {};
}

ot.Client = (function (global) {
  'use strict';

  // Client constructor
  function Client (revision) {
    this.revision = revision; // the next expected revision number
    this.setState(synchronized_); // start state
  }

  Client.prototype.setState = function (state) {
    this.state = state;
  };

  // Call this method when the user changes the document.
  Client.prototype.applyClient = function (operation) {
    this.setState(this.state.applyClient(this, operation));
  };

  // Call this method with a new operation from the server
  Client.prototype.applyServer = function (revision, operation) {
    this.setState(this.state.applyServer(this, revision, operation));
  };

  Client.prototype.applyOperations = function (head, operations) {
    this.setState(this.state.applyOperations(this, head, operations));
  };

  Client.prototype.serverAck = function (revision) {
    this.setState(this.state.serverAck(this, revision));
  };

  Client.prototype.serverReconnect = function () {
    if (typeof this.state.resend === 'function') { this.state.resend(this); }
  };

  // Transforms a selection from the latest known server state to the current
  // client state. For example, if we get from the server the information that
  // another user's cursor is at position 3, but the server hasn't yet received
  // our newest operation, an insertion of 5 characters at the beginning of the
  // document, the correct position of the other user's cursor in our current
  // document is 8.
  Client.prototype.transformSelection = function (selection) {
    return this.state.transformSelection(selection);
  };

  // Override this method.
  Client.prototype.sendOperation = function (revision, operation) {
    throw new Error("sendOperation must be defined in child class");
  };

  // Override this method.
  Client.prototype.applyOperation = function (operation) {
    throw new Error("applyOperation must be defined in child class");
  };


  // In the 'Synchronized' state, there is no pending operation that the client
  // has sent to the server.
  function Synchronized () {}
  Client.Synchronized = Synchronized;

  Synchronized.prototype.applyClient = function (client, operation) {
    // When the user makes an edit, send the operation to the server and
    // switch to the 'AwaitingConfirm' state
    client.sendOperation(client.revision, operation);
    return new AwaitingConfirm(operation);
  };

  Synchronized.prototype.applyServer = function (client, revision, operation) {
    if (revision - client.revision > 1) {
      throw new Error("Invalid revision.");
    }
    client.revision = revision;
    // When we receive a new operation from the server, the operation can be
    // simply applied to the current document
    client.applyOperation(operation);
    return this;
  };

  Synchronized.prototype.serverAck = function (client, revision) {
    throw new Error("There is no pending operation.");
  };

  // Nothing to do because the latest server state and client state are the same.
  Synchronized.prototype.transformSelection = function (x) { return x; };

  // Singleton
  var synchronized_ = new Synchronized();


  // In the 'AwaitingConfirm' state, there's one operation the client has sent
  // to the server and is still waiting for an acknowledgement.
  function AwaitingConfirm (outstanding) {
    // Save the pending operation
    this.outstanding = outstanding;
  }
  Client.AwaitingConfirm = AwaitingConfirm;

  AwaitingConfirm.prototype.applyClient = function (client, operation) {
    // When the user makes an edit, don't send the operation immediately,
    // instead switch to 'AwaitingWithBuffer' state
    return new AwaitingWithBuffer(this.outstanding, operation);
  };

  AwaitingConfirm.prototype.applyServer = function (client, revision, operation) {
    if (revision - client.revision > 1) {
      throw new Error("Invalid revision.");
    }
    client.revision = revision;
    // This is another client's operation. Visualization:
    //
    //                   /\
    // this.outstanding /  \ operation
    //                 /    \
    //                 \    /
    //  pair[1]         \  / pair[0] (new outstanding)
    //  (can be applied  \/
    //  to the client's
    //  current document)
    var pair = operation.constructor.transform(this.outstanding, operation);
    client.applyOperation(pair[1]);
    return new AwaitingConfirm(pair[0]);
  };

  AwaitingConfirm.prototype.serverAck = function (client, revision) {
    if (revision - client.revision > 1) {
      return new Stale(this.outstanding, client, revision).getOperations();
    }
    client.revision = revision;
    // The client's operation has been acknowledged
    // => switch to synchronized state
    return synchronized_;
  };

  AwaitingConfirm.prototype.transformSelection = function (selection) {
    return selection.transform(this.outstanding);
  };

  AwaitingConfirm.prototype.resend = function (client) {
    // The confirm didn't come because the client was disconnected.
    // Now that it has reconnected, we resend the outstanding operation.
    client.sendOperation(client.revision, this.outstanding);
  };


  // In the 'AwaitingWithBuffer' state, the client is waiting for an operation
  // to be acknowledged by the server while buffering the edits the user makes
  function AwaitingWithBuffer (outstanding, buffer) {
    // Save the pending operation and the user's edits since then
    this.outstanding = outstanding;
    this.buffer = buffer;
  }
  Client.AwaitingWithBuffer = AwaitingWithBuffer;

  AwaitingWithBuffer.prototype.applyClient = function (client, operation) {
    // Compose the user's changes onto the buffer
    var newBuffer = this.buffer.compose(operation);
    return new AwaitingWithBuffer(this.outstanding, newBuffer);
  };

  AwaitingWithBuffer.prototype.applyServer = function (client, revision, operation) {
    if (revision - client.revision > 1) {
      throw new Error("Invalid revision.");
    }
    client.revision = revision;
    // Operation comes from another client
    //
    //                       /\
    //     this.outstanding /  \ operation
    //                     /    \
    //                    /\    /
    //       this.buffer /  \* / pair1[0] (new outstanding)
    //                  /    \/
    //                  \    /
    //          pair2[1] \  / pair2[0] (new buffer)
    // the transformed    \/
    // operation -- can
    // be applied to the
    // client's current
    // document
    //
    // * pair1[1]
    var transform = operation.constructor.transform;
    var pair1 = transform(this.outstanding, operation);
    var pair2 = transform(this.buffer, pair1[1]);
    client.applyOperation(pair2[1]);
    return new AwaitingWithBuffer(pair1[0], pair2[0]);
  };

  AwaitingWithBuffer.prototype.serverAck = function (client, revision) {
    if (revision - client.revision > 1) {
      return new StaleWithBuffer(this.outstanding, this.buffer, client, revision).getOperations();
    }
    client.revision = revision;
    // The pending operation has been acknowledged
    // => send buffer
    client.sendOperation(client.revision, this.buffer);
    return new AwaitingConfirm(this.buffer);
  };

  AwaitingWithBuffer.prototype.transformSelection = function (selection) {
    return selection.transform(this.outstanding).transform(this.buffer);
  };

  AwaitingWithBuffer.prototype.resend = function (client) {
    // The confirm didn't come because the client was disconnected.
    // Now that it has reconnected, we resend the outstanding operation.
    client.sendOperation(client.revision, this.outstanding);
  };


  function Stale(acknowlaged, client, revision) {
    this.acknowlaged = acknowlaged;
    this.client = client;
    this.revision = revision;
  }
  Client.Stale = Stale;

  Stale.prototype.applyClient = function (client, operation) {
    return new StaleWithBuffer(this.acknowlaged, operation, client, this.revision);
  };

  Stale.prototype.applyServer = function (client, revision, operation) {
    throw new Error("Ignored server-side change.");
  };

  Stale.prototype.applyOperations = function (client, head, operations) {
    var transform = this.acknowlaged.constructor.transform;
    for (var i = 0; i < operations.length; i++) {
      var op = ot.TextOperation.fromJSON(operations[i]);
      var pair = transform(this.acknowlaged, op);
      client.applyOperation(pair[1]);
      this.acknowlaged = pair[0];
    }
    client.revision = this.revision;
    return synchronized_;
  };

  Stale.prototype.serverAck = function (client, revision) {
    throw new Error("There is no pending operation.");
  };

  Stale.prototype.transformSelection = function (selection) {
    return selection;
  };

  Stale.prototype.getOperations = function () {
    this.client.getOperations(this.client.revision, this.revision - 1); // acknowlaged is the one at revision
    return this;
  };


  function StaleWithBuffer(acknowlaged, buffer, client, revision) {
    this.acknowlaged = acknowlaged;
    this.buffer = buffer;
    this.client = client;
    this.revision = revision;
  }
  Client.StaleWithBuffer = StaleWithBuffer;

  StaleWithBuffer.prototype.applyClient = function (client, operation) {
    var buffer = this.buffer.compose(operation);
    return new StaleWithBuffer(this.acknowlaged, buffer, client, this.revision);
  };

  StaleWithBuffer.prototype.applyServer = function (client, revision, operation) {
    throw new Error("Ignored server-side change.");
  };

  StaleWithBuffer.prototype.applyOperations = function (client, head, operations) {
    var transform = this.acknowlaged.constructor.transform;
    for (var i = 0; i < operations.length; i++) {
      var op = ot.TextOperation.fromJSON(operations[i]);
      var pair1 = transform(this.acknowlaged, op);
      var pair2 = transform(this.buffer, pair1[1]);
      client.applyOperation(pair2[1]);
      this.acknowlaged = pair1[0];
      this.buffer = pair2[0];
    }
    client.revision = this.revision;
    client.sendOperation(client.revision, this.buffer);
    return new AwaitingConfirm(this.buffer);
  };

  StaleWithBuffer.prototype.serverAck = function (client, revision) {
    throw new Error("There is no pending operation.");
  };

  StaleWithBuffer.prototype.transformSelection = function (selection) {
    return selection;
  };

  StaleWithBuffer.prototype.getOperations = function () {
    this.client.getOperations(this.client.revision, this.revision - 1); // acknowlaged is the one at revision
    return this;
  };


  return Client;

}(this));

if (typeof module === 'object') {
  module.exports = ot.Client;
}