const EventEmitter = require("events");

const { readModel } = require("../../SDK/esSDK/readModel");
const esReadModel = new readModel();


export class EsWorker extends EventEmitter{
  constructor(params) {
    super();
    this.isClose = false;
  }

  async subscribe(params) {
    var streamId =params.streamId;
    const subscription = await esReadModel.subscribe(streamId);
    console.log("【eventStore】subscribeES",streamId);
    for await (const resolvedEvent of subscription) {
      if (this.isClose) {
        console.log("【eventStore】closeConnection");
       return;
      }
      this.emit("event", resolvedEvent.event);
    }
  }

  start(){
    this.isClose=true;
  }

  stop() {
    this.isClose=false;
  }

}