8
0

deltareplayer.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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, console */
  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.
  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. * @param {String} stringifiedDeltas Deltas to replay.
  26. */
  27. setStringifiedDeltas( stringifiedDeltas ) {
  28. if ( stringifiedDeltas === '' ) {
  29. this._deltasToReplay = [];
  30. return;
  31. }
  32. this._deltasToReplay = stringifiedDeltas
  33. .split( this._logSeparator )
  34. .map( stringifiedDelta => JSON.parse( stringifiedDelta ) );
  35. }
  36. getDeltasToReplay() {
  37. return this._deltasToReplay;
  38. }
  39. /**
  40. * Applies all deltas with delay between actions.
  41. *
  42. * @param {Number} timeInterval Time between applying deltas.
  43. * @returns {Promise}
  44. */
  45. play( timeInterval = 1000 ) {
  46. const deltaReplayer = this;
  47. return new Promise( ( res ) => {
  48. play();
  49. function play() {
  50. if ( deltaReplayer._deltasToReplay.length === 0 ) {
  51. return res();
  52. }
  53. deltaReplayer.applyNextDelta().then( () => {
  54. setTimeout( play, timeInterval );
  55. }, res );
  56. }
  57. } );
  58. }
  59. /**
  60. * @param {Number} numberOfDeltas Number of deltas to apply.
  61. * @returns {Promise}
  62. */
  63. applyDeltas( numberOfDeltas ) {
  64. if ( numberOfDeltas <= 0 ) {
  65. return;
  66. }
  67. return this.applyNextDelta()
  68. .then( () => this.applyDeltas( numberOfDeltas - 1 ) )
  69. .catch( err => console.warn( err ) );
  70. }
  71. /**
  72. * @returns {Promise}
  73. */
  74. applyAllDeltas() {
  75. return this.applyNextDelta()
  76. .then( () => this.applyAllDeltas() )
  77. .catch( () => {} );
  78. }
  79. /**
  80. * @returns {Promise}
  81. */
  82. applyNextDelta() {
  83. const document = this._document;
  84. return new Promise( ( res, rej ) => {
  85. document.enqueueChanges( () => {
  86. const jsonDelta = this._deltasToReplay.shift();
  87. if ( !jsonDelta ) {
  88. return rej( new Error( 'No deltas to replay' ) );
  89. }
  90. const delta = DeltaFactory.fromJSON( jsonDelta, this._document );
  91. const batch = document.batch();
  92. batch.addDelta( delta );
  93. for ( const operation of delta.operations ) {
  94. document.applyOperation( operation );
  95. }
  96. res();
  97. } );
  98. } );
  99. }
  100. }