import * as gracely from "gracely";
import * as model from "@payfunc/model";
export class OrderHandler {
  static register(callback) {
    this.callbacks.push(callback);
    callback(this.orders);
    if (!OrderHandler.timer) {
      this.download();
      OrderHandler.timer = setInterval(OrderHandler.download, 5000);
      model.Connection.keyChanged.push(() => this.download());
    }
  }
  static async download() {
    if (!this.downloading) {
      try {
        this.downloading = true;
        const response = await model.Connection.get("order");
        this.downloading = false;
        OrderHandler.orders = gracely.Error.is(response) || typeof response == "string" ? OrderHandler.orders : response;
        OrderHandler.callbacks.forEach(callback => callback(OrderHandler.orders));
      }
      catch (_a) {
        this.downloading = false;
      }
    }
  }
}
OrderHandler.downloading = false;
OrderHandler.callbacks = [];
