deltareplayer.js 3.3 KB

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