import { RWLock } from "@rocicorp/lock";
import { promiseVoid } from "../../../shared/src/resolved-promises.js";
import { ReadImpl } from "./read-impl.js";
import { throwIfStoreClosed } from "./throw-if-closed.js";
import { WriteImpl } from "./write-impl.js";
const stores = /* @__PURE__ */ new Map();
function dropMemStore(name) {
  stores.delete(name);
  return promiseVoid;
}
class MemStore {
  #map;
  #rwLock;
  #closed = false;
  constructor(name) {
    const entry = stores.get(name);
    let lock;
    let map;
    if (entry) {
      ({ lock, map } = entry);
    } else {
      lock = new RWLock();
      map = /* @__PURE__ */ new Map();
      stores.set(name, { lock, map });
    }
    this.#rwLock = lock;
    this.#map = map;
  }
  async read() {
    throwIfStoreClosed(this);
    const release = await this.#rwLock.read();
    return new ReadImpl(this.#map, release);
  }
  async write() {
    throwIfStoreClosed(this);
    const release = await this.#rwLock.write();
    return new WriteImpl(this.#map, release);
  }
  close() {
    this.#closed = true;
    return promiseVoid;
  }
  get closed() {
    return this.#closed;
  }
}
export {
  MemStore,
  dropMemStore
};
//# sourceMappingURL=mem-store.js.map
