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

Commit 3ab1732

Browse files
committed
Code refactor
Correct flow type notations and remove unnecessary code.
1 parent 7c887ba commit 3ab1732

File tree

3 files changed

+10
-23
lines changed

3 files changed

+10
-23
lines changed

src/android.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const RNFetchBlob:RNFetchBlobNative = NativeModules.RNFetchBlob
1818
* @param {string} mime MIME type string
1919
* @return {Promise}
2020
*/
21-
function actionViewIntent(path:string, mime = 'text/plain':string) {
21+
function actionViewIntent(path:string, mime:string = 'text/plain') {
2222
if(Platform.OS === 'android')
2323
return RNFetchBlob.actionViewIntent(path, mime)
2424
else

src/class/RNFetchBlobReadStream.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ export default class RNFetchBlobReadStream {
3535
// register for file stream event
3636
let subscription = emitter.addListener(this.streamId, (e) => {
3737
let {event, detail} = e
38-
if(this._onData && event === 'data')
38+
if(this._onData && event === 'data') {
3939
this._onData(detail)
40+
return
41+
}
4042
else if (this._onEnd && event === 'end') {
4143
this._onEnd(detail)
4244
}
@@ -62,12 +64,7 @@ export default class RNFetchBlobReadStream {
6264
throw new Error('Stream closed')
6365
}
6466

65-
onData(fn) {
66-
// if(this.encoding.toLowerCase() === 'ascii')
67-
// this._onData = (data) => {
68-
// fn(data)
69-
// }
70-
// else
67+
onData(fn:() => void) {
7168
this._onData = fn
7269
}
7370

src/index.js

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -229,14 +229,14 @@ class FetchBlobResponse {
229229
path : () => string | null;
230230
type : 'base64' | 'path' | 'utf8';
231231
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>;
234234
json : () => any;
235235
base64 : () => any;
236236
flush : () => void;
237237
respInfo : RNFetchBlobResponseInfo;
238238
session : (name:string) => RNFetchBlobSession | null;
239-
readFile : (encode: 'base64' | 'utf8' | 'ascii') => ?Promise;
239+
readFile : (encode: 'base64' | 'utf8' | 'ascii') => ?Promise<any>;
240240
readStream : (
241241
encode: 'utf8' | 'ascii' | 'base64',
242242
) => RNFetchBlobStream | null;
@@ -275,18 +275,15 @@ class FetchBlobResponse {
275275
* Convert result to text.
276276
* @return {string} Decoded base64 string.
277277
*/
278-
this.text = ():string => {
278+
this.text = ():string | Promise<any> => {
279279
let res = this.data
280280
switch(this.type) {
281281
case 'base64':
282282
return base64.decode(this.data)
283-
break
284283
case 'path':
285284
return fs.readFile(this.data, 'base64').then((b64) => Promise.resolve(base64.decode(b64)))
286-
break
287285
default:
288286
return this.data
289-
break
290287
}
291288
}
292289
/**
@@ -297,31 +294,25 @@ class FetchBlobResponse {
297294
switch(this.type) {
298295
case 'base64':
299296
return JSON.parse(base64.decode(this.data))
300-
break
301297
case 'path':
302298
return fs.readFile(this.data, 'utf8')
303299
.then((text) => Promise.resolve(JSON.parse(text)))
304-
break
305300
default:
306301
return JSON.parse(this.data)
307-
break
308302
}
309303
}
310304
/**
311305
* Return BASE64 string directly.
312306
* @return {string} BASE64 string of response body.
313307
*/
314-
this.base64 = ():string => {
308+
this.base64 = ():string | Promise<any> => {
315309
switch(this.type) {
316310
case 'base64':
317311
return this.data
318-
break
319312
case 'path':
320313
return fs.readFile(this.data, 'base64')
321-
break
322314
default:
323315
return base64.encode(this.data)
324-
break
325316
}
326317
}
327318
/**
@@ -376,7 +367,6 @@ class FetchBlobResponse {
376367
this.readFile = (encode: 'base64' | 'utf8' | 'ascii') => {
377368
if(this.type === 'path') {
378369
encode = encode || 'utf8'
379-
380370
return readFile(this.data, encode)
381371
}
382372
else {

0 commit comments

Comments
 (0)