* validateAsync * Validates anything that has changed in the head graph since the last time it was run. * (head graph contains user's edits) * Returns a Promise fulfilled when the validation has completed and then emits a `validated` event. * This may take time but happen in the backgrou
()
| 520 | * @return {Promise} Promise fulfilled when validation is completed. |
| 521 | */ |
| 522 | validateAsync() { |
| 523 | const context = this.context; |
| 524 | const editor = context.systems.editor; |
| 525 | this._completeDiff = editor.difference().complete(); |
| 526 | |
| 527 | if (editor.canRestoreBackup) return Promise.resolve(); // Wait to see if the user wants to restore their backup |
| 528 | if (this._validationPromise) return this._validationPromise; // Validation already in progress |
| 529 | |
| 530 | const baseGraph = editor.base.graph; |
| 531 | const stableGraph = editor.stable.graph; |
| 532 | const previousGraph = this._head.graph ?? baseGraph; // the previously validated graph |
| 533 | |
| 534 | // User has not edited, or undone back to the base state, reset head cache |
| 535 | if (stableGraph === baseGraph) { |
| 536 | this._head = new ValidationCache('head'); |
| 537 | this._head.graph = stableGraph; |
| 538 | this._resolvedIssueIDs.clear(); |
| 539 | this.emit('validated'); |
| 540 | return Promise.resolve(); |
| 541 | } |
| 542 | |
| 543 | // We are caught up to the stable graph |
| 544 | if (stableGraph === previousGraph) { |
| 545 | this.emit('validated'); |
| 546 | return Promise.resolve(); |
| 547 | } |
| 548 | |
| 549 | // If we get here, stable !== previous, so it's time to validate the stable graph.. |
| 550 | this._head.graph = stableGraph; // take snapshot |
| 551 | const incrementalDiff = new Difference(previousGraph, stableGraph); |
| 552 | let entityIDs = [ ...incrementalDiff.complete().keys() ]; |
| 553 | entityIDs = this._head.withAllRelatedEntities(entityIDs); // expand set |
| 554 | |
| 555 | if (!entityIDs.size) { // nothing to do - committed a no-op edit? |
| 556 | this.emit('validated'); |
| 557 | return Promise.resolve(); |
| 558 | } |
| 559 | |
| 560 | this._validationPromise = this._validateEntitiesAsync(this._head, entityIDs) |
| 561 | .then(() => this._updateResolvedIssues(entityIDs)) |
| 562 | .then(() => this.emit('validated')) |
| 563 | .catch(e => console.error(e)) // eslint-disable-line |
| 564 | .then(() => { |
| 565 | this._validationPromise = null; |
| 566 | |
| 567 | // Check if `stable` has changed while we were validating, and run it again to catch up if needed... |
| 568 | const stableGraph = editor.stable.graph; |
| 569 | const previousGraph = this._head.graph; |
| 570 | if (stableGraph !== previousGraph) { |
| 571 | this.validateAsync(); // recurse |
| 572 | } |
| 573 | }); |
| 574 | |
| 575 | return this._validationPromise; |
| 576 | } |
| 577 | |
| 578 | |
| 579 | /** |
no test coverage detected