Преглед изворни кода

Fix recursive Blob creation problem on Android

Ben Hsieh пре 8 година
родитељ
комит
5caa270ee5
2 измењених фајлова са 39 додато и 24 уклоњено
  1. 11
    8
      src/polyfill/Blob.js
  2. 28
    16
      src/polyfill/XMLHttpRequest.js

+ 11
- 8
src/polyfill/Blob.js Прегледај датотеку

@@ -172,11 +172,12 @@ export default class Blob extends EventTarget {
172 172
    * @return {Blob} The Blob object instance itself
173 173
    */
174 174
   onCreated(fn:() => void):Blob {
175
-    log.verbose('register blob onCreated', this._onCreated.length)
175
+    log.verbose('#register blob onCreated', this._blobCreated)
176 176
     if(!this._blobCreated)
177 177
       this._onCreated.push(fn)
178
-    else
178
+    else {
179 179
       fn(this)
180
+    }
180 181
     return this
181 182
   }
182 183
 
@@ -230,12 +231,13 @@ export default class Blob extends EventTarget {
230 231
   }
231 232
 
232 233
   _invokeOnCreateEvent() {
233
-    log.verbose('invoke create event')
234
+    log.verbose('invoke create event', this._onCreated)
234 235
     this._blobCreated = true
235 236
     let fns = this._onCreated
236 237
     for(let i in fns) {
237
-      if(typeof fns[i] === 'function')
238
+      if(typeof fns[i] === 'function') {
238 239
         fns[i](this)
240
+      }
239 241
     }
240 242
     delete this._onCreated
241 243
   }
@@ -279,12 +281,13 @@ function createMixedBlobData(ref, dataArray) {
279 281
   // start write blob data
280 282
   // return p.then(() => {
281 283
     for(let i in args) {
282
-      p = p.then((written) => {
284
+      p = p.then(function(written){
285
+        let arg = this
283 286
         if(written)
284 287
           size += written
285
-        log.verbose('mixed blob write', ...args[i], written)
286
-        return fs.appendFile.call(this, ...args[i])
287
-      })
288
+        log.verbose('mixed blob write', args[i], written)
289
+        return fs.appendFile(...arg)
290
+      }.bind(args[i]))
288 291
     }
289 292
     return p.then(() => Promise.resolve(size))
290 293
     // let promises = args.map((p) => {

+ 28
- 16
src/polyfill/XMLHttpRequest.js Прегледај датотеку

@@ -133,7 +133,7 @@ export default class XMLHttpRequest extends XMLHttpRequestEventTarget{
133 133
 
134 134
     if(this._readyState !== XMLHttpRequest.OPENED)
135 135
       throw 'InvalidStateError : XMLHttpRequest is not opened yet.'
136
-
136
+    let promise = Promise.resolve()
137 137
     this._sendFlag = true
138 138
     log.verbose('XMLHttpRequest send ', body)
139 139
     let {_method, _url, _headers } = this
@@ -141,26 +141,37 @@ export default class XMLHttpRequest extends XMLHttpRequestEventTarget{
141 141
     log.verbose(typeof body, body instanceof FormData)
142 142
 
143 143
     if(body instanceof Blob) {
144
-      body = RNFetchBlob.wrap(body.getRNFetchBlobRef())
144
+      promise = new Promise((resolve, reject) => {
145
+          body.onCreated((blob) => {
146
+            body = RNFetchBlob.wrap(body.getRNFetchBlobRef())
147
+            resolve()
148
+          })
149
+        })
145 150
     }
146 151
     else if(typeof body === 'object') {
147 152
       body = JSON.stringify(body)
153
+      promise = Promise.resolve()
148 154
     }
149
-    else
155
+    else {
150 156
       body = body ? body.toString() : body
151
-    this._task = RNFetchBlob
152
-                  .config({
153
-                    auto: true,
154
-                    timeout : this._timeout,
155
-                    binaryContentTypes : XMLHttpRequest.binaryContentTypes
156
-                  })
157
-                  .fetch(_method, _url, _headers, body)
158
-    this._task
159
-        .stateChange(this._headerReceived.bind(this))
160
-        .uploadProgress(this._uploadProgressEvent.bind(this))
161
-        .progress(this._progressEvent.bind(this))
162
-        .catch(this._onError.bind(this))
163
-        .then(this._onDone.bind(this))
157
+      promise = Promise.resolve()
158
+    }
159
+
160
+    promise.then(() => {
161
+      this._task = RNFetchBlob
162
+                    .config({
163
+                      auto: true,
164
+                      timeout : this._timeout,
165
+                      binaryContentTypes : XMLHttpRequest.binaryContentTypes
166
+                    })
167
+                    .fetch(_method, _url, _headers, body)
168
+      this._task
169
+          .stateChange(this._headerReceived.bind(this))
170
+          .uploadProgress(this._uploadProgressEvent.bind(this))
171
+          .progress(this._progressEvent.bind(this))
172
+          .catch(this._onError.bind(this))
173
+          .then(this._onDone.bind(this))
174
+    })
164 175
   }
165 176
 
166 177
   overrideMimeType(mime:string) {
@@ -311,6 +322,7 @@ export default class XMLHttpRequest extends XMLHttpRequestEventTarget{
311 322
           })
312 323
         break;
313 324
         default :
325
+        console.log(resp, resp.text())
314 326
           this._responseText = resp.text()
315 327
           this._response = this.responseText
316 328
           responseDataReady()