@webview-bridge/web 1.0.6 → 1.1.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
|
@@ -90,16 +90,44 @@ var createRandomId = (size = ID_LENGTH) => {
|
|
|
90
90
|
};
|
|
91
91
|
|
|
92
92
|
// ../../shared/util/src/timeout.ts
|
|
93
|
-
var timeout = (ms) => {
|
|
94
|
-
return new Promise((
|
|
93
|
+
var timeout = (ms, throwOnError = true) => {
|
|
94
|
+
return new Promise((resolve, reject) => {
|
|
95
95
|
setTimeout(() => {
|
|
96
|
-
|
|
96
|
+
if (throwOnError) {
|
|
97
|
+
reject(new Error("Timeout"));
|
|
98
|
+
} else {
|
|
99
|
+
resolve(void 0);
|
|
100
|
+
}
|
|
97
101
|
}, ms);
|
|
98
102
|
});
|
|
99
103
|
};
|
|
100
104
|
|
|
101
105
|
// src/linkNativeMethod.ts
|
|
102
106
|
var emitter = createEvents();
|
|
107
|
+
var createNativeMethod = (method, timeoutMs, throwOnError) => (...args) => {
|
|
108
|
+
const eventId = createRandomId();
|
|
109
|
+
return Promise.race([
|
|
110
|
+
createResolver(
|
|
111
|
+
emitter,
|
|
112
|
+
method,
|
|
113
|
+
eventId,
|
|
114
|
+
() => {
|
|
115
|
+
window.ReactNativeWebView?.postMessage(
|
|
116
|
+
JSON.stringify({
|
|
117
|
+
type: "bridge",
|
|
118
|
+
body: {
|
|
119
|
+
method,
|
|
120
|
+
eventId,
|
|
121
|
+
args
|
|
122
|
+
}
|
|
123
|
+
})
|
|
124
|
+
);
|
|
125
|
+
},
|
|
126
|
+
throwOnError && new NativeMethodError(method)
|
|
127
|
+
),
|
|
128
|
+
timeout(timeoutMs, throwOnError)
|
|
129
|
+
]);
|
|
130
|
+
};
|
|
103
131
|
var linkNativeMethod = (options = {
|
|
104
132
|
timeout: 2e3,
|
|
105
133
|
throwOnError: false
|
|
@@ -116,37 +144,18 @@ var linkNativeMethod = (options = {
|
|
|
116
144
|
if (!window.nativeEmitter) {
|
|
117
145
|
window.nativeEmitter = emitter;
|
|
118
146
|
}
|
|
119
|
-
const
|
|
147
|
+
const willMethodThrowOnError = (methodName) => {
|
|
120
148
|
return throwOnError === true || Array.isArray(throwOnError) && throwOnError.includes(methodName);
|
|
121
149
|
};
|
|
122
150
|
const target = bridgeMethods.reduce(
|
|
123
151
|
(acc, method) => {
|
|
124
152
|
return {
|
|
125
153
|
...acc,
|
|
126
|
-
[method]: (
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
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
|
-
),
|
|
147
|
-
timeout(timeoutMs)
|
|
148
|
-
]);
|
|
149
|
-
}
|
|
154
|
+
[method]: createNativeMethod(
|
|
155
|
+
method,
|
|
156
|
+
timeoutMs,
|
|
157
|
+
willMethodThrowOnError(method)
|
|
158
|
+
)
|
|
150
159
|
};
|
|
151
160
|
},
|
|
152
161
|
{
|
|
@@ -156,6 +165,21 @@ var linkNativeMethod = (options = {
|
|
|
156
165
|
}
|
|
157
166
|
}
|
|
158
167
|
);
|
|
168
|
+
const loose = new Proxy(target, {
|
|
169
|
+
get: (target2, method) => {
|
|
170
|
+
if (method in target2 && !["isWebViewBridgeAvailable", "isNativeMethodAvailable"].includes(
|
|
171
|
+
method
|
|
172
|
+
)) {
|
|
173
|
+
return target2[method];
|
|
174
|
+
}
|
|
175
|
+
return createNativeMethod(
|
|
176
|
+
method,
|
|
177
|
+
timeoutMs,
|
|
178
|
+
willMethodThrowOnError(method)
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
Object.assign(target, { loose });
|
|
159
183
|
return new Proxy(target, {
|
|
160
184
|
get: (target2, method) => {
|
|
161
185
|
if (method in target2) {
|
|
@@ -170,7 +194,7 @@ var linkNativeMethod = (options = {
|
|
|
170
194
|
})
|
|
171
195
|
);
|
|
172
196
|
onFallback?.(method);
|
|
173
|
-
if (
|
|
197
|
+
if (willMethodThrowOnError(method)) {
|
|
174
198
|
return () => Promise.reject(new MethodNotFoundError(method));
|
|
175
199
|
} else {
|
|
176
200
|
console.warn(
|
package/dist/module/index.mjs
CHANGED
|
@@ -61,16 +61,44 @@ var createRandomId = (size = ID_LENGTH) => {
|
|
|
61
61
|
};
|
|
62
62
|
|
|
63
63
|
// ../../shared/util/src/timeout.ts
|
|
64
|
-
var timeout = (ms) => {
|
|
65
|
-
return new Promise((
|
|
64
|
+
var timeout = (ms, throwOnError = true) => {
|
|
65
|
+
return new Promise((resolve, reject) => {
|
|
66
66
|
setTimeout(() => {
|
|
67
|
-
|
|
67
|
+
if (throwOnError) {
|
|
68
|
+
reject(new Error("Timeout"));
|
|
69
|
+
} else {
|
|
70
|
+
resolve(void 0);
|
|
71
|
+
}
|
|
68
72
|
}, ms);
|
|
69
73
|
});
|
|
70
74
|
};
|
|
71
75
|
|
|
72
76
|
// src/linkNativeMethod.ts
|
|
73
77
|
var emitter = createEvents();
|
|
78
|
+
var createNativeMethod = (method, timeoutMs, throwOnError) => (...args) => {
|
|
79
|
+
const eventId = createRandomId();
|
|
80
|
+
return Promise.race([
|
|
81
|
+
createResolver(
|
|
82
|
+
emitter,
|
|
83
|
+
method,
|
|
84
|
+
eventId,
|
|
85
|
+
() => {
|
|
86
|
+
window.ReactNativeWebView?.postMessage(
|
|
87
|
+
JSON.stringify({
|
|
88
|
+
type: "bridge",
|
|
89
|
+
body: {
|
|
90
|
+
method,
|
|
91
|
+
eventId,
|
|
92
|
+
args
|
|
93
|
+
}
|
|
94
|
+
})
|
|
95
|
+
);
|
|
96
|
+
},
|
|
97
|
+
throwOnError && new NativeMethodError(method)
|
|
98
|
+
),
|
|
99
|
+
timeout(timeoutMs, throwOnError)
|
|
100
|
+
]);
|
|
101
|
+
};
|
|
74
102
|
var linkNativeMethod = (options = {
|
|
75
103
|
timeout: 2e3,
|
|
76
104
|
throwOnError: false
|
|
@@ -87,37 +115,18 @@ var linkNativeMethod = (options = {
|
|
|
87
115
|
if (!window.nativeEmitter) {
|
|
88
116
|
window.nativeEmitter = emitter;
|
|
89
117
|
}
|
|
90
|
-
const
|
|
118
|
+
const willMethodThrowOnError = (methodName) => {
|
|
91
119
|
return throwOnError === true || Array.isArray(throwOnError) && throwOnError.includes(methodName);
|
|
92
120
|
};
|
|
93
121
|
const target = bridgeMethods.reduce(
|
|
94
122
|
(acc, method) => {
|
|
95
123
|
return {
|
|
96
124
|
...acc,
|
|
97
|
-
[method]: (
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
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
|
-
),
|
|
118
|
-
timeout(timeoutMs)
|
|
119
|
-
]);
|
|
120
|
-
}
|
|
125
|
+
[method]: createNativeMethod(
|
|
126
|
+
method,
|
|
127
|
+
timeoutMs,
|
|
128
|
+
willMethodThrowOnError(method)
|
|
129
|
+
)
|
|
121
130
|
};
|
|
122
131
|
},
|
|
123
132
|
{
|
|
@@ -127,6 +136,21 @@ var linkNativeMethod = (options = {
|
|
|
127
136
|
}
|
|
128
137
|
}
|
|
129
138
|
);
|
|
139
|
+
const loose = new Proxy(target, {
|
|
140
|
+
get: (target2, method) => {
|
|
141
|
+
if (method in target2 && !["isWebViewBridgeAvailable", "isNativeMethodAvailable"].includes(
|
|
142
|
+
method
|
|
143
|
+
)) {
|
|
144
|
+
return target2[method];
|
|
145
|
+
}
|
|
146
|
+
return createNativeMethod(
|
|
147
|
+
method,
|
|
148
|
+
timeoutMs,
|
|
149
|
+
willMethodThrowOnError(method)
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
Object.assign(target, { loose });
|
|
130
154
|
return new Proxy(target, {
|
|
131
155
|
get: (target2, method) => {
|
|
132
156
|
if (method in target2) {
|
|
@@ -141,7 +165,7 @@ var linkNativeMethod = (options = {
|
|
|
141
165
|
})
|
|
142
166
|
);
|
|
143
167
|
onFallback?.(method);
|
|
144
|
-
if (
|
|
168
|
+
if (willMethodThrowOnError(method)) {
|
|
145
169
|
return () => Promise.reject(new MethodNotFoundError(method));
|
|
146
170
|
} else {
|
|
147
171
|
console.warn(
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { Bridge,
|
|
1
|
+
import { Bridge, NativeMethod } from "./types";
|
|
2
2
|
export interface LinkNativeMethodOptions<BridgeObject extends Bridge> {
|
|
3
3
|
timeout?: number;
|
|
4
|
-
throwOnError?: boolean | (keyof BridgeObject)[];
|
|
5
|
-
onFallback?: (method:
|
|
4
|
+
throwOnError?: boolean | (keyof BridgeObject)[] | string[];
|
|
5
|
+
onFallback?: (method: string) => void;
|
|
6
6
|
}
|
|
7
|
-
export declare const linkNativeMethod: <BridgeObject extends Bridge>(options?: LinkNativeMethodOptions<BridgeObject>) =>
|
|
7
|
+
export declare const linkNativeMethod: <BridgeObject extends Bridge>(options?: LinkNativeMethodOptions<BridgeObject>) => NativeMethod<BridgeObject>;
|
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
export type AsyncFunction = (...args: any[]) => Promise<any>;
|
|
2
2
|
export type Bridge = Record<string, AsyncFunction>;
|
|
3
|
-
export type
|
|
3
|
+
export type NativeMethod<T> = {
|
|
4
4
|
isWebViewBridgeAvailable: boolean;
|
|
5
5
|
isNativeMethodAvailable(method: keyof T): boolean;
|
|
6
|
+
isNativeMethodAvailable(method: string): boolean;
|
|
7
|
+
loose: {
|
|
8
|
+
[K in keyof T]: (...args: any[]) => any;
|
|
9
|
+
} & {
|
|
10
|
+
[key: string]: (...args: any[]) => any;
|
|
11
|
+
};
|
|
6
12
|
} & T;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const timeout: (ms: number) => Promise<unknown>;
|
|
1
|
+
export declare const timeout: (ms: number, throwOnError?: boolean) => Promise<unknown>;
|