All files / app/platform nativeLink.ts

78.26% Statements 18/23
42.85% Branches 3/7
87.5% Functions 7/8
77.27% Lines 17/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 4668x   68x 99x         68x 99x     68x             68x 34x   34x 17x     34x         68x 82x           68x 78x     68x 4x    
import { useEffect, useState } from "react";
 
export function getReactNativeWebView(): typeof window.ReactNativeWebView {
  Iif (window && window.ReactNativeWebView) {
    return window.ReactNativeWebView;
  }
}
 
export function isNativeEmbed(): boolean {
  return !!getReactNativeWebView();
}
 
export function getNativeData() {
  const webview = getReactNativeWebView();
  Iif (webview && webview.injectedObjectJson()) {
    return JSON.parse(webview.injectedObjectJson());
  }
}
 
export function useIsNativeEmbed(): boolean {
  const [isNative, setIsNative] = useState(false);
 
  useEffect(() => {
    setIsNative(isNativeEmbed());
  }, []);
 
  return isNative;
}
 
type MessageType = "sendState" | "clearState";
 
export function sendToNative(type: MessageType, data: any) {
  if (!isNativeEmbed()) return;
  getReactNativeWebView()!.postMessage(
    JSON.stringify({ type: type, data: data })
  );
}
 
export function sendState<T>(key: string, value: T) {
  sendToNative("sendState", { key: key, value: value });
}
 
export function clearState(key: string) {
  sendToNative("clearState", { key: key });
}