import { assertObject, assertString } from "../../shared/src/asserts.js";
import { BroadcastChannel as bc } from "../../shared/src/broadcast-channel.js";
function makeChannelName(replicacheName) {
  return `replicache-on-persist:${replicacheName}`;
}
function assertPersistInfo(value) {
  assertObject(value);
  assertString(value.clientGroupID);
  assertString(value.clientID);
}
function initOnPersistChannel(replicacheName, signal, handlePersist) {
  if (signal.aborted) {
    return () => void 0;
  }
  const channel = new bc(makeChannelName(replicacheName));
  channel.onmessage = (e) => {
    const { data } = e;
    assertPersistInfo(data);
    handlePersist({
      clientGroupID: data.clientGroupID,
      clientID: data.clientID
    });
  };
  signal.addEventListener("abort", () => channel.close(), { once: true });
  return (persistInfo) => {
    if (signal.aborted) {
      return;
    }
    channel.postMessage(persistInfo);
    handlePersist(persistInfo);
  };
}
export {
  initOnPersistChannel
};
//# sourceMappingURL=on-persist-channel.js.map
