/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import { Logger } from "#log/Logger.js";
import { MatterAggregateError } from "#MatterError.js";
import { MaybePromise } from "./Promises.js";
import { BasicSet } from "./Set.js";
const logger = Logger.get("Multiplex");
class BasicMultiplex {
  #workers = new BasicSet();
  add(worker, description) {
    if (!MaybePromise.is(worker)) {
      return;
    }
    const entry = {
      done: Promise.resolve(worker).catch((e) => {
        let message = "Error";
        if (description) {
          message = `${message} ${description}`;
        }
        logger.error(`${message}:`, e);
      }).finally(() => this.#workers.delete(entry)),
      description
    };
    this.#workers.add(entry);
  }
  async close() {
    while (this.#workers.size) {
      await MatterAggregateError.allSettled([...this.#workers].map((entry) => entry.done));
    }
  }
  then(onfulfilled, onrejected) {
    return this.close().then(onfulfilled, onrejected);
  }
  [Symbol.asyncDispose] = this.close.bind(this);
}
export {
  BasicMultiplex
};
//# sourceMappingURL=Multiplex.js.map
