aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/frontend/src/xtext/UpdateService.ts
blob: 3b4ae25974e575d7aa87397b0c7573dbc914e253 (plain) (blame)
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
import {
  type ChangeDesc,
  ChangeSet,
  type ChangeSpec,
  StateEffect,
  type Transaction,
} from '@codemirror/state';
import { E_CANCELED, E_TIMEOUT, Mutex, withTimeout } from 'async-mutex';
import { debounce } from 'lodash-es';
import { nanoid } from 'nanoid';

import type EditorStore from '../editor/EditorStore';
import getLogger from '../utils/getLogger';

import type XtextWebSocketClient from './XtextWebSocketClient';
import {
  type ContentAssistEntry,
  ContentAssistResult,
  DocumentStateResult,
  FormattingResult,
  isConflictResult,
  OccurrencesResult,
} from './xtextServiceResults';

const UPDATE_TIMEOUT_MS = 500;

const WAIT_FOR_UPDATE_TIMEOUT_MS = 1000;

const FORMAT_TEXT_RETRIES = 5;

const log = getLogger('xtext.UpdateService');

/**
 * State effect used to override the dirty changes after a transaction.
 *
 * If this state effect is _not_ present in a transaction,
 * the transaction will be appended to the current dirty changes.
 *
 * If this state effect is present, the current dirty changes will be replaced
 * by the value of this effect.
 */
const setDirtyChanges = StateEffect.define<ChangeSet>();

export interface AbortSignal {
  aborted: boolean;
}

export interface ContentAssistParams {
  caretOffset: number;

  proposalsLimit: number;
}

export type CancellableResult<T> =
  | { cancelled: false; data: T }
  | { cancelled: true };

interface StateUpdateResult<T> {
  newStateId: string;

  data: T;
}

interface Delta {
  deltaOffset: number;

  deltaReplaceLength: number;

  deltaText: string;
}

export default class UpdateService {
  readonly resourceName: string;

  xtextStateId: string | undefined;

  private readonly store: EditorStore;

  private readonly mutex = withTimeout(new Mutex(), WAIT_FOR_UPDATE_TIMEOUT_MS);

  /**
   * The changes being synchronized to the server if a full or delta text update is running
   * withing a `withUpdateExclusive` block, `undefined` otherwise.
   *
   * Must be `undefined` before and after entering the critical section of `mutex`
   * and may only be changes in the critical section of `mutex`.
   *
   * Methods named with an `Exclusive` suffix in this class assume that the mutex is held
   * and may call `withUpdateExclusive` or `doFallbackUpdateFullTextExclusive`
   * to mutate this field.
   *
   * Methods named with a `do` suffix assume that they are called in a `withUpdateExclusive`
   * block and require this field to be non-`undefined`.
   */
  private pendingUpdate: ChangeSet | undefined;

  /**
   * Local changes not yet sychronized to the server and not part of the running update, if any.
   */
  private dirtyChanges: ChangeSet;

  private readonly webSocketClient: XtextWebSocketClient;

  private readonly idleUpdateLater = debounce(
    () => this.idleUpdate(),
    UPDATE_TIMEOUT_MS,
  );

  constructor(store: EditorStore, webSocketClient: XtextWebSocketClient) {
    this.resourceName = `${nanoid(7)}.problem`;
    this.store = store;
    this.dirtyChanges = this.newEmptyChangeSet();
    this.webSocketClient = webSocketClient;
  }

  onReconnect(): void {
    this.xtextStateId = undefined;
    this.updateFullText().catch((error) => {
      // Let E_TIMEOUT errors propagate, since if the first update times out,
      // we can't use the connection.
      if (error === E_CANCELED) {
        // Content assist will perform a full-text update anyways.
        log.debug('Full text update cancelled');
        return;
      }
      log.error('Unexpected error during initial update', error);
    });
  }

  onTransaction(transaction: Transaction): void {
    const setDirtyChangesEffect = transaction.effects.find((effect) =>
      effect.is(setDirtyChanges),
    ) as StateEffect<ChangeSet> | undefined;
    if (setDirtyChangesEffect) {
      const { value } = setDirtyChangesEffect;
      if (this.pendingUpdate !== undefined) {
        // Do not clear `pendingUpdate`, because that would indicate an update failure
        // to `withUpdateExclusive`.
        this.pendingUpdate = ChangeSet.empty(value.length);
      }
      this.dirtyChanges = value;
      return;
    }
    if (transaction.docChanged) {
      this.dirtyChanges = this.dirtyChanges.compose(transaction.changes);
      this.idleUpdateLater();
    }
  }

  /**
   * Computes the summary of any changes happened since the last complete update.
   *
   * The result reflects any changes that happened since the `xtextStateId`
   * version was uploaded to the server.
   *
   * @returns the summary of changes since the last update
   */
  computeChangesSinceLastUpdate(): ChangeDesc {
    return (
      this.pendingUpdate?.composeDesc(this.dirtyChanges.desc) ??
      this.dirtyChanges.desc
    );
  }

  private idleUpdate(): void {
    if (!this.webSocketClient.isOpen || this.dirtyChanges.empty) {
      return;
    }
    if (!this.mutex.isLocked()) {
      this.update().catch((error) => {
        if (error === E_CANCELED || error === E_TIMEOUT) {
          log.debug('Idle update cancelled');
          return;
        }
        log.error('Unexpected error during scheduled update', error);
      });
    }
    this.idleUpdateLater();
  }

  private newEmptyChangeSet(): ChangeSet {
    return ChangeSet.of([], this.store.state.doc.length);
  }

  private updateFullText(): Promise<void> {
    return this.runExclusive(() => this.updateFullTextExclusive());
  }

  private async updateFullTextExclusive(): Promise<void> {
    await this.withVoidUpdateExclusive(() => this.doUpdateFullTextExclusive());
  }

  private async doUpdateFullTextExclusive(): Promise<string> {
    const result = await this.webSocketClient.send({
      resource: this.resourceName,
      serviceType: 'update',
      fullText: this.store.state.doc.sliceString(0),
    });
    const { stateId } = DocumentStateResult.parse(result);
    return stateId;
  }

  /**
   * Makes sure that the document state on the server reflects recent
   * local changes.
   *
   * Performs either an update with delta text or a full text update if needed.
   * If there are not local dirty changes, the promise resolves immediately.
   *
   * @returns a promise resolving when the update is completed
   */
  private async update(): Promise<void> {
    // We may check here for the delta to avoid locking,
    // but we'll need to recompute the delta in the critical section,
    // because it may have changed by the time we can acquire the lock.
    if (this.dirtyChanges.empty) {
      return;
    }
    await this.runExclusive(() => this.updateExclusive());
  }

  private async updateExclusive(): Promise<void> {
    if (this.xtextStateId === undefined) {
      await this.updateFullTextExclusive();
    }
    const delta = this.computeDelta();
    if (delta === undefined) {
      return;
    }
    log.trace('Editor delta', delta);
    await this.withVoidUpdateExclusive(async () => {
      const result = await this.webSocketClient.send({
        resource: this.resourceName,
        serviceType: 'update',
        requiredStateId: this.xtextStateId,
        ...delta,
      });
      const parsedDocumentStateResult = DocumentStateResult.safeParse(result);
      if (parsedDocumentStateResult.success) {
        return parsedDocumentStateResult.data.stateId;
      }
      if (isConflictResult(result, 'invalidStateId')) {
        return this.doFallbackUpdateFullTextExclusive();
      }
      throw parsedDocumentStateResult.error;
    });
  }

  async fetchOccurrences(
    getCaretOffset: () => CancellableResult<number>,
  ): Promise<CancellableResult<OccurrencesResult>> {
    try {
      await this.update();
    } catch (error) {
      if (error === E_CANCELED || error === E_TIMEOUT) {
        return { cancelled: true };
      }
      throw error;
    }
    if (!this.dirtyChanges.empty || this.mutex.isLocked()) {
      // Just give up if another update is in progress.
      return { cancelled: true };
    }
    const caretOffsetResult = getCaretOffset();
    if (caretOffsetResult.cancelled) {
      return { cancelled: true };
    }
    const expectedStateId = this.xtextStateId;
    const data = await this.webSocketClient.send({
      resource: this.resourceName,
      serviceType: 'occurrences',
      caretOffset: caretOffsetResult.data,
      expectedStateId,
    });
    if (
      // The query must have reached the server without being conflicted with an update
      // or cancelled server-side.
      isConflictResult(data) ||
      // And no state update should have occurred since then.
      this.xtextStateId !== expectedStateId ||
      // And there should be no change to the editor text since then.
      !this.dirtyChanges.empty ||
      // And there should be no state update in progress.
      this.mutex.isLocked()
    ) {
      return { cancelled: true };
    }
    const parsedOccurrencesResult = OccurrencesResult.safeParse(data);
    if (!parsedOccurrencesResult.success) {
      log.error(
        'Unexpected occurences result',
        data,
        'not an OccurrencesResult:',
        parsedOccurrencesResult.error,
      );
      throw parsedOccurrencesResult.error;
    }
    if (parsedOccurrencesResult.data.stateId !== expectedStateId) {
      return { cancelled: true };
    }
    return { cancelled: false, data: parsedOccurrencesResult.data };
  }

  async fetchContentAssist(
    params: ContentAssistParams,
    signal: AbortSignal,
  ): Promise<ContentAssistEntry[]> {
    if (!this.mutex.isLocked && this.xtextStateId !== undefined) {
      return this.fetchContentAssistFetchOnly(params, this.xtextStateId);
    }
    // Content assist updates should have priority over other updates.
    this.mutex.cancel();
    try {
      return await this.runExclusive(() =>
        this.fetchContentAssistExclusive(params, signal),
      );
    } catch (error) {
      if ((error === E_CANCELED || error === E_TIMEOUT) && signal.aborted) {
        return [];
      }
      throw error;
    }
  }

  private async fetchContentAssistExclusive(
    params: ContentAssistParams,
    signal: AbortSignal,
  ): Promise<ContentAssistEntry[]> {
    if (this.xtextStateId === undefined) {
      await this.updateFullTextExclusive();
    }
    if (signal.aborted) {
      return [];
    }
    const delta = this.computeDelta();
    if (delta !== undefined) {
      log.trace('Editor delta', delta);
      // Try to fetch while also performing a delta update.
      const fetchUpdateEntries = await this.withUpdateExclusive(() =>
        this.doFetchContentAssistWithDeltaExclusive(params, delta),
      );
      if (fetchUpdateEntries !== undefined) {
        return fetchUpdateEntries;
      }
      if (signal.aborted) {
        return [];
      }
    }
    if (this.xtextStateId === undefined) {
      throw new Error('failed to obtain Xtext state id');
    }
    return this.fetchContentAssistFetchOnly(params, this.xtextStateId);
  }

  private async doFetchContentAssistWithDeltaExclusive(
    params: ContentAssistParams,
    delta: Delta,
  ): Promise<StateUpdateResult<ContentAssistEntry[] | undefined>> {
    const fetchUpdateResult = await this.webSocketClient.send({
      ...params,
      resource: this.resourceName,
      serviceType: 'assist',
      requiredStateId: this.xtextStateId,
      ...delta,
    });
    const parsedContentAssistResult =
      ContentAssistResult.safeParse(fetchUpdateResult);
    if (parsedContentAssistResult.success) {
      const { stateId, entries: resultEntries } =
        parsedContentAssistResult.data;
      return { newStateId: stateId, data: resultEntries };
    }
    if (isConflictResult(fetchUpdateResult, 'invalidStateId')) {
      log.warn('Server state invalid during content assist');
      const newStateId = await this.doFallbackUpdateFullTextExclusive();
      // We must finish this state update transaction to prepare for any push events
      // before querying for content assist, so we just return `undefined` and will query
      // the content assist service later.
      return { newStateId, data: undefined };
    }
    throw parsedContentAssistResult.error;
  }

  private async fetchContentAssistFetchOnly(
    params: ContentAssistParams,
    requiredStateId: string,
  ): Promise<ContentAssistEntry[]> {
    // Fallback to fetching without a delta update.
    const fetchOnlyResult = await this.webSocketClient.send({
      ...params,
      resource: this.resourceName,
      serviceType: 'assist',
      requiredStateId,
    });
    const { stateId, entries: fetchOnlyEntries } =
      ContentAssistResult.parse(fetchOnlyResult);
    if (stateId !== requiredStateId) {
      throw new Error(
        `Unexpected state id, expected: ${requiredStateId} got: ${stateId}`,
      );
    }
    return fetchOnlyEntries;
  }

  async formatText(): Promise<void> {
    let retries = 0;
    while (retries < FORMAT_TEXT_RETRIES) {
      try {
        // eslint-disable-next-line no-await-in-loop -- Use a loop for sequential retries.
        await this.runExclusive(() => this.formatTextExclusive());
        return;
      } catch (error) {
        // Let timeout errors propagate to give up formatting on a flaky connection.
        if (error === E_CANCELED && retries < FORMAT_TEXT_RETRIES) {
          retries += 1;
        } else {
          throw error;
        }
      }
    }
  }

  private async formatTextExclusive(): Promise<void> {
    await this.updateExclusive();
    let { from, to } = this.store.state.selection.main;
    if (to <= from) {
      from = 0;
      to = this.store.state.doc.length;
    }
    log.debug('Formatting from', from, 'to', to);
    await this.withVoidUpdateExclusive(async () => {
      const result = await this.webSocketClient.send({
        resource: this.resourceName,
        serviceType: 'format',
        selectionStart: from,
        selectionEnd: to,
      });
      const { stateId, formattedText } = FormattingResult.parse(result);
      this.applyBeforeDirtyChanges({
        from,
        to,
        insert: formattedText,
      });
      return stateId;
    });
  }

  private computeDelta(): Delta | undefined {
    if (this.dirtyChanges.empty) {
      return undefined;
    }
    let minFromA = Number.MAX_SAFE_INTEGER;
    let maxToA = 0;
    let minFromB = Number.MAX_SAFE_INTEGER;
    let maxToB = 0;
    this.dirtyChanges.iterChangedRanges((fromA, toA, fromB, toB) => {
      minFromA = Math.min(minFromA, fromA);
      maxToA = Math.max(maxToA, toA);
      minFromB = Math.min(minFromB, fromB);
      maxToB = Math.max(maxToB, toB);
    });
    return {
      deltaOffset: minFromA,
      deltaReplaceLength: maxToA - minFromA,
      deltaText: this.store.state.doc.sliceString(minFromB, maxToB),
    };
  }

  private applyBeforeDirtyChanges(changeSpec: ChangeSpec): void {
    const pendingChanges =
      this.pendingUpdate?.compose(this.dirtyChanges) ?? this.dirtyChanges;
    const revertChanges = pendingChanges.invert(this.store.state.doc);
    const applyBefore = ChangeSet.of(changeSpec, revertChanges.newLength);
    const redoChanges = pendingChanges.map(applyBefore.desc);
    const changeSet = revertChanges.compose(applyBefore).compose(redoChanges);
    this.store.dispatch({
      changes: changeSet,
      // Keep the current set of dirty changes (but update them according the re-formatting)
      // and to not add the formatting the dirty changes.
      effects: [setDirtyChanges.of(redoChanges)],
    });
  }

  private runExclusive<T>(callback: () => Promise<T>): Promise<T> {
    return this.mutex.runExclusive(async () => {
      if (this.pendingUpdate !== undefined) {
        throw new Error('Update is pending before entering critical section');
      }
      const result = await callback();
      if (this.pendingUpdate !== undefined) {
        throw new Error('Update is pending after entering critical section');
      }
      return result;
    });
  }

  private withVoidUpdateExclusive(
    callback: () => Promise<string>,
  ): Promise<void> {
    return this.withUpdateExclusive<void>(async () => {
      const newStateId = await callback();
      return { newStateId, data: undefined };
    });
  }

  /**
   * Executes an asynchronous callback that updates the state on the server.
   *
   * Ensures that updates happen sequentially and manages `pendingUpdate`
   * and `dirtyChanges` to reflect changes being synchronized to the server
   * and not yet synchronized to the server, respectively.
   *
   * Optionally, `callback` may return a second value that is retured by this function.
   *
   * Once the remote procedure call to update the server state finishes
   * and returns the new `stateId`, `callback` must return _immediately_
   * to ensure that the local `stateId` is updated likewise to be able to handle
   * push messages referring to the new `stateId` from the server.
   * If additional work is needed to compute the second value in some cases,
   * use `T | undefined` instead of `T` as a return type and signal the need for additional
   * computations by returning `undefined`. Thus additional computations can be performed
   * outside of the critical section.
   *
   * @param callback the asynchronous callback that updates the server state
   * @returns a promise resolving to the second value returned by `callback`
   */
  private async withUpdateExclusive<T>(
    callback: () => Promise<StateUpdateResult<T>>,
  ): Promise<T> {
    if (this.pendingUpdate !== undefined) {
      throw new Error('Delta updates are not reentrant');
    }
    this.pendingUpdate = this.dirtyChanges;
    this.dirtyChanges = this.newEmptyChangeSet();
    let data: T;
    try {
      ({ newStateId: this.xtextStateId, data } = await callback());
      this.pendingUpdate = undefined;
    } catch (e) {
      log.error('Error while update', e);
      if (this.pendingUpdate === undefined) {
        log.error('pendingUpdate was cleared during update');
      } else {
        this.dirtyChanges = this.pendingUpdate.compose(this.dirtyChanges);
      }
      this.pendingUpdate = undefined;
      this.webSocketClient.forceReconnectOnError();
      throw e;
    }
    return data;
  }

  private doFallbackUpdateFullTextExclusive(): Promise<string> {
    if (this.pendingUpdate === undefined) {
      throw new Error('Only a pending update can be extended');
    }
    log.warn('Delta update failed, performing full text update');
    this.xtextStateId = undefined;
    this.pendingUpdate = this.pendingUpdate.compose(this.dirtyChanges);
    this.dirtyChanges = this.newEmptyChangeSet();
    return this.doUpdateFullTextExclusive();
  }
}