Skip to content
This repository was archived by the owner on Mar 16, 2019. It is now read-only.

Commit 6353af7

Browse files
committed
Fix recursive Blob creation problem on Android
1 parent 3b6aadf commit 6353af7

File tree

2 files changed

+39
-24
lines changed

2 files changed

+39
-24
lines changed

src/polyfill/Blob.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,12 @@ export default class Blob extends EventTarget {
172172
* @return {Blob} The Blob object instance itself
173173
*/
174174
onCreated(fn:() => void):Blob {
175-
log.verbose('register blob onCreated', this._onCreated.length)
175+
log.verbose('#register blob onCreated', this._blobCreated)
176176
if(!this._blobCreated)
177177
this._onCreated.push(fn)
178-
else
178+
else {
179179
fn(this)
180+
}
180181
return this
181182
}
182183

@@ -230,12 +231,13 @@ export default class Blob extends EventTarget {
230231
}
231232

232233
_invokeOnCreateEvent() {
233-
log.verbose('invoke create event')
234+
log.verbose('invoke create event', this._onCreated)
234235
this._blobCreated = true
235236
let fns = this._onCreated
236237
for(let i in fns) {
237-
if(typeof fns[i] === 'function')
238+
if(typeof fns[i] === 'function') {
238239
fns[i](this)
240+
}
239241
}
240242
delete this._onCreated
241243
}
@@ -279,12 +281,13 @@ function createMixedBlobData(ref, dataArray) {
279281
// start write blob data
280282
// return p.then(() => {
281283
for(let i in args) {
282-
p = p.then((written) => {
284+
p = p.then(function(written){
285+
let arg = this
283286
if(written)
284287
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]))
288291
}
289292
return p.then(() => Promise.resolve(size))
290293
// let promises = args.map((p) => {

src/polyfill/XMLHttpRequest.js

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -133,34 +133,45 @@ export default class XMLHttpRequest extends XMLHttpRequestEventTarget{
133133

134134
if(this._readyState !== XMLHttpRequest.OPENED)
135135
throw 'InvalidStateError : XMLHttpRequest is not opened yet.'
136-
136+
let promise = Promise.resolve()
137137
this._sendFlag = true
138138
log.verbose('XMLHttpRequest send ', body)
139139
let {_method, _url, _headers } = this
140140
log.verbose('sending request with args', _method, _url, _headers, body)
141141
log.verbose(typeof body, body instanceof FormData)
142142

143143
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+
})
145150
}
146151
else if(typeof body === 'object') {
147152
body = JSON.stringify(body)
153+
promise = Promise.resolve()
148154
}
149-
else
155+
else {
150156
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+
})
164175
}
165176

166177
overrideMimeType(mime:string) {
@@ -311,6 +322,7 @@ export default class XMLHttpRequest extends XMLHttpRequestEventTarget{
311322
})
312323
break;
313324
default :
325+
console.log(resp, resp.text())
314326
this._responseText = resp.text()
315327
this._response = this.responseText
316328
responseDataReady()

0 commit comments

Comments
 (0)