Commit d8200ebf by huangzhicong

修复storage接口回调没有执行在主线程的问题

parent ccd0ad5f
......@@ -12,6 +12,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
class StorageContract(fragment: Fragment, proxy: WebViewProxy) : IContract(fragment, proxy){
companion object {
......@@ -32,15 +33,19 @@ class StorageContract(fragment: Fragment, proxy: WebViewProxy) : IContract(fragm
val storageKey = stringPreferencesKey(key)
maxrocky[storageKey] = value
val data = StorageResult(key, value)
withContext(Dispatchers.Main) {
proxy.success(STORAGE_METHOD, storageResult, data)
storageResult.reset()
}
}
} catch (e: Exception) {
withContext(Dispatchers.Main) {
proxy.failed(STORAGE_METHOD, storageResult, e.message ?: "")
storageResult.reset()
}
}
}
}
fun acquireValue(sessionId: Int, key: String) {
acquireResult.setupSessionId(sessionId)
......@@ -48,15 +53,19 @@ class StorageContract(fragment: Fragment, proxy: WebViewProxy) : IContract(fragm
try {
context.dataStore.data.map { it[stringPreferencesKey(key)] ?: "" }.collect { storageValue ->
val data = StorageResult(key, storageValue)
withContext(Dispatchers.Main) {
proxy.success(ACQUIRE_METHOD, acquireResult, data)
acquireResult.reset()
}
}
} catch (e: Exception) {
withContext(Dispatchers.Main) {
proxy.failed(ACQUIRE_METHOD, acquireResult, e.message ?: "")
acquireResult.reset()
}
}
}
}
fun clearValue(sessionId: Int, key: String) {
clearResult.setupSessionId(sessionId)
......@@ -69,13 +78,17 @@ class StorageContract(fragment: Fragment, proxy: WebViewProxy) : IContract(fragm
}
val storageValue = maxrocky.remove(storageKey)
val data = StorageResult(key, storageValue)
withContext(Dispatchers.Main) {
proxy.success(CLEAR_METHOD, clearResult, data)
clearResult.reset()
}
}
} catch (e: Exception) {
withContext(Dispatchers.Main) {
proxy.failed(CLEAR_METHOD, clearResult, e.message ?: "")
clearResult.reset()
}
}
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment