save and load state

This commit is contained in:
2020-01-12 16:19:38 +08:00
parent f1cf32b83a
commit c7f4fe7cc4
7 changed files with 85 additions and 17 deletions

View File

@ -1,9 +1,31 @@
var __EXTRACTOR_STATE__ = "";
class Extractor {
constructor(options) {
this._tasks = [];
this._running = false;
this._options = options;
}
/**
* Save current state, in case we restore it later.
*/
save() {
saveFile(JSON.stringify(this), 'application/json', 'state.json');
}
/**
* Restore previous state by loading from saved state.
*/
load() {
if (!__EXTRACTOR_STATE__) {
console.log('No state found. \nPlease upload a saved state from the popup window first.');
return;
}
let state = JSON.parse(__EXTRACTOR_STATE__);
__EXTRACTOR_STATE__ = "";
this._options = state._options;
this._tasks = state._tasks.map(t => new Task(this._options, 'whaterver', ['whaterver']).load(t));
return this;
}
/**
* Add a task to Extractor. \n
* One Extractor could has multiple tasks, which orgnized in a task chian.
@ -76,7 +98,7 @@ class Extractor {
}, Promise.resolve(undefined)).then(
() => {
this._running = false;
this.save();
this.export();
}
).catch(err => {
this._running = false;
@ -84,10 +106,10 @@ class Extractor {
});
}
/**
* Save result of a task
* export result of a task to CSV
* @param {number} taskid which task id to save, begins with 0
*/
save(taskid) {
export(taskid) {
let id = this._checkTaskId(taskid, this._tasks.length - 1);
if (id < 0) return;
let results = this._tasks[id].results