Skip to content

Commit

Permalink
feat: linkBridge debug mode (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
gronxb authored Dec 11, 2024
1 parent 99c0718 commit 6f44621
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
1 change: 1 addition & 0 deletions example/shared-state-integration-react/react/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { AppBridge } from "@webview-bridge-example-shared-state-integration

const bridge = linkBridge<AppBridge>({
throwOnError: true,
debug: true,
initialBridge: {
count: 10,
data: {
Expand Down
16 changes: 12 additions & 4 deletions packages/web/src/linkBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ export interface LinkBridgeOptions<
T extends BridgeStore<T extends Bridge ? T : any>,
V extends ParserSchema<any>,
> {
/**
* If `true`, console warnings will be displayed.
* @default false
*/
debug?: boolean;
/**
* It is possible to configure `initialBridge` to operate in a non-React Native environment.
* Prioritize applying the bridge of the React Native WebView, and if it is unavailable, apply the `initialBridge`.
Expand Down Expand Up @@ -65,6 +70,7 @@ export const linkBridge = <
options: LinkBridgeOptions<T, V> = {
timeout: 2000,
throwOnError: false,
debug: false,
},
): LinkBridge<ExcludePrimitive<ExtractStore<T>>, Omit<T, "setState">, V> => {
if (typeof window === "undefined") {
Expand All @@ -88,7 +94,7 @@ export const linkBridge = <
} as LinkBridge<ExcludePrimitive<ExtractStore<T>>, Omit<T, "setState">, V>;
}

if (!window.ReactNativeWebView) {
if (options.debug && !window.ReactNativeWebView) {
console.warn("[WebViewBridge] Not in a WebView environment");
}

Expand Down Expand Up @@ -147,9 +153,11 @@ export const linkBridge = <
};
}

console.warn(
`[WebViewBridge] ${methodName} is not defined, using fallback.`,
);
if (options.debug) {
console.warn(
`[WebViewBridge] ${methodName} is not defined, using fallback.`,
);
}
return () => Promise.resolve();
},
}) as LinkBridge<ExcludePrimitive<ExtractStore<T>>, Omit<T, "setState">, V>;
Expand Down

0 comments on commit 6f44621

Please sign in to comment.