import EventEmitter from "events";
import {Logger} from "../logging/logger.js";

export class BasePoller extends EventEmitter {

    constructor() {
        super();
        this.task = null;
    }

    async pollOnce(){
        throw new Error("Not implemented.");
    }

    start(interval) {
        if(this.task){
            Logger.error("A task has already been scheduled");
            return;
        }

        super.task = setInterval(async () => {
            await this.pollOnce();
        }, interval);
    }

    stop() {
        if (this.task) {
            clearInterval(this.task);
            super.task = null;
        }
    }
}