浏览代码

Change XMLHttpRequest event trigger timing

Ben Hsieh 7 年前
父节点
当前提交
f19773f60a
共有 4 个文件被更改,包括 10 次插入6 次删除
  1. 2
    1
      src/polyfill/Blob.js
  2. 4
    2
      src/polyfill/File.js
  3. 3
    2
      src/polyfill/XMLHttpRequest.js
  4. 1
    1
      src/polyfill/index.js

+ 2
- 1
src/polyfill/Blob.js 查看文件

@@ -6,6 +6,7 @@ import RNFetchBlob from '../index.js'
6 6
 import fs from '../fs.js'
7 7
 import getUUID from '../utils/uuid'
8 8
 import Log from '../utils/log.js'
9
+import EventTarget from './EventTarget'
9 10
 
10 11
 const log = new Log('Blob')
11 12
 const blobCacheDir = fs.dirs.DocumentDir + '/RNFetchBlob-blobs/'
@@ -16,7 +17,7 @@ log.level(3)
16 17
  * A RNFetchBlob style Blob polyfill class, this is a Blob which compatible to
17 18
  * Response object attain fron RNFetchBlob.fetch.
18 19
  */
19
-export default class Blob {
20
+export default class Blob extends EventTarget {
20 21
 
21 22
   cacheName:string;
22 23
   type:string;

+ 4
- 2
src/polyfill/File.js 查看文件

@@ -7,8 +7,10 @@ import Blob from './Blob.js'
7 7
 
8 8
 export default class File extends Blob {
9 9
 
10
-  constructor() {
11
-    super()
10
+  name : string = '';
11
+
12
+  constructor(data:any , cType:string) {
13
+    super(data, cType)
12 14
   }
13 15
 
14 16
 }

+ 3
- 2
src/polyfill/XMLHttpRequest.js 查看文件

@@ -123,6 +123,8 @@ export default class XMLHttpRequest extends XMLHttpRequestEventTarget{
123 123
     else if(typeof body === 'object') {
124 124
       body = JSON.stringify(body)
125 125
     }
126
+    else
127
+      body = body.toString()
126 128
 
127 129
     this._task = RNFetchBlob
128 130
                   .config({ auto: true, timeout : this._timeout })
@@ -208,8 +210,6 @@ export default class XMLHttpRequest extends XMLHttpRequestEventTarget{
208 210
   _headerReceived(e) {
209 211
     log.verbose('header received ', this._task.taskId, e)
210 212
     this.responseURL = this._url
211
-    this.upload.dispatchEvent('loadend')
212
-    this.dispatchEvent('load')
213 213
     if(e.state === "2") {
214 214
       this._responseHeaders = e.headers
215 215
       this._statusText = e.status
@@ -282,6 +282,7 @@ export default class XMLHttpRequest extends XMLHttpRequestEventTarget{
282 282
         break;
283 283
       }
284 284
       this.dispatchEvent('loadend')
285
+      this.dispatchEvent('load')
285 286
       this._dispatchReadStateChange(XMLHttpRequest.DONE)
286 287
     }
287 288
     this.clearEventListeners()

+ 1
- 1
src/polyfill/index.js 查看文件

@@ -5,5 +5,5 @@ import FormData from './FormData.js'
5 5
 import ProgressEvent from './ProgressEvent'
6 6
 
7 7
 export default {
8
-  Blob, File, XMLHttpRequest, FormData, ProgressEvent
8
+  Blob, File, XMLHttpRequest, FormData, ProgressEvent, Event
9 9
 }