class ServiceRunner {
  #lc;
  #instances = /* @__PURE__ */ new Map();
  #create;
  #isValid;
  constructor(lc, factory, isValid = () => true) {
    this.#lc = lc;
    this.#create = factory;
    this.#isValid = isValid;
  }
  /**
   * Creates and runs the Service with the given `id`, returning
   * an existing one if it is still running a valid.
   */
  getService(id) {
    const existing = this.#instances.get(id);
    if (existing && this.#isValid(existing)) {
      return existing;
    }
    const service = this.#create(id);
    this.#instances.set(id, service);
    void service.run().catch(
      (e) => this.#lc.error?.(
        `Error running ${service.constructor?.name} ${service.id}`,
        e
      )
    ).finally(() => {
      this.#instances.delete(id);
    });
    return service;
  }
  get size() {
    return this.#instances.size;
  }
  getServices() {
    return this.#instances.values();
  }
}
export {
  ServiceRunner
};
//# sourceMappingURL=runner.js.map
