فهرست منبع

added cancel method

Former-commit-id: 93d1e7c000946800866730c5763e3d0af0a033ef
Gildas 6 سال پیش
والد
کامیت
f77160b86b
1فایلهای تغییر یافته به همراه49 افزوده شده و 8 حذف شده
  1. 49 8
      lib/single-file/single-file-core.js

+ 49 - 8
lib/single-file/single-file-core.js

@@ -48,6 +48,12 @@ this.singlefile.lib.core = this.singlefile.lib.core || (() => {
 			await this.runner.initialize();
 			await this.runner.run();
 		}
+		cancel() {
+			this.cancelled = true;
+			if (this.runner) {
+				this.runner.cancel();
+			}
+		}
 		async getPageData() {
 			return this.runner.getPageData();
 		}
@@ -187,6 +193,25 @@ this.singlefile.lib.core = this.singlefile.lib.core || (() => {
 			this.options.win = null;
 		}
 
+		cancel() {
+			this.cancelled = true;
+			this.batchRequest.cancel();
+			if (this.root) {
+				if (this.options.frames) {
+					this.options.frames.forEach(cancelRunner);
+				}
+				if (this.options.imports) {
+					this.options.imports.forEach(cancelRunner);
+				}
+			}
+
+			function cancelRunner(resourceData) {
+				if (resourceData.runner) {
+					resourceData.runner.cancel();
+				}
+			}
+		}
+
 		async run() {
 			if (this.root) {
 				this.processor.initialize(this.batchRequest);
@@ -230,7 +255,9 @@ this.singlefile.lib.core = this.singlefile.lib.core || (() => {
 					log("  -- STARTED task =", task.action);
 				}
 				this.onprogress(new ProgressEvent(STAGE_TASK_STARTED, { pageURL: this.options.url, step, task: task.action, frame }));
-				this.executeTask(task);
+				if (!this.cancelled) {
+					this.executeTask(task);
+				}
 				this.onprogress(new ProgressEvent(STAGE_TASK_ENDED, { pageURL: this.options.url, step, task: task.action, frame }));
 				if (DEBUG) {
 					log("  -- ENDED   task =", task.action, "delay =", Date.now() - startTime);
@@ -245,7 +272,9 @@ this.singlefile.lib.core = this.singlefile.lib.core || (() => {
 						log("  // STARTED task =", task.action);
 					}
 					this.onprogress(new ProgressEvent(STAGE_TASK_STARTED, { pageURL: this.options.url, step, task: task.action, frame }));
-					await this.executeTask(task);
+					if (!this.cancelled) {
+						await this.executeTask(task);
+					}
 					this.onprogress(new ProgressEvent(STAGE_TASK_ENDED, { pageURL: this.options.url, step, task: task.action, frame }));
 					if (DEBUG) {
 						log("  // ENDED task =", task.action, "delay =", Date.now() - startTime);
@@ -308,7 +337,6 @@ this.singlefile.lib.core = this.singlefile.lib.core || (() => {
 			return Promise.all(resourceURLs.map(async requestKey => {
 				const [resourceURL, asBinary] = JSON.parse(requestKey);
 				const resourceRequests = this.requests.get(requestKey);
-				this.requests.delete(requestKey);
 				try {
 					const content = await util.getContent(resourceURL, {
 						asBinary,
@@ -319,18 +347,31 @@ this.singlefile.lib.core = this.singlefile.lib.core || (() => {
 					});
 					indexResource = indexResource + 1;
 					onloadListener({ url: resourceURL });
-					resourceRequests.forEach(callbacks => {
-						const duplicateCallbacks = this.duplicates.get(requestKey);
-						const duplicate = duplicateCallbacks && duplicateCallbacks.length > 1 && duplicateCallbacks.includes(callbacks);
-						callbacks.resolve({ content: content.data, indexResource, duplicate });
-					});
+					if (!this.cancelled) {
+						resourceRequests.forEach(callbacks => {
+							const duplicateCallbacks = this.duplicates.get(requestKey);
+							const duplicate = duplicateCallbacks && duplicateCallbacks.length > 1 && duplicateCallbacks.includes(callbacks);
+							callbacks.resolve({ content: content.data, indexResource, duplicate });
+						});
+					}
 				} catch (error) {
 					indexResource = indexResource + 1;
 					onloadListener({ url: resourceURL });
 					resourceRequests.forEach(resourceRequest => resourceRequest.reject(error));
 				}
+				this.requests.delete(requestKey);
 			}));
 		}
+
+		cancel() {
+			this.cancelled = true;
+			const resourceURLs = Array.from(this.requests.keys());
+			resourceURLs.forEach(requestKey => {
+				const resourceRequests = this.requests.get(requestKey);
+				resourceRequests.forEach(callbacks => callbacks.reject());
+				this.requests.delete(requestKey);
+			});
+		}
 	}
 
 	// ---------