-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWebViewBridge.js
More file actions
45 lines (38 loc) · 1.23 KB
/
WebViewBridge.js
File metadata and controls
45 lines (38 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/* Usage examples:
import DeviceInfo from "react-native-device-info";
WebViewBridge.addSyncFunction("getDeviceId", DeviceInfo.getUniqueID);
const customAsyncFunction = (arg1, arg2, callback) => {
callback(null, "success");
});
WebViewBridge.addAsyncFunction("myFunctionName", asyncFunction);
*/
export default {
// Prepare an empty functions object
functions: {},
// Accepts a name and a function which accepts two arguments:
// an array of args and callback with signature (err, res)
addFunction(name, normalizedFunc) {
if (this.functions[name]) {
console.warn(`Overwriting WebViewBridge function ${name}!`);
}
this.functions[name] = normalizedFunc;
},
// Accepts an asynchronous function which accepts any number of
// arguments, followed by a callback with signature (err, res)
addAsyncFunction(name, func) {
this.addFunction(name, function(args, callback) {
func(...args, callback);
});
},
// Accepts a synchronous function which accepts any number of
// arguments and returns any value
addSyncFunction(name, func) {
this.addFunction(name, function(args, callback) {
try {
callback(null, func(...args));
} catch (err) {
callback(err);
}
});
}
};