Browse Source

Code refactor

Correct flow type notations and remove unnecessary code.
Ben Hsieh 7 years ago
parent
commit
3ab17328f6
3 changed files with 10 additions and 23 deletions
  1. 1
    1
      src/android.js
  2. 4
    7
      src/class/RNFetchBlobReadStream.js
  3. 5
    15
      src/index.js

+ 1
- 1
src/android.js View File

@@ -18,7 +18,7 @@ const RNFetchBlob:RNFetchBlobNative = NativeModules.RNFetchBlob
18 18
  * @param  {string} mime MIME type string
19 19
  * @return {Promise}
20 20
  */
21
-function actionViewIntent(path:string, mime = 'text/plain':string) {
21
+function actionViewIntent(path:string, mime:string = 'text/plain') {
22 22
   if(Platform.OS === 'android')
23 23
     return RNFetchBlob.actionViewIntent(path, mime)
24 24
   else

+ 4
- 7
src/class/RNFetchBlobReadStream.js View File

@@ -35,8 +35,10 @@ export default class RNFetchBlobReadStream {
35 35
     // register for file stream event
36 36
     let subscription = emitter.addListener(this.streamId, (e) => {
37 37
       let {event, detail} = e
38
-      if(this._onData && event === 'data')
38
+      if(this._onData && event === 'data') {
39 39
         this._onData(detail)
40
+        return 
41
+      }
40 42
       else if (this._onEnd && event === 'end') {
41 43
         this._onEnd(detail)
42 44
       }
@@ -62,12 +64,7 @@ export default class RNFetchBlobReadStream {
62 64
       throw new Error('Stream closed')
63 65
   }
64 66
 
65
-  onData(fn) {
66
-    // if(this.encoding.toLowerCase() === 'ascii')
67
-    //   this._onData = (data) => {
68
-    //     fn(data)
69
-    //   }
70
-    // else
67
+  onData(fn:() => void) {
71 68
     this._onData = fn
72 69
   }
73 70
 

+ 5
- 15
src/index.js View File

@@ -229,14 +229,14 @@ class FetchBlobResponse {
229 229
   path : () => string | null;
230 230
   type : 'base64' | 'path' | 'utf8';
231 231
   data : any;
232
-  blob : (contentType:string, sliceSize:number) => null;
233
-  text : () => string;
232
+  blob : (contentType:string, sliceSize:number) => Promise<Blob>;
233
+  text : () => string | Promise<any>;
234 234
   json : () => any;
235 235
   base64 : () => any;
236 236
   flush : () => void;
237 237
   respInfo : RNFetchBlobResponseInfo;
238 238
   session : (name:string) => RNFetchBlobSession | null;
239
-  readFile : (encode: 'base64' | 'utf8' | 'ascii') => ?Promise;
239
+  readFile : (encode: 'base64' | 'utf8' | 'ascii') => ?Promise<any>;
240 240
   readStream : (
241 241
     encode: 'utf8' | 'ascii' | 'base64',
242 242
   ) => RNFetchBlobStream | null;
@@ -275,18 +275,15 @@ class FetchBlobResponse {
275 275
      * Convert result to text.
276 276
      * @return {string} Decoded base64 string.
277 277
      */
278
-    this.text = ():string => {
278
+    this.text = ():string | Promise<any> => {
279 279
       let res = this.data
280 280
       switch(this.type) {
281 281
         case 'base64':
282 282
           return base64.decode(this.data)
283
-        break
284 283
         case 'path':
285 284
           return fs.readFile(this.data, 'base64').then((b64) => Promise.resolve(base64.decode(b64)))
286
-        break
287 285
         default:
288 286
           return this.data
289
-        break
290 287
       }
291 288
     }
292 289
     /**
@@ -297,31 +294,25 @@ class FetchBlobResponse {
297 294
       switch(this.type) {
298 295
         case 'base64':
299 296
           return JSON.parse(base64.decode(this.data))
300
-        break
301 297
         case 'path':
302 298
           return fs.readFile(this.data, 'utf8')
303 299
                    .then((text) => Promise.resolve(JSON.parse(text)))
304
-        break
305 300
         default:
306 301
           return JSON.parse(this.data)
307
-        break
308 302
       }
309 303
     }
310 304
     /**
311 305
      * Return BASE64 string directly.
312 306
      * @return {string} BASE64 string of response body.
313 307
      */
314
-    this.base64 = ():string => {
308
+    this.base64 = ():string | Promise<any> => {
315 309
       switch(this.type) {
316 310
         case 'base64':
317 311
           return this.data
318
-        break
319 312
         case 'path':
320 313
           return fs.readFile(this.data, 'base64')
321
-        break
322 314
         default:
323 315
           return base64.encode(this.data)
324
-        break
325 316
       }
326 317
     }
327 318
     /**
@@ -376,7 +367,6 @@ class FetchBlobResponse {
376 367
     this.readFile = (encode: 'base64' | 'utf8' | 'ascii') => {
377 368
       if(this.type === 'path') {
378 369
         encode = encode || 'utf8'
379
-
380 370
         return readFile(this.data, encode)
381 371
       }
382 372
       else {