@webview-bridge/web 1.1.2 → 1.2.0
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.
package/dist/commonjs/index.cjs
CHANGED
|
@@ -57,10 +57,10 @@ var createEvents = () => ({
|
|
|
57
57
|
};
|
|
58
58
|
}
|
|
59
59
|
});
|
|
60
|
-
var createResolver = (emitter2,
|
|
60
|
+
var createResolver = (emitter2, methodName, eventId, evaluate, failHandler = false) => {
|
|
61
61
|
return new Promise((resolve, reject) => {
|
|
62
62
|
const unbind = emitter2.on(
|
|
63
|
-
`${
|
|
63
|
+
`${methodName}-${eventId}`,
|
|
64
64
|
(data, throwOccurred) => {
|
|
65
65
|
unbind();
|
|
66
66
|
if (throwOccurred) {
|
|
@@ -104,26 +104,26 @@ var timeout = (ms, throwOnError = true) => {
|
|
|
104
104
|
|
|
105
105
|
// src/linkNativeMethod.ts
|
|
106
106
|
var emitter = createEvents();
|
|
107
|
-
var createNativeMethod = (
|
|
107
|
+
var createNativeMethod = (methodName, timeoutMs, throwOnError) => (...args) => {
|
|
108
108
|
const eventId = createRandomId();
|
|
109
109
|
return Promise.race([
|
|
110
110
|
createResolver(
|
|
111
111
|
emitter,
|
|
112
|
-
|
|
112
|
+
methodName,
|
|
113
113
|
eventId,
|
|
114
114
|
() => {
|
|
115
115
|
window.ReactNativeWebView?.postMessage(
|
|
116
116
|
JSON.stringify({
|
|
117
117
|
type: "bridge",
|
|
118
118
|
body: {
|
|
119
|
-
method,
|
|
119
|
+
method: methodName,
|
|
120
120
|
eventId,
|
|
121
121
|
args
|
|
122
122
|
}
|
|
123
123
|
})
|
|
124
124
|
);
|
|
125
125
|
},
|
|
126
|
-
throwOnError && new NativeMethodError(
|
|
126
|
+
throwOnError && new NativeMethodError(methodName)
|
|
127
127
|
),
|
|
128
128
|
timeout(timeoutMs, throwOnError)
|
|
129
129
|
]);
|
|
@@ -135,7 +135,8 @@ var linkNativeMethod = (options = {
|
|
|
135
135
|
const {
|
|
136
136
|
timeout: timeoutMs = 2e3,
|
|
137
137
|
throwOnError = false,
|
|
138
|
-
onFallback
|
|
138
|
+
onFallback,
|
|
139
|
+
onReady
|
|
139
140
|
} = options;
|
|
140
141
|
if (!window.ReactNativeWebView) {
|
|
141
142
|
console.warn("[WebViewBridge] Not in a WebView environment");
|
|
@@ -148,62 +149,64 @@ var linkNativeMethod = (options = {
|
|
|
148
149
|
return throwOnError === true || Array.isArray(throwOnError) && throwOnError.includes(methodName);
|
|
149
150
|
};
|
|
150
151
|
const target = bridgeMethods.reduce(
|
|
151
|
-
(acc,
|
|
152
|
+
(acc, methodName) => {
|
|
152
153
|
return {
|
|
153
154
|
...acc,
|
|
154
|
-
[
|
|
155
|
-
|
|
155
|
+
[methodName]: createNativeMethod(
|
|
156
|
+
methodName,
|
|
156
157
|
timeoutMs,
|
|
157
|
-
willMethodThrowOnError(
|
|
158
|
+
willMethodThrowOnError(methodName)
|
|
158
159
|
)
|
|
159
160
|
};
|
|
160
161
|
},
|
|
161
162
|
{
|
|
162
163
|
isWebViewBridgeAvailable: Boolean(window.ReactNativeWebView) && bridgeMethods.length > 0,
|
|
163
|
-
isNativeMethodAvailable(
|
|
164
|
-
return typeof
|
|
164
|
+
isNativeMethodAvailable(methodName) {
|
|
165
|
+
return typeof methodName === "string" && Boolean(window.ReactNativeWebView) && bridgeMethods.includes(methodName);
|
|
165
166
|
}
|
|
166
167
|
}
|
|
167
168
|
);
|
|
168
169
|
const loose = new Proxy(target, {
|
|
169
|
-
get: (target2,
|
|
170
|
-
if (
|
|
171
|
-
|
|
170
|
+
get: (target2, methodName) => {
|
|
171
|
+
if (methodName in target2 && !["isWebViewBridgeAvailable", "isNativeMethodAvailable"].includes(
|
|
172
|
+
methodName
|
|
172
173
|
)) {
|
|
173
|
-
return target2[
|
|
174
|
+
return target2[methodName];
|
|
174
175
|
}
|
|
175
176
|
return createNativeMethod(
|
|
176
|
-
|
|
177
|
+
methodName,
|
|
177
178
|
timeoutMs,
|
|
178
|
-
willMethodThrowOnError(
|
|
179
|
+
willMethodThrowOnError(methodName)
|
|
179
180
|
);
|
|
180
181
|
}
|
|
181
182
|
});
|
|
182
183
|
Object.assign(target, { loose });
|
|
183
|
-
|
|
184
|
-
get: (target2,
|
|
185
|
-
if (
|
|
186
|
-
return target2[
|
|
184
|
+
const proxy = new Proxy(target, {
|
|
185
|
+
get: (target2, methodName) => {
|
|
186
|
+
if (methodName in target2) {
|
|
187
|
+
return target2[methodName];
|
|
187
188
|
}
|
|
188
189
|
window.ReactNativeWebView?.postMessage(
|
|
189
190
|
JSON.stringify({
|
|
190
191
|
type: "fallback",
|
|
191
192
|
body: {
|
|
192
|
-
method
|
|
193
|
+
method: methodName
|
|
193
194
|
}
|
|
194
195
|
})
|
|
195
196
|
);
|
|
196
|
-
onFallback?.(
|
|
197
|
-
if (willMethodThrowOnError(
|
|
198
|
-
return () => Promise.reject(new MethodNotFoundError(
|
|
197
|
+
onFallback?.(methodName);
|
|
198
|
+
if (willMethodThrowOnError(methodName)) {
|
|
199
|
+
return () => Promise.reject(new MethodNotFoundError(methodName));
|
|
199
200
|
} else {
|
|
200
201
|
console.warn(
|
|
201
|
-
`[WebViewBridge] ${
|
|
202
|
+
`[WebViewBridge] ${methodName} is not defined, using fallback.`
|
|
202
203
|
);
|
|
203
204
|
}
|
|
204
205
|
return () => Promise.resolve();
|
|
205
206
|
}
|
|
206
207
|
});
|
|
208
|
+
onReady?.(proxy);
|
|
209
|
+
return proxy;
|
|
207
210
|
};
|
|
208
211
|
|
|
209
212
|
// src/registerWebMethod.ts
|
package/dist/module/index.mjs
CHANGED
|
@@ -28,10 +28,10 @@ var createEvents = () => ({
|
|
|
28
28
|
};
|
|
29
29
|
}
|
|
30
30
|
});
|
|
31
|
-
var createResolver = (emitter2,
|
|
31
|
+
var createResolver = (emitter2, methodName, eventId, evaluate, failHandler = false) => {
|
|
32
32
|
return new Promise((resolve, reject) => {
|
|
33
33
|
const unbind = emitter2.on(
|
|
34
|
-
`${
|
|
34
|
+
`${methodName}-${eventId}`,
|
|
35
35
|
(data, throwOccurred) => {
|
|
36
36
|
unbind();
|
|
37
37
|
if (throwOccurred) {
|
|
@@ -75,26 +75,26 @@ var timeout = (ms, throwOnError = true) => {
|
|
|
75
75
|
|
|
76
76
|
// src/linkNativeMethod.ts
|
|
77
77
|
var emitter = createEvents();
|
|
78
|
-
var createNativeMethod = (
|
|
78
|
+
var createNativeMethod = (methodName, timeoutMs, throwOnError) => (...args) => {
|
|
79
79
|
const eventId = createRandomId();
|
|
80
80
|
return Promise.race([
|
|
81
81
|
createResolver(
|
|
82
82
|
emitter,
|
|
83
|
-
|
|
83
|
+
methodName,
|
|
84
84
|
eventId,
|
|
85
85
|
() => {
|
|
86
86
|
window.ReactNativeWebView?.postMessage(
|
|
87
87
|
JSON.stringify({
|
|
88
88
|
type: "bridge",
|
|
89
89
|
body: {
|
|
90
|
-
method,
|
|
90
|
+
method: methodName,
|
|
91
91
|
eventId,
|
|
92
92
|
args
|
|
93
93
|
}
|
|
94
94
|
})
|
|
95
95
|
);
|
|
96
96
|
},
|
|
97
|
-
throwOnError && new NativeMethodError(
|
|
97
|
+
throwOnError && new NativeMethodError(methodName)
|
|
98
98
|
),
|
|
99
99
|
timeout(timeoutMs, throwOnError)
|
|
100
100
|
]);
|
|
@@ -106,7 +106,8 @@ var linkNativeMethod = (options = {
|
|
|
106
106
|
const {
|
|
107
107
|
timeout: timeoutMs = 2e3,
|
|
108
108
|
throwOnError = false,
|
|
109
|
-
onFallback
|
|
109
|
+
onFallback,
|
|
110
|
+
onReady
|
|
110
111
|
} = options;
|
|
111
112
|
if (!window.ReactNativeWebView) {
|
|
112
113
|
console.warn("[WebViewBridge] Not in a WebView environment");
|
|
@@ -119,62 +120,64 @@ var linkNativeMethod = (options = {
|
|
|
119
120
|
return throwOnError === true || Array.isArray(throwOnError) && throwOnError.includes(methodName);
|
|
120
121
|
};
|
|
121
122
|
const target = bridgeMethods.reduce(
|
|
122
|
-
(acc,
|
|
123
|
+
(acc, methodName) => {
|
|
123
124
|
return {
|
|
124
125
|
...acc,
|
|
125
|
-
[
|
|
126
|
-
|
|
126
|
+
[methodName]: createNativeMethod(
|
|
127
|
+
methodName,
|
|
127
128
|
timeoutMs,
|
|
128
|
-
willMethodThrowOnError(
|
|
129
|
+
willMethodThrowOnError(methodName)
|
|
129
130
|
)
|
|
130
131
|
};
|
|
131
132
|
},
|
|
132
133
|
{
|
|
133
134
|
isWebViewBridgeAvailable: Boolean(window.ReactNativeWebView) && bridgeMethods.length > 0,
|
|
134
|
-
isNativeMethodAvailable(
|
|
135
|
-
return typeof
|
|
135
|
+
isNativeMethodAvailable(methodName) {
|
|
136
|
+
return typeof methodName === "string" && Boolean(window.ReactNativeWebView) && bridgeMethods.includes(methodName);
|
|
136
137
|
}
|
|
137
138
|
}
|
|
138
139
|
);
|
|
139
140
|
const loose = new Proxy(target, {
|
|
140
|
-
get: (target2,
|
|
141
|
-
if (
|
|
142
|
-
|
|
141
|
+
get: (target2, methodName) => {
|
|
142
|
+
if (methodName in target2 && !["isWebViewBridgeAvailable", "isNativeMethodAvailable"].includes(
|
|
143
|
+
methodName
|
|
143
144
|
)) {
|
|
144
|
-
return target2[
|
|
145
|
+
return target2[methodName];
|
|
145
146
|
}
|
|
146
147
|
return createNativeMethod(
|
|
147
|
-
|
|
148
|
+
methodName,
|
|
148
149
|
timeoutMs,
|
|
149
|
-
willMethodThrowOnError(
|
|
150
|
+
willMethodThrowOnError(methodName)
|
|
150
151
|
);
|
|
151
152
|
}
|
|
152
153
|
});
|
|
153
154
|
Object.assign(target, { loose });
|
|
154
|
-
|
|
155
|
-
get: (target2,
|
|
156
|
-
if (
|
|
157
|
-
return target2[
|
|
155
|
+
const proxy = new Proxy(target, {
|
|
156
|
+
get: (target2, methodName) => {
|
|
157
|
+
if (methodName in target2) {
|
|
158
|
+
return target2[methodName];
|
|
158
159
|
}
|
|
159
160
|
window.ReactNativeWebView?.postMessage(
|
|
160
161
|
JSON.stringify({
|
|
161
162
|
type: "fallback",
|
|
162
163
|
body: {
|
|
163
|
-
method
|
|
164
|
+
method: methodName
|
|
164
165
|
}
|
|
165
166
|
})
|
|
166
167
|
);
|
|
167
|
-
onFallback?.(
|
|
168
|
-
if (willMethodThrowOnError(
|
|
169
|
-
return () => Promise.reject(new MethodNotFoundError(
|
|
168
|
+
onFallback?.(methodName);
|
|
169
|
+
if (willMethodThrowOnError(methodName)) {
|
|
170
|
+
return () => Promise.reject(new MethodNotFoundError(methodName));
|
|
170
171
|
} else {
|
|
171
172
|
console.warn(
|
|
172
|
-
`[WebViewBridge] ${
|
|
173
|
+
`[WebViewBridge] ${methodName} is not defined, using fallback.`
|
|
173
174
|
);
|
|
174
175
|
}
|
|
175
176
|
return () => Promise.resolve();
|
|
176
177
|
}
|
|
177
178
|
});
|
|
179
|
+
onReady?.(proxy);
|
|
180
|
+
return proxy;
|
|
178
181
|
};
|
|
179
182
|
|
|
180
183
|
// src/registerWebMethod.ts
|
|
@@ -2,6 +2,7 @@ import { Bridge, NativeMethod } from "./types";
|
|
|
2
2
|
export interface LinkNativeMethodOptions<BridgeObject extends Bridge> {
|
|
3
3
|
timeout?: number;
|
|
4
4
|
throwOnError?: boolean | (keyof BridgeObject)[] | string[];
|
|
5
|
-
onFallback?: (
|
|
5
|
+
onFallback?: (methodName: string) => void;
|
|
6
|
+
onReady?: (method: NativeMethod<BridgeObject>) => void;
|
|
6
7
|
}
|
|
7
8
|
export declare const linkNativeMethod: <BridgeObject extends Bridge>(options?: LinkNativeMethodOptions<BridgeObject>) => NativeMethod<BridgeObject>;
|
|
@@ -12,5 +12,5 @@ export interface EventEmitter<Events extends EventsMap = DefaultEvents> {
|
|
|
12
12
|
on<K extends keyof Events>(this: this, event: K, cb: Events[K]): () => void;
|
|
13
13
|
}
|
|
14
14
|
export declare const createEvents: <Events extends EventsMap = DefaultEvents>() => EventEmitter<Events>;
|
|
15
|
-
export declare const createResolver: (emitter: EventEmitter<DefaultEvents>,
|
|
15
|
+
export declare const createResolver: (emitter: EventEmitter<DefaultEvents>, methodName: string, eventId: string, evaluate: () => void, failHandler?: Error | false) => Promise<unknown>;
|
|
16
16
|
export {};
|