Maciej Bukowski преди 7 години
родител
ревизия
e77dbf43d8
променени са 2 файла, в които са добавени 61 реда и са изтрити 0 реда
  1. 4 0
      packages/ckeditor5-autosave/src/autosave.js
  2. 57 0
      packages/ckeditor5-autosave/tests/autosave.js

+ 4 - 0
packages/ckeditor5-autosave/src/autosave.js

@@ -87,6 +87,10 @@ export default class Autosave extends Plugin {
 	}
 
 	/**
+	 * If the provider is set and new document version exists,
+	 * `_save()` method creates a pending action and calls `provider.save()` method.
+	 * It waits for the result and then removes the created pending action.
+	 *
 	 * @protected
 	 */
 	_save() {

+ 57 - 0
packages/ckeditor5-autosave/tests/autosave.js

@@ -144,6 +144,63 @@ describe( 'Autosave', () => {
 				expect( pendingActions.isPending ).to.be.false;
 			} );
 		} );
+
+		it( 'should flush remaining calls after editor\'s destroy', () => {
+			const spy = sandbox.spy();
+			const savedStates = [];
+
+			autosave.provider = {
+				save() {
+					spy();
+
+					savedStates.push( editor.getData() );
+				}
+			};
+
+			editor.model.change( writer => {
+				writer.setSelection( ModelRange.createIn( editor.model.document.getRoot().getChild( 0 ) ) );
+				editor.model.insertContent( new ModelText( 'foo' ), editor.model.document.selection );
+			} );
+
+			editor.model.change( writer => {
+				writer.setSelection( ModelRange.createIn( editor.model.document.getRoot().getChild( 1 ) ) );
+				editor.model.insertContent( new ModelText( 'bar' ), editor.model.document.selection );
+			} );
+
+			return editor.destroy().then( () => {
+				expect( spy.callCount ).to.equal( 2 );
+				expect( savedStates ).to.deep.equal( [
+					'<p>foo</p><p>paragraph2</p>',
+					'<p>foo</p><p>bar</p>',
+				] );
+			} );
+		} );
+
+		it( 'should work after editor\'s destroy with long server action time', () => {
+			const serverActionSpy = sinon.spy();
+			const pendingActions = editor.plugins.get( PendingActions );
+
+			autosave.provider = {
+				save() {
+					return wait5ms()
+						.then( serverActionSpy );
+				}
+			};
+
+			editor.model.change( writer => {
+				writer.setSelection( ModelRange.createIn( editor.model.document.getRoot().getChild( 0 ) ) );
+				editor.model.insertContent( new ModelText( 'foo' ), editor.model.document.selection );
+			} );
+
+			return editor.destroy()
+				.then( () => {
+					expect( pendingActions.isPending ).to.be.true;
+				} )
+				.then( wait5ms )
+				.then( () => {
+					expect( pendingActions.isPending ).to.be.false;
+				} );
+		} );
 	} );
 
 	function wait5ms() {