Extractor.stop()
This commit is contained in:
		| @ -51,14 +51,19 @@ export class Extractor { | ||||
|     /** | ||||
|      * Start the task chain. | ||||
|      */ | ||||
|     async start() { | ||||
|     start() { | ||||
|         return this._startTasks(0); | ||||
|     } | ||||
|     stop() { | ||||
|         for (let i = 0; i < this._tasks.length; i++) { | ||||
|             this._tasks[i].stop(); | ||||
|         } | ||||
|     } | ||||
|     /** | ||||
|      * restart from specified task, but don't restart the previous tasks. | ||||
|      * @param {number} from where to restart the tasks, begins with 0 | ||||
|      */ | ||||
|     async restart(from: number = 0) { | ||||
|     restart(from: number = 0) { | ||||
|         let id = this._checkTaskId(from, 0); | ||||
|         if (id < 0) return; | ||||
|         for (let i = id; i < this._tasks.length; i++) { | ||||
|  | ||||
| @ -10,6 +10,7 @@ export class Task { | ||||
|     private _itemsSelector: string; | ||||
|     private _fieldSelectors: string[]; | ||||
|     private _urls: string[] = []; | ||||
|     private _running = false; | ||||
|  | ||||
|     constructor(options: any, ...arg: any); | ||||
|     constructor(options: any, itemsSelector: string, fieldSelectors: string[]); | ||||
| @ -49,8 +50,13 @@ export class Task { | ||||
|         this._data_keys = []; | ||||
|         return this; | ||||
|     } | ||||
|     stop() { | ||||
|         this._running = false; | ||||
|     } | ||||
|     async execute(tab: chrome.tabs.Tab, upstreamData?: ExtractResult): Promise<void> { | ||||
|         if (!tab) return Promise.reject("No tab to execute the task."); | ||||
|         if (this._running) return Promise.reject("The task is running. Please wait..."); | ||||
|         this._running = true; | ||||
|         let urls = this._urls | ||||
|         if (!urls.length) { | ||||
|             if (upstreamData) { | ||||
| @ -63,6 +69,10 @@ export class Task { | ||||
|             this._data[key] = results; | ||||
|             this._data_keys.push(key); | ||||
|         } | ||||
|         let runningCheck = (fn: () => Promise<any>): Promise<any> => { | ||||
|             if (!this._running) return Promise.reject("The task is stopped by user."); | ||||
|             return fn(); | ||||
|         } | ||||
|         return urls.reduce((p, url, i) => p.then( | ||||
|             results => { | ||||
|                 if (i > 0 && results instanceof Array) { | ||||
| @ -70,12 +80,13 @@ export class Task { | ||||
|                     saveResult(results, lastURL); | ||||
|                 } | ||||
|                 if (this._data[url]) return; | ||||
|                 let pms: Promise<any> = redirectTab(tab, url); | ||||
|  | ||||
|                 let pms: Promise<any> = runningCheck(() => redirectTab(tab, url)); | ||||
|                 if (this._options["scrollToBottom"]) { | ||||
|                     pms = pms.then(() => scrollToBottom(tab)); | ||||
|                     pms = pms.then(() => runningCheck(() => scrollToBottom(tab))); | ||||
|                 } | ||||
|                 return pms.then( | ||||
|                     () => extractTabData(tab, this._itemsSelector, this._fieldSelectors) | ||||
|                     () => runningCheck(() => extractTabData(tab, this._itemsSelector, this._fieldSelectors)) | ||||
|                 ); | ||||
|             } | ||||
|         ), Promise.resolve<string[][]>(null)).then( | ||||
| @ -83,9 +94,14 @@ export class Task { | ||||
|                 if (results && results.length) { | ||||
|                     let lastURL = urls[urls.length - 1]; | ||||
|                     saveResult(results, lastURL); | ||||
|                     return; | ||||
|                     this._running = false; | ||||
|                 } | ||||
|             } | ||||
|         ).catch( | ||||
|             e => { | ||||
|                 this._running = false; | ||||
|                 return Promise.reject(e); | ||||
|             } | ||||
|         ); | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user