import "./chunk-F5QR3K72.js";

// ../reflect-react/src/index.ts
import { useEffect, useState } from "react";
function usePresence(r) {
  const [presentClientIDs, setPresentClientIDs] = useState([]);
  useEffect(() => {
    if (!r) {
      return;
    }
    const unsubscribe = r.subscribeToPresence((ids) => {
      setPresentClientIDs(ids);
    });
    return () => {
      unsubscribe();
      setPresentClientIDs([]);
    };
  }, [r]);
  return presentClientIDs;
}

// ../../node_modules/replicache-react/out/index.mjs
import { useEffect as useEffect2, useState as useState2 } from "react";
import { unstable_batchedUpdates } from "react-dom";
var hasPendingCallback = false;
var callbacks = [];
function doCallback() {
  const cbs = callbacks;
  callbacks = [];
  hasPendingCallback = false;
  unstable_batchedUpdates(() => {
    for (const callback of cbs) {
      callback();
    }
  });
}
function useSubscribe(r, query, def, deps = []) {
  const [snapshot, setSnapshot] = useState2(void 0);
  useEffect2(() => {
    if (!r) {
      return;
    }
    const unsubscribe = r.subscribe(query, {
      onData: (data) => {
        callbacks.push(() => setSnapshot(data));
        if (!hasPendingCallback) {
          void Promise.resolve().then(doCallback);
          hasPendingCallback = true;
        }
      }
    });
    return () => {
      unsubscribe();
      setSnapshot(void 0);
    };
  }, [r, ...deps]);
  if (snapshot === void 0) {
    return def;
  }
  return snapshot;
}
export {
  usePresence,
  useSubscribe
};
//# sourceMappingURL=react.js.map
