feat: 完善小程序适配 location -> $location XHRHttpReauest -> $XHRHttpRequest

This commit is contained in:
2912401452 2021-11-11 20:09:01 +08:00
parent 968604064c
commit 99f2848faa
4 changed files with 50 additions and 50 deletions

View File

@ -23,7 +23,7 @@ function _changeReadyState(readyState, event = { readyState }) {
_triggerEvent.call(this, 'readystatechange', event);
}
export class XMLHttpRequest extends EventTarget {
export class $XMLHttpRequest extends EventTarget {
public static UNSEND: number;
public static OPENED: number;
public static HEADERS_RECEIVED: number;
@ -107,7 +107,7 @@ export class XMLHttpRequest extends EventTarget {
public open(method, url /* GET/POST*/, flag) {
this._method = method;
this._url = url;
_changeReadyState.call(this, XMLHttpRequest.OPENED);
// _changeReadyState.call(this, XMLHttpRequest.OPENED);
}
public overrideMimeType() {}
@ -130,45 +130,45 @@ export class XMLHttpRequest extends EventTarget {
this.response = null;
const onSuccess = ({ data, status, headers }) => {
status = status === undefined ? 200 : status;
// status = status === undefined ? 200 : status;
try {
if (
data == null ||
(data instanceof ArrayBuffer && data.byteLength == 0)
) {
status = 404;
}
} catch (e) {}
// try {
// if (
// data == null ||
// (data instanceof ArrayBuffer && data.byteLength == 0)
// ) {
// status = 404;
// }
// } catch (e) {}
this.status = status;
if (headers) {
_responseHeader.set('responseHeader', headers);
}
_triggerEvent.call(this, 'loadstart');
_changeReadyState.call(this, XMLHttpRequest.HEADERS_RECEIVED);
_changeReadyState.call(this, XMLHttpRequest.LOADING);
// this.status = status;
// if (headers) {
// _responseHeader.set('responseHeader', headers);
// }
// _triggerEvent.call(this, 'loadstart');
// // _changeReadyState.call(this, XMLHttpRequest.HEADERS_RECEIVED);
// // _changeReadyState.call(this, XMLHttpRequest.LOADING);
this.response = data;
// this.response = data;
if (data instanceof ArrayBuffer) {
// TODO temporary solution, fix native gc error.
this.response = data.slice(0);
Object.defineProperty(this, 'responseText', {
enumerable: true,
configurable: true,
get() {
throw new Error(
'InvalidStateError : responseType is ' + this._responseType,
);
},
});
} else {
this.responseText = data;
}
_changeReadyState.call(this, XMLHttpRequest.DONE);
_triggerEvent.call(this, 'load');
_triggerEvent.call(this, 'loadend');
// if (data instanceof ArrayBuffer) {
// // TODO temporary solution, fix native gc error.
// this.response = data.slice(0);
// Object.defineProperty(this, 'responseText', {
// enumerable: true,
// configurable: true,
// get() {
// throw new Error(
// 'InvalidStateError : responseType is ' + this._responseType,
// );
// },
// });
// } else {
// this.responseText = data;
// }
// _changeReadyState.call(this, XMLHttpRequest.DONE);
// _triggerEvent.call(this, 'load');
// _triggerEvent.call(this, 'loadend');
};
const onFail = (e) => {
@ -229,8 +229,8 @@ export class XMLHttpRequest extends EventTarget {
}
// TODO 没法模拟 HEADERS_RECEIVED 和 LOADING 两个状态
XMLHttpRequest.UNSEND = 0;
XMLHttpRequest.OPENED = 1;
XMLHttpRequest.HEADERS_RECEIVED = 2;
XMLHttpRequest.LOADING = 3;
XMLHttpRequest.DONE = 4;
$XMLHttpRequest.UNSEND = 0;
$XMLHttpRequest.OPENED = 1;
$XMLHttpRequest.HEADERS_RECEIVED = 2;
$XMLHttpRequest.LOADING = 3;
$XMLHttpRequest.DONE = 4;

View File

@ -4,7 +4,7 @@ import { Event } from './Event';
import { HTMLElement } from './HTMLElement';
import { HTMLVideoElement } from './HTMLVideoElement';
import { Image } from './Image';
import { location } from './location';
import { $location } from './location';
class Body extends HTMLElement {
constructor() {
@ -55,7 +55,7 @@ export const $document = {
scripts: [],
style: {},
location: location,
location: $location,
ontouchstart: null,
ontouchmove: null,

View File

@ -13,7 +13,7 @@ import { HTMLMediaElement } from './HTMLMediaElement';
import { HTMLVideoElement } from './HTMLVideoElement';
import { Image } from './Image';
import { ImageData } from './ImageData';
import { location } from './location';
import { $location } from './location';
import { navigator } from './navigator';
import { Node } from './Node';
import { performance } from './performance';
@ -25,7 +25,7 @@ import { screen } from './screen';
import { URL } from './url';
import { WebGLRenderingContext } from './WebGL';
import { WebGL2RenderingContext } from './WebGL2';
import { XMLHttpRequest } from './XMLHttpRequest';
import { $XMLHttpRequest } from './XMLHttpRequest';
import { globalWindow } from './global'
@ -58,7 +58,7 @@ export const miniWindow = {
requestAnimationFrame,
cancelAnimationFrame,
screen,
XMLHttpRequest,
XMLHttpRequest: $XMLHttpRequest,
performance,
URL,
WebGLRenderingContext,
@ -81,8 +81,8 @@ export const miniWindow = {
} as Window & typeof globalThis;
export const $window = isMini ? miniWindow : globalWindow;
export const $XMLHttpRequest = isMini ? XMLHttpRequest: globalWindow.XMLHttpRequest;
export const $location = isMini ? location : globalWindow.location;
export const $XMLHttpRequest = isMini ? $XMLHttpRequest: globalWindow.XMLHttpRequest;
export const $location = isMini ? $location : globalWindow.location;
// TODO:
// export { registerCanvas, registerCanvas2D } from './register';

View File

@ -1,6 +1,6 @@
// @ts-nocheck
// tslint:disable
export let location = {
export let $location = {
href: '',
protocol: '',
host:''