@chromahq/react 1.0.25 → 1.0.26

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.js +91 -1
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -762,4 +762,94 @@ const useConnectionStatus = (store) => {
762
762
  };
763
763
  };
764
764
 
765
- export { BridgeProvider, useBridge, useBridgeMutation, useBridgeQuery, useConnectionStatus };
765
+ const globalSubscribers = /* @__PURE__ */ new Set();
766
+ function subscribeToHealth(callback) {
767
+ globalSubscribers.add(callback);
768
+ return () => {
769
+ globalSubscribers.delete(callback);
770
+ };
771
+ }
772
+ let currentHealthStatus = "unknown";
773
+ let currentIsHealthy = false;
774
+ function getHealthStatus() {
775
+ return { status: currentHealthStatus, isHealthy: currentIsHealthy };
776
+ }
777
+ function broadcastHealthChange(status, isHealthy) {
778
+ currentHealthStatus = status;
779
+ currentIsHealthy = isHealthy;
780
+ globalSubscribers.forEach((callback) => {
781
+ try {
782
+ callback(status, isHealthy);
783
+ } catch (e) {
784
+ console.error("[ServiceWorkerHealth] Subscriber error:", e);
785
+ }
786
+ });
787
+ }
788
+ const ServiceWorkerHealthContext = createContext(null);
789
+ const ServiceWorkerHealthProvider = ({
790
+ children,
791
+ onHealthChange
792
+ }) => {
793
+ const bridgeContext = useContext(BridgeContext);
794
+ const [lastHealthyAt, setLastHealthyAt] = useState(null);
795
+ const bridgeStatus = bridgeContext?.status;
796
+ const isServiceWorkerAlive = bridgeContext?.isServiceWorkerAlive ?? false;
797
+ const derivedStatus = useMemo(() => {
798
+ if (!bridgeStatus) return "unknown";
799
+ if (bridgeStatus === "connected" && isServiceWorkerAlive) {
800
+ return "healthy";
801
+ }
802
+ if (bridgeStatus === "reconnecting" || bridgeStatus === "connecting") {
803
+ return "recovering";
804
+ }
805
+ return "unhealthy";
806
+ }, [bridgeStatus, isServiceWorkerAlive]);
807
+ const isHealthy = derivedStatus === "healthy";
808
+ const isRecovering = derivedStatus === "recovering";
809
+ const isLoading = derivedStatus === "recovering" || derivedStatus === "unknown";
810
+ useEffect(() => {
811
+ if (isHealthy) {
812
+ setLastHealthyAt(Date.now());
813
+ }
814
+ }, [isHealthy]);
815
+ useEffect(() => {
816
+ broadcastHealthChange(derivedStatus, isHealthy);
817
+ onHealthChange?.(derivedStatus, isHealthy);
818
+ }, [derivedStatus, isHealthy, onHealthChange]);
819
+ const forceReconnect = useCallback(() => {
820
+ bridgeContext?.reconnect();
821
+ }, [bridgeContext]);
822
+ const value = useMemo(
823
+ () => ({
824
+ status: derivedStatus,
825
+ isHealthy,
826
+ isRecovering,
827
+ isLoading,
828
+ lastHealthyAt,
829
+ forceReconnect
830
+ }),
831
+ [derivedStatus, isHealthy, isRecovering, isLoading, lastHealthyAt, forceReconnect]
832
+ );
833
+ return /* @__PURE__ */ jsx(ServiceWorkerHealthContext.Provider, { value, children });
834
+ };
835
+ function useServiceWorkerHealth() {
836
+ const context = useContext(ServiceWorkerHealthContext);
837
+ if (!context) {
838
+ throw new Error(
839
+ "useServiceWorkerHealth must be used within a ServiceWorkerHealthProvider. Wrap your app with <ServiceWorkerHealthProvider> inside <BridgeProvider>."
840
+ );
841
+ }
842
+ return context;
843
+ }
844
+ function useServiceWorkerHealthSimple() {
845
+ const bridgeContext = useContext(BridgeContext);
846
+ const isHealthy = bridgeContext?.status === "connected" && bridgeContext?.isServiceWorkerAlive === true;
847
+ const isRecovering = bridgeContext?.status === "reconnecting" || bridgeContext?.status === "connecting";
848
+ const isLoading = !isHealthy && (isRecovering || !bridgeContext);
849
+ const reconnect = useCallback(() => {
850
+ bridgeContext?.reconnect();
851
+ }, [bridgeContext]);
852
+ return { isHealthy, isRecovering, isLoading, reconnect };
853
+ }
854
+
855
+ export { BridgeProvider, ServiceWorkerHealthProvider, getHealthStatus, subscribeToHealth, useBridge, useBridgeMutation, useBridgeQuery, useConnectionStatus, useServiceWorkerHealth, useServiceWorkerHealthSimple };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chromahq/react",
3
- "version": "1.0.25",
3
+ "version": "1.0.26",
4
4
  "description": "React bindings for the Chroma Chrome extension framework",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",