@webview-bridge/web 1.0.0 → 1.0.2

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.
@@ -98,29 +98,34 @@ var linkNativeMethod = (options = {
98
98
  if (!window.nativeEmitter) {
99
99
  window.nativeEmitter = emitter;
100
100
  }
101
- const target = bridgeMethods.reduce((acc, method) => {
102
- return {
103
- ...acc,
104
- [method]: (...args) => {
105
- const eventId = createRandomId();
106
- return Promise.race([
107
- createResolver(emitter, method, eventId, () => {
108
- window.ReactNativeWebView?.postMessage(
109
- JSON.stringify({
110
- type: "bridge",
111
- body: {
112
- method,
113
- eventId,
114
- args
115
- }
116
- })
117
- );
118
- }),
119
- timeout(timeoutMs)
120
- ]);
121
- }
122
- };
123
- }, {});
101
+ const target = bridgeMethods.reduce(
102
+ (acc, method) => {
103
+ return {
104
+ ...acc,
105
+ [method]: (...args) => {
106
+ const eventId = createRandomId();
107
+ return Promise.race([
108
+ createResolver(emitter, method, eventId, () => {
109
+ window.ReactNativeWebView?.postMessage(
110
+ JSON.stringify({
111
+ type: "bridge",
112
+ body: {
113
+ method,
114
+ eventId,
115
+ args
116
+ }
117
+ })
118
+ );
119
+ }),
120
+ timeout(timeoutMs)
121
+ ]);
122
+ }
123
+ };
124
+ },
125
+ {
126
+ isWebViewBridgeAvailable: Boolean(window.ReactNativeWebView) && bridgeMethods.length > 0
127
+ }
128
+ );
124
129
  return new Proxy(target, {
125
130
  get: (target2, method) => {
126
131
  if (method in target2) {
@@ -70,29 +70,34 @@ var linkNativeMethod = (options = {
70
70
  if (!window.nativeEmitter) {
71
71
  window.nativeEmitter = emitter;
72
72
  }
73
- const target = bridgeMethods.reduce((acc, method) => {
74
- return {
75
- ...acc,
76
- [method]: (...args) => {
77
- const eventId = createRandomId();
78
- return Promise.race([
79
- createResolver(emitter, method, eventId, () => {
80
- window.ReactNativeWebView?.postMessage(
81
- JSON.stringify({
82
- type: "bridge",
83
- body: {
84
- method,
85
- eventId,
86
- args
87
- }
88
- })
89
- );
90
- }),
91
- timeout(timeoutMs)
92
- ]);
93
- }
94
- };
95
- }, {});
73
+ const target = bridgeMethods.reduce(
74
+ (acc, method) => {
75
+ return {
76
+ ...acc,
77
+ [method]: (...args) => {
78
+ const eventId = createRandomId();
79
+ return Promise.race([
80
+ createResolver(emitter, method, eventId, () => {
81
+ window.ReactNativeWebView?.postMessage(
82
+ JSON.stringify({
83
+ type: "bridge",
84
+ body: {
85
+ method,
86
+ eventId,
87
+ args
88
+ }
89
+ })
90
+ );
91
+ }),
92
+ timeout(timeoutMs)
93
+ ]);
94
+ }
95
+ };
96
+ },
97
+ {
98
+ isWebViewBridgeAvailable: Boolean(window.ReactNativeWebView) && bridgeMethods.length > 0
99
+ }
100
+ );
96
101
  return new Proxy(target, {
97
102
  get: (target2, method) => {
98
103
  if (method in target2) {
@@ -1,6 +1,7 @@
1
+ import { WithAvailable } from "./types";
1
2
  export interface LinkNativeMethodOptions<T extends object> {
2
3
  timeout?: number;
3
4
  throwOnError?: boolean | (keyof T)[];
4
5
  onFallback?: (method: keyof T) => void;
5
6
  }
6
- export declare const linkNativeMethod: <T extends object>(options?: LinkNativeMethodOptions<T>) => T;
7
+ export declare const linkNativeMethod: <T extends object>(options?: LinkNativeMethodOptions<T>) => WithAvailable<T>;
@@ -3,3 +3,6 @@ export type ProceduresObject<T extends Record<string, Procedure>> = {
3
3
  [K in keyof T]: (...args: Parameters<T[K]>) => Promise<Awaited<ReturnType<T[K]>>>;
4
4
  };
5
5
  export type Bridge = <T extends Record<string, Procedure>>(procedures: T) => ProceduresObject<T>;
6
+ export type WithAvailable<T> = {
7
+ isWebViewBridgeAvailable: boolean;
8
+ } & T;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@webview-bridge/web",
3
3
  "type": "module",
4
- "version": "1.0.0",
4
+ "version": "1.0.2",
5
5
  "description": "Integration Web and React Native WebView",
6
6
  "publishConfig": {
7
7
  "access": "public"