Explorar o código

removed emulation of rAF with setTimeout (fix #151)

Gildas %!s(int64=7) %!d(string=hai) anos
pai
achega
69a4ea8acb
Modificáronse 1 ficheiros con 7 adicións e 18 borrados
  1. 7 18
      lib/hooks/hooks-frame.js

+ 7 - 18
lib/hooks/hooks-frame.js

@@ -18,7 +18,7 @@
  *   along with SingleFile.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-/* global window, addEventListener, dispatchEvent, CustomEvent, document, HTMLDocument, FileReader, Blob, setTimeout, clearTimeout, screen, Element, UIEvent */
+/* global window, addEventListener, dispatchEvent, CustomEvent, document, HTMLDocument, FileReader, Blob, screen, Element, UIEvent */
 
 this.hooksFrame = this.hooksFrame || (() => {
 
@@ -207,7 +207,7 @@ this.hooksFrame = this.hooksFrame || (() => {
 
 		let warningRequestAnimationFrameDisplayed;
 		const pendingRequestAnimationFrameCalls = new Map();
-		const pendingSetTimeoutCalls = new Map();
+		let lastTimestamp = 0;
 		window.requestAnimationFrame = function (callback) {
 			if (!warningRequestAnimationFrameDisplayed) {
 				console.warn("SingleFile is hooking the requestAnimationFrame and cancelAnimationFrame functions to load deferred images."); // eslint-disable-line no-console
@@ -215,14 +215,12 @@ this.hooksFrame = this.hooksFrame || (() => {
 			}
 			let requestId;
 			if (loadDeferredImages) {
-				requestId = setTimeout(() => {
-					pendingSetTimeoutCalls.delete(requestId);
-					callback(0);
-				}, 0);
-				pendingSetTimeoutCalls.set(requestId, callback);
+				requestId = 0;
+				callback(lastTimestamp);
 			} else {
 				requestId = requestAnimationFrame(timestamp => {
 					pendingRequestAnimationFrameCalls.delete(requestId);
+					lastTimestamp = timestamp;
 					callback(timestamp);
 				});
 				pendingRequestAnimationFrameCalls.set(requestId, callback);
@@ -232,17 +230,8 @@ this.hooksFrame = this.hooksFrame || (() => {
 		window.requestAnimationFrame.toString = function () { return "requestAnimationFrame() { [native code] }"; };
 
 		window.cancelAnimationFrame = function (requestId) {
-			const pendingCallDeleted = pendingRequestAnimationFrameCalls.delete(requestId);
-			if (pendingCallDeleted) {
-				return cancelAnimationFrame(requestId);
-			} else {
-				const pendingCallDeleted = pendingSetTimeoutCalls.delete(requestId);
-				if (pendingCallDeleted) {
-					return clearTimeout(requestId);
-				} else {
-					return cancelAnimationFrame(requestId);
-				}
-			}
+			pendingRequestAnimationFrameCalls.delete(requestId);
+			return cancelAnimationFrame(requestId);
 		};
 		window.cancelAnimationFrame.toString = function () { return "cancelAnimationFrame() { [native code] }"; };