Browse Source

Make derived blob auto release

Ben Hsieh 8 years ago
parent
commit
49a0b8d134
2 changed files with 12 additions and 3 deletions
  1. 1
    0
      src/polyfill/Blob.js
  2. 11
    3
      src/polyfill/XMLHttpRequest.js

+ 1
- 0
src/polyfill/Blob.js View File

@@ -73,6 +73,7 @@ export default class Blob extends EventTarget {
73 73
     cType = cType || {}
74 74
     this.cacheName = getBlobName()
75 75
     this.isRNFetchBlobPolyfill = true
76
+    this.isDerived = defer
76 77
     this.type = cType.type || 'text/plain'
77 78
     log.verbose('Blob constructor called', 'mime', this.type, 'type', typeof data, 'length', data?  data.length:0)
78 79
     this._ref = blobCacheDir + this.cacheName

+ 11
- 3
src/polyfill/XMLHttpRequest.js View File

@@ -53,6 +53,7 @@ export default class XMLHttpRequest extends XMLHttpRequestEventTarget{
53 53
   _headers: any = {
54 54
     'Content-Type' : 'text/plain'
55 55
   };
56
+  _cleanUp : () => void = null;
56 57
   _body: any;
57 58
 
58 59
   // RNFetchBlob promise object, which has `progress`, `uploadProgress`, and
@@ -158,9 +159,16 @@ export default class XMLHttpRequest extends XMLHttpRequestEventTarget{
158 159
       log.debug('sending blob body', body._blobCreated)
159 160
       promise = new Promise((resolve, reject) => {
160 161
           body.onCreated((blob) => {
161
-              log.debug('body created send request')
162
-              body = RNFetchBlob.wrap(blob.getRNFetchBlobRef())
163
-              resolve()
162
+            // when the blob is derived (not created by RN developer), the blob
163
+            // will be released after XMLHttpRequest sent
164
+            if(blob.isDerived) {
165
+              this._cleanUp = () => {
166
+                blob.close()
167
+              }
168
+            }
169
+            log.debug('body created send request')
170
+            body = RNFetchBlob.wrap(blob.getRNFetchBlobRef())
171
+            resolve()
164 172
           })
165 173
         })
166 174
     }