style: xml-http-request.ts

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2022-11-22 15:50:54 +08:00
parent ac16754c9c
commit 3f58f5992c

View File

@ -212,13 +212,13 @@ export class XMLHttpRequest {
} }
abort() { abort() {
this._connection.disconnect() this._connection.disconnect()
this.onabort && this.onabort() this.onabort?.()
} }
private _send(body?: string | object) { private _send(body?: string | object) {
try { try {
this._connection.connect() this._connection.connect()
this.onloadstart && this.onloadstart() this.onloadstart?.()
if (body) { if (body) {
let bodyType = Object.prototype.toString.call(body) let bodyType = Object.prototype.toString.call(body)
if (typeof body !== "string") { throw new Error(`body(${bodyType}) must be string!`) } if (typeof body !== "string") { throw new Error(`body(${bodyType}) must be string!`) }
@ -228,6 +228,7 @@ export class XMLHttpRequest {
out.close() out.close()
} }
this.setReadyState(ReadyState.LOADING) this.setReadyState(ReadyState.LOADING)
this.onload?.()
this._status = this._connection.getResponseCode() this._status = this._connection.getResponseCode()
this._statusText = this._connection.getResponseMessage() this._statusText = this._connection.getResponseMessage()
if (this._status >= 0 && this._status < 300) { if (this._status >= 0 && this._status < 300) {
@ -238,7 +239,7 @@ export class XMLHttpRequest {
this._responseText = this.readOutput(this._connection.getErrorStream()) this._responseText = this.readOutput(this._connection.getErrorStream())
} }
this.setResponseHeaders(this._connection.getHeaderFields()) this.setResponseHeaders(this._connection.getHeaderFields())
this.onloadend && this.onloadend() this.onloadend?.()
} catch (ex: any) { } catch (ex: any) {
if (ex instanceof SocketTimeoutException && this.ontimeout) { if (ex instanceof SocketTimeoutException && this.ontimeout) {
return this.ontimeout(ex) return this.ontimeout(ex)
@ -260,7 +261,7 @@ export class XMLHttpRequest {
private setReadyState(state: ReadyState) { private setReadyState(state: ReadyState) {
this._readyState = state this._readyState = state
this.onreadystatechange && this.onreadystatechange() this.onreadystatechange?.()
} }
private readOutput(input: any) { private readOutput(input: any) {