import logger from './logger.js';
import EventEmitter from 'node:events';
import safe from 'safetydance';

const { log } = logger('asynctask');

// this runs in-process
class AsyncTask extends EventEmitter {
    name;
    #abortController;

    constructor(name) {
        super();
        this.name = name;
        this.#abortController = new AbortController();
    }

    async _run(/*signal*/) {
        // subclasses implement this
    }

    async start() { // should not throw!
        log(`start: ${this.name} started`);
        const [error] = await safe(this._run(this.#abortController.signal));
        log(`start: ${this.name} finished`);
        this.emit('done', { errorMessage: error?.message || '' });
        this.#abortController = null;
    }

    stop() {
        if (this.#abortController === null) return; // already finished
        log(`stop: ${this.name} . sending abort signal`);
        this.#abortController.abort();
    }

    emitProgress(percent, message) {
        this.emit('data', 'progress', { percent, message });
    }

    emitData(obj) {
        this.emit('data', 'data', obj);
    }
}

export default {
    AsyncTask
};
