Browse Source

Fix recursive Blob creation problem on Android

Ben Hsieh 8 years ago
parent
commit
5caa270ee5
2 changed files with 39 additions and 24 deletions
  1. 11
    8
      src/polyfill/Blob.js
  2. 28
    16
      src/polyfill/XMLHttpRequest.js

+ 11
- 8
src/polyfill/Blob.js View File

172
    * @return {Blob} The Blob object instance itself
172
    * @return {Blob} The Blob object instance itself
173
    */
173
    */
174
   onCreated(fn:() => void):Blob {
174
   onCreated(fn:() => void):Blob {
175
-    log.verbose('register blob onCreated', this._onCreated.length)
175
+    log.verbose('#register blob onCreated', this._blobCreated)
176
     if(!this._blobCreated)
176
     if(!this._blobCreated)
177
       this._onCreated.push(fn)
177
       this._onCreated.push(fn)
178
-    else
178
+    else {
179
       fn(this)
179
       fn(this)
180
+    }
180
     return this
181
     return this
181
   }
182
   }
182
 
183
 
230
   }
231
   }
231
 
232
 
232
   _invokeOnCreateEvent() {
233
   _invokeOnCreateEvent() {
233
-    log.verbose('invoke create event')
234
+    log.verbose('invoke create event', this._onCreated)
234
     this._blobCreated = true
235
     this._blobCreated = true
235
     let fns = this._onCreated
236
     let fns = this._onCreated
236
     for(let i in fns) {
237
     for(let i in fns) {
237
-      if(typeof fns[i] === 'function')
238
+      if(typeof fns[i] === 'function') {
238
         fns[i](this)
239
         fns[i](this)
240
+      }
239
     }
241
     }
240
     delete this._onCreated
242
     delete this._onCreated
241
   }
243
   }
279
   // start write blob data
281
   // start write blob data
280
   // return p.then(() => {
282
   // return p.then(() => {
281
     for(let i in args) {
283
     for(let i in args) {
282
-      p = p.then((written) => {
284
+      p = p.then(function(written){
285
+        let arg = this
283
         if(written)
286
         if(written)
284
           size += written
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
     return p.then(() => Promise.resolve(size))
292
     return p.then(() => Promise.resolve(size))
290
     // let promises = args.map((p) => {
293
     // let promises = args.map((p) => {

+ 28
- 16
src/polyfill/XMLHttpRequest.js View File

133
 
133
 
134
     if(this._readyState !== XMLHttpRequest.OPENED)
134
     if(this._readyState !== XMLHttpRequest.OPENED)
135
       throw 'InvalidStateError : XMLHttpRequest is not opened yet.'
135
       throw 'InvalidStateError : XMLHttpRequest is not opened yet.'
136
-
136
+    let promise = Promise.resolve()
137
     this._sendFlag = true
137
     this._sendFlag = true
138
     log.verbose('XMLHttpRequest send ', body)
138
     log.verbose('XMLHttpRequest send ', body)
139
     let {_method, _url, _headers } = this
139
     let {_method, _url, _headers } = this
141
     log.verbose(typeof body, body instanceof FormData)
141
     log.verbose(typeof body, body instanceof FormData)
142
 
142
 
143
     if(body instanceof Blob) {
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
     else if(typeof body === 'object') {
151
     else if(typeof body === 'object') {
147
       body = JSON.stringify(body)
152
       body = JSON.stringify(body)
153
+      promise = Promise.resolve()
148
     }
154
     }
149
-    else
155
+    else {
150
       body = body ? body.toString() : body
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
   overrideMimeType(mime:string) {
177
   overrideMimeType(mime:string) {
311
           })
322
           })
312
         break;
323
         break;
313
         default :
324
         default :
325
+        console.log(resp, resp.text())
314
           this._responseText = resp.text()
326
           this._responseText = resp.text()
315
           this._response = this.responseText
327
           this._response = this.responseText
316
           responseDataReady()
328
           responseDataReady()