Skip to content

Commit

Permalink
Merge pull request #180 from Anshiii/master
Browse files Browse the repository at this point in the history
fix: 修复连续多次调用 callHandler 部分回调函数未被调用的问题
  • Loading branch information
uknownothingsnow authored Nov 5, 2021
2 parents ce4d01c + 11999d5 commit d5c4a16
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions library/src/main/assets/WebViewJavascriptBridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,22 @@
var responseCallbacks = {};
var uniqueId = 1;

var lastCallTime = 0;
var stoId = null;
var FETCH_QUEUE_INTERVAL = 20;

// 创建消息index队列iframe
function _createQueueReadyIframe(doc) {
messagingIframe = doc.createElement('iframe');
messagingIframe.style.display = 'none';
doc.documentElement.appendChild(messagingIframe);
}
//创建消息体队列iframe
function _createQueueReadyIframe4biz(doc) {
bizMessagingIframe = doc.createElement('iframe');
bizMessagingIframe.style.display = 'none';
doc.documentElement.appendChild(bizMessagingIframe);
}
//set default messageHandler 初始化默认的消息线程
function init(messageHandler) {
if (WebViewJavascriptBridge._messageHandler) {
Expand Down Expand Up @@ -52,6 +68,7 @@
} else if (responseCallback) {
callbackId = 'cb_' + (uniqueId++) + '_' + new Date().getTime();
responseCallbacks[callbackId] = responseCallback;
message.callbackId = callbackId;
}else{
callbackId = '';
}
Expand All @@ -72,6 +89,32 @@
delete responseCallbacks[callbackId];
}
}

sendMessageQueue.push(message);
messagingIframe.src = CUSTOM_PROTOCOL_SCHEME + '://' + QUEUE_HAS_MESSAGE;
}

// 提供给native调用,该函数作用:获取sendMessageQueue返回给native,由于android不能直接获取返回的内容,所以使用url shouldOverrideUrlLoading 的方式返回内容
function _fetchQueue() {
// 空数组直接返回
if (sendMessageQueue.length === 0) {
return;
}

// _fetchQueue 的调用间隔过短,延迟调用
if (new Date().getTime() - lastCallTime < FETCH_QUEUE_INTERVAL) {
if (!stoId) {
stoId = setTimeout(_fetchQueue, FETCH_QUEUE_INTERVAL);
}
return;
}

lastCallTime = new Date().getTime();
stoId = null;
var messageQueueString = JSON.stringify(sendMessageQueue);
sendMessageQueue = [];
//android can't read directly the return data, so we can reload iframe src to communicate with java
bizMessagingIframe.src = CUSTOM_PROTOCOL_SCHEME + '://return/_fetchQueue/' + encodeURIComponent(messageQueueString);
}

//提供给native使用,
Expand Down

0 comments on commit d5c4a16

Please sign in to comment.