bug修复: 修复executeJs锁无效导致的并发回调丢失
This commit is contained in:
parent
c317d05e6e
commit
397e103596
|
|
@ -31,10 +31,12 @@ class MainActivity : AppCompatActivity() {
|
||||||
@Volatile
|
@Volatile
|
||||||
private var pendingJsCode: String? = null
|
private var pendingJsCode: String? = null
|
||||||
|
|
||||||
|
private val jsLock = Any()
|
||||||
|
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun executeJs(script: String) {
|
fun executeJs(script: String) {
|
||||||
Timber.tag("ExecuteJs").d(script)
|
Timber.tag("ExecuteJs").d(script)
|
||||||
synchronized(pendingJsCode ?: Any()) {
|
synchronized(jsLock) {
|
||||||
// 合并多次调用(用换行分隔)
|
// 合并多次调用(用换行分隔)
|
||||||
pendingJsCode = if (pendingJsCode != null) "$pendingJsCode;$script" else script
|
pendingJsCode = if (pendingJsCode != null) "$pendingJsCode;$script" else script
|
||||||
}
|
}
|
||||||
|
|
@ -119,7 +121,7 @@ class MainActivity : AppCompatActivity() {
|
||||||
// 获取并清除待执行的 JS 代码(由 bridge.js 轮询)
|
// 获取并清除待执行的 JS 代码(由 bridge.js 轮询)
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun pollPendingJs(): String {
|
fun pollPendingJs(): String {
|
||||||
synchronized(pendingJsCode ?: Any()) {
|
synchronized(jsLock) {
|
||||||
val code = pendingJsCode ?: ""
|
val code = pendingJsCode ?: ""
|
||||||
pendingJsCode = null
|
pendingJsCode = null
|
||||||
return code
|
return code
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue