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
     cType = cType || {}
73
     cType = cType || {}
74
     this.cacheName = getBlobName()
74
     this.cacheName = getBlobName()
75
     this.isRNFetchBlobPolyfill = true
75
     this.isRNFetchBlobPolyfill = true
76
+    this.isDerived = defer
76
     this.type = cType.type || 'text/plain'
77
     this.type = cType.type || 'text/plain'
77
     log.verbose('Blob constructor called', 'mime', this.type, 'type', typeof data, 'length', data?  data.length:0)
78
     log.verbose('Blob constructor called', 'mime', this.type, 'type', typeof data, 'length', data?  data.length:0)
78
     this._ref = blobCacheDir + this.cacheName
79
     this._ref = blobCacheDir + this.cacheName

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

53
   _headers: any = {
53
   _headers: any = {
54
     'Content-Type' : 'text/plain'
54
     'Content-Type' : 'text/plain'
55
   };
55
   };
56
+  _cleanUp : () => void = null;
56
   _body: any;
57
   _body: any;
57
 
58
 
58
   // RNFetchBlob promise object, which has `progress`, `uploadProgress`, and
59
   // RNFetchBlob promise object, which has `progress`, `uploadProgress`, and
158
       log.debug('sending blob body', body._blobCreated)
159
       log.debug('sending blob body', body._blobCreated)
159
       promise = new Promise((resolve, reject) => {
160
       promise = new Promise((resolve, reject) => {
160
           body.onCreated((blob) => {
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
     }