8
0
فهرست منبع

Improved implementation of the throttle util.

Maciej Bukowski 7 سال پیش
والد
کامیت
333f31f3e7
2فایلهای تغییر یافته به همراه11 افزوده شده و 9 حذف شده
  1. 8 8
      packages/ckeditor5-autosave/src/autosave.js
  2. 3 1
      packages/ckeditor5-autosave/tests/autosave.js

+ 8 - 8
packages/ckeditor5-autosave/src/autosave.js

@@ -247,8 +247,8 @@ function throttle( fn, wait ) {
 	// Time in ms of the last call.
 	// Time in ms of the last call.
 	let lastCallTime = 0;
 	let lastCallTime = 0;
 
 
-	// Specifies whether there is a pending call.
-	let scheduledCall = false;
+	// Timeout id that enables stopping scheduled call.
+	let timeoutId = null;
 
 
 	// @returns {Boolean} `true` if the original function was or will be called.
 	// @returns {Boolean} `true` if the original function was or will be called.
 	function throttledFn() {
 	function throttledFn() {
@@ -261,13 +261,12 @@ function throttle( fn, wait ) {
 		}
 		}
 
 
 		// Cancel call, as the next call is scheduled.
 		// Cancel call, as the next call is scheduled.
-		if ( scheduledCall ) {
+		if ( timeoutId ) {
 			return false;
 			return false;
 		}
 		}
 
 
 		// Set timeout, so the fn will be called `time` ms after the last call.
 		// Set timeout, so the fn will be called `time` ms after the last call.
-		scheduledCall = true;
-		window.setTimeout( call, lastCallTime + wait - now );
+		timeoutId = window.setTimeout( call, lastCallTime + wait - now );
 
 
 		return true;
 		return true;
 	}
 	}
@@ -275,18 +274,19 @@ function throttle( fn, wait ) {
 	throttledFn.flush = flush;
 	throttledFn.flush = flush;
 
 
 	function flush() {
 	function flush() {
-		if ( scheduledCall ) {
+		if ( timeoutId ) {
+			window.clearTimeout( timeoutId );
+			timeoutId = null;
+
 			call();
 			call();
 		}
 		}
 
 
-		scheduledCall = false;
 		lastCallTime = 0;
 		lastCallTime = 0;
 	}
 	}
 
 
 	// Calls the original function and updates internals.
 	// Calls the original function and updates internals.
 	function call() {
 	function call() {
 		lastCallTime = Date.now();
 		lastCallTime = Date.now();
-		scheduledCall = false;
 
 
 		fn();
 		fn();
 	}
 	}

+ 3 - 1
packages/ckeditor5-autosave/tests/autosave.js

@@ -406,7 +406,7 @@ describe( 'Autosave', () => {
 			} );
 			} );
 		} );
 		} );
 
 
-		it( 'should work after editor\'s destroy with long server\'s action time', () => {
+		it( 'should work after editor\'s destroy with long server\'s response time', () => {
 			sandbox.useFakeTimers();
 			sandbox.useFakeTimers();
 			const pendingActions = editor.plugins.get( PendingActions );
 			const pendingActions = editor.plugins.get( PendingActions );
 			const serverActionSpy = sinon.spy();
 			const serverActionSpy = sinon.spy();
@@ -427,9 +427,11 @@ describe( 'Autosave', () => {
 					expect( pendingActions.isPending ).to.be.true;
 					expect( pendingActions.isPending ).to.be.true;
 					sandbox.clock.tick( 500 );
 					sandbox.clock.tick( 500 );
 				} )
 				} )
+				.then( () => Promise.resolve() )
 				.then( () => {
 				.then( () => {
 					expect( pendingActions.isPending ).to.be.false;
 					expect( pendingActions.isPending ).to.be.false;
 					sinon.assert.calledOnce( serverActionSpy );
 					sinon.assert.calledOnce( serverActionSpy );
+					sinon.assert.calledOnce( serverActionStub );
 				} );
 				} );
 		} );
 		} );
 	} );
 	} );