Skip to content

Commit

Permalink
使axios支持设置timeout参数
Browse files Browse the repository at this point in the history
  • Loading branch information
aiselp committed Aug 14, 2023
1 parent 95bbc9e commit e6ecf16
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
let HttpUrl = Packages.okhttp3.HttpUrl;
let MediaType = Packages.okhttp3.MediaType;
let Headers = Packages.okhttp3.Headers;
let InterruptedIOException = java.io.InterruptedIOException

const stream = require("stream");
let EventTarget = require("./EventTarget.js");
Expand All @@ -24,6 +25,7 @@
///////XMLHttpRequest对象
let XMLHttpRequest = function () {
this.responseType = 'text';
this.timeout = 0;
setReadonlyAttribute(this, 'upload', {})
setReadonlyAttribute(this, 'readyState', 0);
setReadonlyAttribute(this, 'status', 0);
Expand Down Expand Up @@ -105,12 +107,7 @@
XMLHttpRequest.prototype.send = function (body) {
atl.addTask();
const xhr = this;
const {
url,
method,
ac,
headers,
} = this._requestData
const { url, method, ac, headers, } = this._requestData
const responseType = xhr.responseType;

const builder = new Request.Builder();
Expand All @@ -120,12 +117,20 @@
let reqBody = parserReqBody(xhr, body);

let request = builder.url(url.build()).method(method, reqBody).build();
let call = XMLHttpRequest._okHttpClient.newCall(request);
let client = XMLHttpRequest._okHttpClient;
if (xhr.timeout > 0) {
client = new OkHttpClient.Builder().callTimeout(xhr.timeout, java.util.concurrent.TimeUnit.MILLISECONDS)
.followRedirects(true).build();
}
let call = client.newCall(request);
setReadonlyAttribute(xhr, '_call', call, false);
call.enqueue({
onFailure(call, e) {
xhr._setReadyState(4);
setReadonlyAttribute(xhr, 'statusText', e.message)
if (e instanceof InterruptedIOException) {
xhr.dispatchEvent(new Event('timeout'));
}
xhr.dispatchEvent(new Event('error'))
xhr.dispatchEvent(new Event('loadend'))
atl.removeTask()
Expand Down
15 changes: 7 additions & 8 deletions autojs/src/main/assets/modules/npm/process.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
const {
EventEmitter
} = require("events");
const es = require("event-stream");

const { EventEmitter } = require("events");
// const es = require("event-stream");

const process = new EventEmitter();

//events.on('exit', process.emit.bind(process, 'exit'))

/*
process.stdout = es.map(function(data, callback) {
console.log(data);
return callback(null, data)
})
/*
process.stdout.end = function() {};
process.stdout.destroy = function(){};
*/
process.stderr = es.map(function(data, callback) {
console.error(data);
return callback(null, data)
})

*/
process.nextTick = setImmediate;
process.env = {}
//log(process);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import com.stardust.autojs.core.web.JsBridge

open class JsWebView : WebView {
//val events = EventEmitter()

@RequiresApi(Build.VERSION_CODES.M)
val jsBridge = JsBridge(this)

init {
Expand All @@ -21,7 +21,7 @@ open class JsWebView : WebView {
settings.javaScriptCanOpenWindowsAutomatically = true
settings.domStorageEnabled = true
settings.displayZoomControls = false
webViewClient = JsBridge.SuperWebViewClient()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) webViewClient = JsBridge.SuperWebViewClient()
}

constructor(context: Context) : super(context)
Expand Down
11 changes: 1 addition & 10 deletions autojs/src/main/java/com/stardust/autojs/core/web/JsBridge.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,20 @@ import org.mozilla.javascript.Undefined
import java.io.ByteArrayOutputStream
import kotlin.random.Random


@RequiresApi(Build.VERSION_CODES.M)
class JsBridge(private val webView: WebView) {
companion object {
const val WEBOBJECTNAME = "\$autox"
const val JAVABRIDGE = "AutoxJavaBridge"
const val sdkPath = "web/autox.sdk.v1.js"

@RequiresApi(Build.VERSION_CODES.M)
fun evaluateJavascript(js: String, webView: WebView) {
Looper.getMainLooper().queue.addIdleHandler {
webView.evaluateJavascript(js, null)
false
}
}

@RequiresApi(Build.VERSION_CODES.M)
fun injectionJsBridge(webView: WebView) {
val js: String = try {
val inputStream = webView.context.assets.open(sdkPath)
Expand Down Expand Up @@ -67,7 +65,6 @@ class JsBridge(private val webView: WebView) {
return this
}

@RequiresApi(Build.VERSION_CODES.M)
fun callHandler(event: String, data: String?, callBack: BaseFunction?) {
val fn = if (callBack is Handle) callBack else callBack?.let { Handle(it) }
val pos = Pos(getId(), event, data)
Expand All @@ -81,12 +78,10 @@ class JsBridge(private val webView: WebView) {
evaluateJavascript(js, this.webView)
}

@RequiresApi(Build.VERSION_CODES.M)
fun callHandler(event: String, data: String?) {
callHandler(event, data, null)
}

@RequiresApi(Build.VERSION_CODES.M)
fun callHandler(event: String) {
callHandler(event, null, null)
}
Expand Down Expand Up @@ -127,7 +122,6 @@ class JsBridge(private val webView: WebView) {
}
}

@RequiresApi(Build.VERSION_CODES.M)
fun invokeToMainThread(p1: String?, p2: Handle?) {
Looper.getMainLooper().queue.addIdleHandler {
invoke(p1, p2)
Expand Down Expand Up @@ -156,7 +150,6 @@ class JsBridge(private val webView: WebView) {
inner class JsObject {
private val callBackData = HashMap<Int, Any>()

@RequiresApi(Build.VERSION_CODES.M)
@JavascriptInterface
//web调用安卓
fun callHandle(reqData: String) {
Expand Down Expand Up @@ -184,7 +177,6 @@ class JsBridge(private val webView: WebView) {
return Gson().toJson(data)
}

@RequiresApi(Build.VERSION_CODES.M)
@JavascriptInterface
fun callBack(callBackId: Int, data: String?) {
val callBack = callHandlerData[callBackId]?.callBack
Expand Down Expand Up @@ -233,7 +225,6 @@ class JsBridge(private val webView: WebView) {
return super.shouldInterceptRequest(view, request)
}

@RequiresApi(Build.VERSION_CODES.M)
override fun onPageFinished(view: WebView?, url: String?) {
super.onPageFinished(view, url)
if (view != null) {
Expand Down

0 comments on commit e6ecf16

Please sign in to comment.