Browse Source

Code refactor

Correct flow type notations and remove unnecessary code.
Ben Hsieh 8 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
  * @param  {string} mime MIME type string
18
  * @param  {string} mime MIME type string
19
  * @return {Promise}
19
  * @return {Promise}
20
  */
20
  */
21
-function actionViewIntent(path:string, mime = 'text/plain':string) {
21
+function actionViewIntent(path:string, mime:string = 'text/plain') {
22
   if(Platform.OS === 'android')
22
   if(Platform.OS === 'android')
23
     return RNFetchBlob.actionViewIntent(path, mime)
23
     return RNFetchBlob.actionViewIntent(path, mime)
24
   else
24
   else

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

35
     // register for file stream event
35
     // register for file stream event
36
     let subscription = emitter.addListener(this.streamId, (e) => {
36
     let subscription = emitter.addListener(this.streamId, (e) => {
37
       let {event, detail} = e
37
       let {event, detail} = e
38
-      if(this._onData && event === 'data')
38
+      if(this._onData && event === 'data') {
39
         this._onData(detail)
39
         this._onData(detail)
40
+        return 
41
+      }
40
       else if (this._onEnd && event === 'end') {
42
       else if (this._onEnd && event === 'end') {
41
         this._onEnd(detail)
43
         this._onEnd(detail)
42
       }
44
       }
62
       throw new Error('Stream closed')
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
     this._onData = fn
68
     this._onData = fn
72
   }
69
   }
73
 
70
 

+ 5
- 15
src/index.js View File

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