@webview-bridge/web 1.0.5 → 1.0.6
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
|
@@ -21,6 +21,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
21
21
|
var src_exports = {};
|
|
22
22
|
__export(src_exports, {
|
|
23
23
|
MethodNotFoundError: () => MethodNotFoundError,
|
|
24
|
+
NativeMethodError: () => NativeMethodError,
|
|
24
25
|
linkNativeMethod: () => linkNativeMethod,
|
|
25
26
|
registerWebMethod: () => registerWebMethod
|
|
26
27
|
});
|
|
@@ -33,6 +34,12 @@ var MethodNotFoundError = class extends Error {
|
|
|
33
34
|
this.name = "MethodNotFoundError";
|
|
34
35
|
}
|
|
35
36
|
};
|
|
37
|
+
var NativeMethodError = class extends Error {
|
|
38
|
+
constructor(methodName) {
|
|
39
|
+
super(`An error occurred in the native bridge: ${methodName}`);
|
|
40
|
+
this.name = "NativeMethodError";
|
|
41
|
+
}
|
|
42
|
+
};
|
|
36
43
|
|
|
37
44
|
// ../../shared/util/src/createEvents.ts
|
|
38
45
|
var createEvents = () => ({
|
|
@@ -50,12 +57,23 @@ var createEvents = () => ({
|
|
|
50
57
|
};
|
|
51
58
|
}
|
|
52
59
|
});
|
|
53
|
-
var createResolver = (emitter2, method, eventId, evaluate) => {
|
|
54
|
-
return new Promise((resolve) => {
|
|
55
|
-
const unbind = emitter2.on(
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
60
|
+
var createResolver = (emitter2, method, eventId, evaluate, failHandler = false) => {
|
|
61
|
+
return new Promise((resolve, reject) => {
|
|
62
|
+
const unbind = emitter2.on(
|
|
63
|
+
`${method}-${eventId}`,
|
|
64
|
+
(data, throwOccurred) => {
|
|
65
|
+
unbind();
|
|
66
|
+
if (throwOccurred) {
|
|
67
|
+
if (failHandler instanceof Error) {
|
|
68
|
+
reject(failHandler);
|
|
69
|
+
} else {
|
|
70
|
+
resolve(void 0);
|
|
71
|
+
}
|
|
72
|
+
} else {
|
|
73
|
+
resolve(data);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
);
|
|
59
77
|
evaluate();
|
|
60
78
|
});
|
|
61
79
|
};
|
|
@@ -98,6 +116,9 @@ var linkNativeMethod = (options = {
|
|
|
98
116
|
if (!window.nativeEmitter) {
|
|
99
117
|
window.nativeEmitter = emitter;
|
|
100
118
|
}
|
|
119
|
+
const isMethodAvailable = (methodName) => {
|
|
120
|
+
return throwOnError === true || Array.isArray(throwOnError) && throwOnError.includes(methodName);
|
|
121
|
+
};
|
|
101
122
|
const target = bridgeMethods.reduce(
|
|
102
123
|
(acc, method) => {
|
|
103
124
|
return {
|
|
@@ -105,18 +126,24 @@ var linkNativeMethod = (options = {
|
|
|
105
126
|
[method]: (...args) => {
|
|
106
127
|
const eventId = createRandomId();
|
|
107
128
|
return Promise.race([
|
|
108
|
-
createResolver(
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
129
|
+
createResolver(
|
|
130
|
+
emitter,
|
|
131
|
+
method,
|
|
132
|
+
eventId,
|
|
133
|
+
() => {
|
|
134
|
+
window.ReactNativeWebView?.postMessage(
|
|
135
|
+
JSON.stringify({
|
|
136
|
+
type: "bridge",
|
|
137
|
+
body: {
|
|
138
|
+
method,
|
|
139
|
+
eventId,
|
|
140
|
+
args
|
|
141
|
+
}
|
|
142
|
+
})
|
|
143
|
+
);
|
|
144
|
+
},
|
|
145
|
+
isMethodAvailable(method) && new NativeMethodError(method)
|
|
146
|
+
),
|
|
120
147
|
timeout(timeoutMs)
|
|
121
148
|
]);
|
|
122
149
|
}
|
|
@@ -124,7 +151,9 @@ var linkNativeMethod = (options = {
|
|
|
124
151
|
},
|
|
125
152
|
{
|
|
126
153
|
isWebViewBridgeAvailable: Boolean(window.ReactNativeWebView) && bridgeMethods.length > 0,
|
|
127
|
-
isNativeMethodAvailable
|
|
154
|
+
isNativeMethodAvailable(method) {
|
|
155
|
+
return typeof method === "string" && Boolean(window.ReactNativeWebView) && bridgeMethods.includes(method);
|
|
156
|
+
}
|
|
128
157
|
}
|
|
129
158
|
);
|
|
130
159
|
return new Proxy(target, {
|
|
@@ -141,9 +170,7 @@ var linkNativeMethod = (options = {
|
|
|
141
170
|
})
|
|
142
171
|
);
|
|
143
172
|
onFallback?.(method);
|
|
144
|
-
if (
|
|
145
|
-
return () => Promise.reject(new MethodNotFoundError(method));
|
|
146
|
-
} else if (Array.isArray(throwOnError) && throwOnError.includes(method)) {
|
|
173
|
+
if (isMethodAvailable(method)) {
|
|
147
174
|
return () => Promise.reject(new MethodNotFoundError(method));
|
|
148
175
|
} else {
|
|
149
176
|
console.warn(
|
|
@@ -196,6 +223,7 @@ var registerWebMethod = (bridge) => {
|
|
|
196
223
|
// Annotate the CommonJS export names for ESM import in node:
|
|
197
224
|
0 && (module.exports = {
|
|
198
225
|
MethodNotFoundError,
|
|
226
|
+
NativeMethodError,
|
|
199
227
|
linkNativeMethod,
|
|
200
228
|
registerWebMethod
|
|
201
229
|
});
|
package/dist/module/index.mjs
CHANGED
|
@@ -5,6 +5,12 @@ var MethodNotFoundError = class extends Error {
|
|
|
5
5
|
this.name = "MethodNotFoundError";
|
|
6
6
|
}
|
|
7
7
|
};
|
|
8
|
+
var NativeMethodError = class extends Error {
|
|
9
|
+
constructor(methodName) {
|
|
10
|
+
super(`An error occurred in the native bridge: ${methodName}`);
|
|
11
|
+
this.name = "NativeMethodError";
|
|
12
|
+
}
|
|
13
|
+
};
|
|
8
14
|
|
|
9
15
|
// ../../shared/util/src/createEvents.ts
|
|
10
16
|
var createEvents = () => ({
|
|
@@ -22,12 +28,23 @@ var createEvents = () => ({
|
|
|
22
28
|
};
|
|
23
29
|
}
|
|
24
30
|
});
|
|
25
|
-
var createResolver = (emitter2, method, eventId, evaluate) => {
|
|
26
|
-
return new Promise((resolve) => {
|
|
27
|
-
const unbind = emitter2.on(
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
+
var createResolver = (emitter2, method, eventId, evaluate, failHandler = false) => {
|
|
32
|
+
return new Promise((resolve, reject) => {
|
|
33
|
+
const unbind = emitter2.on(
|
|
34
|
+
`${method}-${eventId}`,
|
|
35
|
+
(data, throwOccurred) => {
|
|
36
|
+
unbind();
|
|
37
|
+
if (throwOccurred) {
|
|
38
|
+
if (failHandler instanceof Error) {
|
|
39
|
+
reject(failHandler);
|
|
40
|
+
} else {
|
|
41
|
+
resolve(void 0);
|
|
42
|
+
}
|
|
43
|
+
} else {
|
|
44
|
+
resolve(data);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
);
|
|
31
48
|
evaluate();
|
|
32
49
|
});
|
|
33
50
|
};
|
|
@@ -70,6 +87,9 @@ var linkNativeMethod = (options = {
|
|
|
70
87
|
if (!window.nativeEmitter) {
|
|
71
88
|
window.nativeEmitter = emitter;
|
|
72
89
|
}
|
|
90
|
+
const isMethodAvailable = (methodName) => {
|
|
91
|
+
return throwOnError === true || Array.isArray(throwOnError) && throwOnError.includes(methodName);
|
|
92
|
+
};
|
|
73
93
|
const target = bridgeMethods.reduce(
|
|
74
94
|
(acc, method) => {
|
|
75
95
|
return {
|
|
@@ -77,18 +97,24 @@ var linkNativeMethod = (options = {
|
|
|
77
97
|
[method]: (...args) => {
|
|
78
98
|
const eventId = createRandomId();
|
|
79
99
|
return Promise.race([
|
|
80
|
-
createResolver(
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
100
|
+
createResolver(
|
|
101
|
+
emitter,
|
|
102
|
+
method,
|
|
103
|
+
eventId,
|
|
104
|
+
() => {
|
|
105
|
+
window.ReactNativeWebView?.postMessage(
|
|
106
|
+
JSON.stringify({
|
|
107
|
+
type: "bridge",
|
|
108
|
+
body: {
|
|
109
|
+
method,
|
|
110
|
+
eventId,
|
|
111
|
+
args
|
|
112
|
+
}
|
|
113
|
+
})
|
|
114
|
+
);
|
|
115
|
+
},
|
|
116
|
+
isMethodAvailable(method) && new NativeMethodError(method)
|
|
117
|
+
),
|
|
92
118
|
timeout(timeoutMs)
|
|
93
119
|
]);
|
|
94
120
|
}
|
|
@@ -96,7 +122,9 @@ var linkNativeMethod = (options = {
|
|
|
96
122
|
},
|
|
97
123
|
{
|
|
98
124
|
isWebViewBridgeAvailable: Boolean(window.ReactNativeWebView) && bridgeMethods.length > 0,
|
|
99
|
-
isNativeMethodAvailable
|
|
125
|
+
isNativeMethodAvailable(method) {
|
|
126
|
+
return typeof method === "string" && Boolean(window.ReactNativeWebView) && bridgeMethods.includes(method);
|
|
127
|
+
}
|
|
100
128
|
}
|
|
101
129
|
);
|
|
102
130
|
return new Proxy(target, {
|
|
@@ -113,9 +141,7 @@ var linkNativeMethod = (options = {
|
|
|
113
141
|
})
|
|
114
142
|
);
|
|
115
143
|
onFallback?.(method);
|
|
116
|
-
if (
|
|
117
|
-
return () => Promise.reject(new MethodNotFoundError(method));
|
|
118
|
-
} else if (Array.isArray(throwOnError) && throwOnError.includes(method)) {
|
|
144
|
+
if (isMethodAvailable(method)) {
|
|
119
145
|
return () => Promise.reject(new MethodNotFoundError(method));
|
|
120
146
|
} else {
|
|
121
147
|
console.warn(
|
|
@@ -167,6 +193,7 @@ var registerWebMethod = (bridge) => {
|
|
|
167
193
|
};
|
|
168
194
|
export {
|
|
169
195
|
MethodNotFoundError,
|
|
196
|
+
NativeMethodError,
|
|
170
197
|
linkNativeMethod,
|
|
171
198
|
registerWebMethod
|
|
172
199
|
};
|
|
@@ -2,5 +2,5 @@ export type AsyncFunction = (...args: any[]) => Promise<any>;
|
|
|
2
2
|
export type Bridge = Record<string, AsyncFunction>;
|
|
3
3
|
export type WithAvailable<T> = {
|
|
4
4
|
isWebViewBridgeAvailable: boolean;
|
|
5
|
-
isNativeMethodAvailable
|
|
5
|
+
isNativeMethodAvailable(method: keyof T): boolean;
|
|
6
6
|
} & T;
|
|
@@ -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>, method: string, eventId: string, evaluate: () => void) => Promise<unknown>;
|
|
15
|
+
export declare const createResolver: (emitter: EventEmitter<DefaultEvents>, method: string, eventId: string, evaluate: () => void, failHandler?: Error | false) => Promise<unknown>;
|
|
16
16
|
export {};
|