|
| 1 | +/** |
| 2 | + * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved. |
| 3 | + * For licensing, see LICENSE.md. |
| 4 | + */ |
| 5 | + |
| 6 | +/** |
| 7 | + * @module engine/dev-utils/deltareplayer |
| 8 | + */ |
| 9 | + |
| 10 | +/* global setTimeout, console */ |
| 11 | + |
| 12 | +import DeltaFactory from '../model/delta/deltafactory'; |
| 13 | + |
| 14 | +/** |
| 15 | + * DeltaReplayer is a dev-tool created for easily replaying operations on the document from stringified deltas. |
| 16 | + */ |
| 17 | +export default class DeltaReplayer { |
| 18 | + /** |
| 19 | + * @param {module:engine/model/document~Document} document Document to reply deltas on. |
| 20 | + * @param {String} logSeparator Separator between deltas. |
| 21 | + * @param {String} stringifiedDeltas Deltas to replay. |
| 22 | + */ |
| 23 | + constructor( document, logSeparator, stringifiedDeltas ) { |
| 24 | + this._document = document; |
| 25 | + this._logSeparator = logSeparator; |
| 26 | + this.setStringifiedDeltas( stringifiedDeltas ); |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * Parses given string containing stringified deltas and sets parsed deltas as deltas to reply. |
| 31 | + * |
| 32 | + * @param {String} stringifiedDeltas Stringified deltas to replay. |
| 33 | + */ |
| 34 | + setStringifiedDeltas( stringifiedDeltas ) { |
| 35 | + if ( stringifiedDeltas === '' ) { |
| 36 | + this._deltasToReplay = []; |
| 37 | + |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + this._deltasToReplay = stringifiedDeltas |
| 42 | + .split( this._logSeparator ) |
| 43 | + .map( stringifiedDelta => JSON.parse( stringifiedDelta ) ); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Returns deltas to reply. |
| 48 | + * |
| 49 | + * @returns {Array.<module:engine/model/delta/delta~Delta>} |
| 50 | + */ |
| 51 | + getDeltasToReplay() { |
| 52 | + return this._deltasToReplay; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Applies all deltas with delay between actions. |
| 57 | + * |
| 58 | + * @param {Number} timeInterval Time between applying deltas. |
| 59 | + * @returns {Promise} |
| 60 | + */ |
| 61 | + play( timeInterval = 1000 ) { |
| 62 | + const deltaReplayer = this; |
| 63 | + |
| 64 | + return new Promise( ( res ) => { |
| 65 | + play(); |
| 66 | + |
| 67 | + function play() { |
| 68 | + if ( deltaReplayer._deltasToReplay.length === 0 ) { |
| 69 | + return res(); |
| 70 | + } |
| 71 | + |
| 72 | + deltaReplayer.applyNextDelta().then( () => { |
| 73 | + setTimeout( play, timeInterval ); |
| 74 | + }, res ); |
| 75 | + } |
| 76 | + } ); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Applies `numberOfDeltas` deltas, beginning after the last applied delta (or first delta, if no deltas were applied). |
| 81 | + * |
| 82 | + * @param {Number} numberOfDeltas Number of deltas to apply. |
| 83 | + * @returns {Promise} |
| 84 | + */ |
| 85 | + applyDeltas( numberOfDeltas ) { |
| 86 | + if ( numberOfDeltas <= 0 ) { |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + return this.applyNextDelta() |
| 91 | + .then( () => this.applyDeltas( numberOfDeltas - 1 ) ) |
| 92 | + .catch( err => console.warn( err ) ); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Applies all deltas to replay at once. |
| 97 | + * |
| 98 | + * @returns {Promise} |
| 99 | + */ |
| 100 | + applyAllDeltas() { |
| 101 | + return this.applyNextDelta() |
| 102 | + .then( () => this.applyAllDeltas() ) |
| 103 | + .catch( () => {} ); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Applies the next delta to replay. |
| 108 | + * |
| 109 | + * @returns {Promise} |
| 110 | + */ |
| 111 | + applyNextDelta() { |
| 112 | + const document = this._document; |
| 113 | + |
| 114 | + return new Promise( ( res, rej ) => { |
| 115 | + document.enqueueChanges( () => { |
| 116 | + const jsonDelta = this._deltasToReplay.shift(); |
| 117 | + |
| 118 | + if ( !jsonDelta ) { |
| 119 | + return rej( new Error( 'No deltas to replay' ) ); |
| 120 | + } |
| 121 | + |
| 122 | + const delta = DeltaFactory.fromJSON( jsonDelta, this._document ); |
| 123 | + |
| 124 | + const batch = document.batch(); |
| 125 | + batch.addDelta( delta ); |
| 126 | + |
| 127 | + for ( const operation of delta.operations ) { |
| 128 | + document.applyOperation( operation ); |
| 129 | + } |
| 130 | + |
| 131 | + res(); |
| 132 | + } ); |
| 133 | + } ); |
| 134 | + } |
| 135 | +} |
0 commit comments