Repository navigation
Expand file tree
/
Copy pathBridge.js
More file actions
144 lines (121 loc) · 4.54 KB
/
Copy pathBridge.js
File metadata and controls
144 lines (121 loc) · 4.54 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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
(function() {
if (window.JavaScriptBridge) {
return;
}
var sendMessageQueue = [],
responseCallbacks = {},
errorCallbacks = {},
messagingFrame,
msgCnt = 1;
function handleNativeMessage(message) {
dispatchNativeMessage(message);
}
function dispatchNativeMessage(messageJSON) {
setTimeout(function _timeout() {
const message = JSON.parse(jsonEscape(messageJSON));
var responseCallback, errorCallback;
// Check if some type of callback was specified
if (message.callbackId) {
// If there was an exception there will be data in message.errorData
// We either have a callback for resolve or reject
// Either way, we need to delete the appropriate callbacks
if (message.errorData) {
// We have an Exception, delete the resolve callback and invoke the reject
responseCallback = responseCallbacks[message.callbackId];
if (responseCallback) {
delete responseCallbacks[message.callbackId];
}
errorCallback = errorCallbacks[message.callbackId];
if (errorCallback) {
errorCallback(message.errorData);
delete errorCallbacks[message.callbackId];
}
} else {
errorCallback = errorCallbacks[message.callbackId];
if (errorCallback) {
delete errorCallbacks[message.callbackId];
}
responseCallback = responseCallbacks[message.callbackId];
if (responseCallback) {
responseCallback(message.responseData);
delete responseCallbacks[message.callbackId];
}
}
}
});
}
function fetchQueue() {
const queue = sendMessageQueue;
sendMessageQueue = [];
return queue;
}
function send(data, onSuccess, onFailure) {
callNative(null, data, onSuccess, onFailure);
}
function callNative(handler, data, onSuccess, onFailure) {
const func = this.callNative;
data = data || {};
if (typeof data === "function") {
// No data specified. Need to shift params over by one
onFailure = onSuccess;
onSuccess = data;
data = null;
}
if (onSuccess === undefined && onFailure === undefined) {
return new Promise(function(resolve, reject) {
func(handler,
data,
function(result) {
resolve(result);
},
function(err) {
reject(err);
});
});
}
return sendImpl(
{ handler: handler, handlerdata: data },
onSuccess,
onFailure);
}
function sendImpl(message, onSuccess, onFailure) {
if (onSuccess !== undefined || onFailure !== undefined) {
const cbid = `cb_${msgCnt++}_${new Date().getTime()}`;
message["callbackId"] = cbid;
if (onSuccess) {
responseCallbacks[cbid] = onSuccess;
}
if (onFailure) {
errorCallbacks[cbid] = onFailure;
}
}
sendMessageQueue.push(message);
const notifyMessage = "jsbridge://queue_message";
if (messagingFrame) {
messagingFrame.contentWindow.postMessage(notifyMessage, "*");
} else {
window.external.notify(notifyMessage);
}
}
function jsonEscape(input) {
return input
.replace(/\n/g, "\\\\n")
.replace(/\r/g, "\\\\r")
.replace(/\t/g, "\\\\t");
}
window.JavaScriptBridge = {
send: send,
callNative: callNative,
fetchQueue: fetchQueue,
handleNativeMessage: handleNativeMessage
};
// iframe to handle window.external.notify
// TODO: Detect if this is necessary for your scenario
//messagingFrame = document.createElement('iframe');
//messagingFrame.style.display = 'none';
//messagingFrame.src = "/Bridge.html";
//document.documentElement.appendChild(messagingFrame);
const readyEvent = new CustomEvent("JavaScriptBridgeReady");
readyEvent.bridge = JavaScriptBridge;
document.dispatchEvent(readyEvent);
})();