-
Notifications
You must be signed in to change notification settings - Fork 1
/
ReactNativeBridge.js
99 lines (85 loc) · 3.04 KB
/
ReactNativeBridge.js
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
export default function() {
console.log("Injected ReactNativeBridge");
// Find the native side of the bridge and store it in _ReactNativeBridge
if (typeof _ReactNativeBridge === "undefined") {
if (typeof __REACT_WEB_VIEW_BRIDGE === "undefined") {
if (
window.webkit &&
window.webkit.messageHandlers &&
window.webkit.messageHandlers.ReactNative
) {
_ReactNativeBridge = window.webkit.messageHandlers.ReactNative;
} else {
console.warn("ReactNativeBridge not found!");
}
} else {
_ReactNativeBridge = __REACT_WEB_VIEW_BRIDGE;
}
}
// Usage: ReactNativeBridge.call('myFunctionName', arg1, arg2, function(err, res) { console.log(err, res) });
ReactNativeBridge = {
counter: 0,
callbacks: {},
// Allows calling a native function with any number of arguments,
// optionally accepts a callback function OR an options object,
// which may contain: callback function, keepCallback bool, throwError bool
call(functionName /* .. [arguments] .. (callback|options) */) {
const args = Array.prototype.slice.call(arguments, 1);
// Set defaults for the options
let options = {
keepCallback: false,
throwError: false
};
// If more than one arguments were passed, check whether
// we received a callback function or options object
if (args.length > 1) {
const lastArgument = args[args.length - 1];
if (typeof lastArgument === "function") {
options.callback = args.pop();
} else if (typeof lastArgument === "object") {
options = args.pop();
callback = options.callback;
}
}
// TODO: consider implementing a different type of ID generation
const id = this.counter;
// Store the callback under the counter
if (callback) {
this.callbacks[id] = options;
}
// Create string (we cannot pass objects to native easily)
const message = JSON.stringify({
id,
functionName,
args
});
// Send a message to the native side of things
_ReactNativeBridge.postMessage(message);
// Increment the counter
this.counter++;
},
// Will receive the response from the WebView and pass it to the respective callback
response(message) {
const { id, err, res } = message,
{ callback, keepCallback, throwError } = this.callbacks[id] || {};
// Delete the callback, if we didn't request keeping it around
if (!keepCallback) {
delete this.callbacks[id];
}
// If we want to throw errors
if (throwError) {
// And received an error, then throw it
if (err) {
throw new Error(err);
}
// Otherwise callback with just the result
else {
return typeof callback === "function" ? callback(res) : false;
}
// Otherwise callback with error and response regardless
} else {
return typeof callback === "function" ? callback(err, res) : false;
}
}
};
}