running detect
This commit is contained in:
		| @ -2,6 +2,7 @@ class Extractor { | ||||
|     constructor() { | ||||
|         this._tasks = []; | ||||
|         this._tab = undefined; | ||||
|         this._running = false; | ||||
|         this._results = {}; | ||||
|     } | ||||
|     /** | ||||
| @ -35,6 +36,10 @@ class Extractor { | ||||
|      * Start the task chain. | ||||
|      */ | ||||
|     async start() { | ||||
|         if (this._running) { | ||||
|             console.log('The Extractor is running. Please wait..'); | ||||
|             return; | ||||
|         } | ||||
|         if (!this._tasks.length) { | ||||
|             console.log('No task to run.'); | ||||
|             return; | ||||
| @ -47,6 +52,7 @@ class Extractor { | ||||
|         } else { | ||||
|             this._tab = await getActiveTab(false); | ||||
|         } | ||||
|         this._running = true; | ||||
|         return this._tasks.reduce((pms, args, i, tasks) => { | ||||
|             return pms.then( | ||||
|                 result => { | ||||
| @ -57,15 +63,23 @@ class Extractor { | ||||
|         }, Promise.resolve(undefined)).then( | ||||
|             result => { | ||||
|                 this._results[this._tasks[this._tasks.length - 1]] = result; | ||||
|                 this._running = false; | ||||
|                 this.save(); | ||||
|             } | ||||
|         ).catch(err => console.log(err)); | ||||
|         ).catch(err => { | ||||
|             this._running = false; | ||||
|             console.log(err) | ||||
|         }); | ||||
|     } | ||||
|     /** | ||||
|      * restart from specified task, but don't restart the previous tasks. | ||||
|      * @param {number} taskid from which restart the tasks | ||||
|      */ | ||||
|     async restart(taskid) { | ||||
|         if (this._running) { | ||||
|             console.log('The Extractor is running. Please wait..'); | ||||
|             return; | ||||
|         } | ||||
|         taskid = this._checkTaskId(taskid, 1); | ||||
|         if (!taskid) return; | ||||
|         if (taskid == 1) { | ||||
| @ -77,6 +91,7 @@ class Extractor { | ||||
|             console.log(`No result cache for task (id ${taskid}). \nMake sure call ".start()" before ".restart()"?`); | ||||
|             return; | ||||
|         } | ||||
|         this._running = true; | ||||
|         this._tab = await createTab(parseUrls(cache)[0], false) | ||||
|         return this._tasks.slice(taskid - 1).reduce((pms, args, i, tasks) => { | ||||
|             return pms.then( | ||||
| @ -87,9 +102,13 @@ class Extractor { | ||||
|         }, Promise.resolve(cache)).then( | ||||
|             result => { | ||||
|                 this._results[this._tasks[this._tasks.length - 1]] = result; | ||||
|                 this._running = false; | ||||
|                 this.save(); | ||||
|             } | ||||
|         ).catch(err => console.log(err)); | ||||
|         ).catch(err => { | ||||
|             this._running = false; | ||||
|             console.log(err) | ||||
|         }); | ||||
|     } | ||||
|     /** | ||||
|      * Save result of a task | ||||
|  | ||||
		Reference in New Issue
	
	Block a user