This repository was archived by the owner on Mar 16, 2019. It is now read-only.
File tree Expand file tree Collapse file tree 7 files changed +387
-273
lines changed Expand file tree Collapse file tree 7 files changed +387
-273
lines changed Original file line number Diff line number Diff line change
1
+ import {
2
+ NativeModules ,
3
+ DeviceEventEmitter ,
4
+ NativeAppEventEmitter ,
5
+ } from 'react-native'
6
+
7
+ const RNFetchBlob = NativeModules . RNFetchBlob
8
+ const emitter = DeviceEventEmitter
9
+
10
+ export default class RNFetchBlobFile {
11
+
12
+ }
Original file line number Diff line number Diff line change
1
+ import {
2
+ NativeModules ,
3
+ DeviceEventEmitter ,
4
+ NativeAppEventEmitter ,
5
+ } from 'react-native'
6
+
7
+ const RNFetchBlob = NativeModules . RNFetchBlob
8
+ const emitter = DeviceEventEmitter
9
+
10
+ export default class RNFetchBlobReadStream {
11
+
12
+ path : string ;
13
+ encoding : 'utf8' | 'ascii' | 'base64' ;
14
+ bufferSize : ?number ;
15
+ closed : boolean ;
16
+
17
+ constructor ( path :string , encoding :string , bufferSize ?:?number ) {
18
+ if ( ! path )
19
+ throw Error ( 'RNFetchBlob could not open file stream with empty `path`' )
20
+ this . encoding = encoding || 'utf8'
21
+ this . bufferSize = bufferSize
22
+ this . path = path
23
+ this . closed = false
24
+ this . _onData = ( ) => { }
25
+ this . _onEnd = ( ) => { }
26
+ this . _onError = ( ) => { }
27
+
28
+ // register for file stream event
29
+ let subscription = emitter . addListener ( `RNFetchBlobStream+${ this . path } ` , ( e ) => {
30
+
31
+ let { event, detail} = e
32
+ if ( this . _onData && event === 'data' )
33
+ this . _onData ( detail )
34
+ else if ( this . _onEnd && event === 'end' ) {
35
+ this . _onEnd ( detail )
36
+ }
37
+ else {
38
+ if ( this . _onError )
39
+ this . _onError ( detail )
40
+ else
41
+ throw new Error ( detail )
42
+ }
43
+ // when stream closed or error, remove event handler
44
+ if ( event === 'error' || event === 'end' ) {
45
+ subscription . remove ( )
46
+ this . closed = true
47
+ }
48
+ } )
49
+
50
+ }
51
+
52
+ open ( ) {
53
+ if ( ! this . closed )
54
+ RNFetchBlob . readStream ( this . path , this . encoding , this . bufferSize || 0 )
55
+ else
56
+ throw new Error ( 'Stream closed' )
57
+ }
58
+
59
+ onData ( fn ) {
60
+ if ( this . encoding . toLowerCase ( ) === 'ascii' )
61
+ this . _onData = ( data ) => {
62
+ fn ( JSON . parse ( data ) )
63
+ }
64
+ else
65
+ this . _onData = fn
66
+ }
67
+
68
+ onError ( fn ) {
69
+ this . _onError = fn
70
+ }
71
+
72
+ onEnd ( fn ) {
73
+ this . _onEnd = fn
74
+ }
75
+
76
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Session class
3
+ * @class RNFetchBlobSession
4
+ */
5
+
6
+ import {
7
+ NativeModules ,
8
+ DeviceEventEmitter ,
9
+ NativeAppEventEmitter ,
10
+ } from 'react-native'
11
+
12
+ const RNFetchBlob = NativeModules . RNFetchBlob
13
+ const emitter = DeviceEventEmitter
14
+
15
+ export default class RNFetchBlobSession {
16
+
17
+ add : ( path :string ) => RNFetchBlobSession ;
18
+ remove : ( path :string ) => RNFetchBlobSession ;
19
+ dispose : ( ) => Promise ;
20
+ list : ( ) => Array < string > ;
21
+ name : string ;
22
+
23
+ constructor ( name :string , list :Array < string > ) {
24
+ this . name = name
25
+ if ( ! sessions [ name ] ) {
26
+ if ( Array . isArray ( list ) )
27
+ sessions [ name ] = list
28
+ else
29
+ sessions [ name ] = [ ]
30
+ }
31
+ }
32
+
33
+ add ( path :string ) :RNFetchBlobSession {
34
+ sessions [ this . name ] . push ( path )
35
+ return this
36
+ }
37
+
38
+ remove ( path :string ) :RNFetchBlobSession {
39
+ let list = sessions [ this . name ]
40
+ for ( let i in list ) {
41
+ if ( list [ i ] === path ) {
42
+ sessions [ this . name ] . splice ( i , 1 )
43
+ break ;
44
+ }
45
+ }
46
+ return this
47
+ }
48
+
49
+ list ( ) :Array < string > {
50
+ return sessions [ this . name ]
51
+ }
52
+
53
+ dispose ( ) :Promise {
54
+ return new Promise ( ( resolve , reject ) => {
55
+ RNFetchBlob . removeSession ( sessions [ this . name ] , ( err ) => {
56
+ if ( err )
57
+ reject ( err )
58
+ else {
59
+ delete sessions [ this . name ]
60
+ resolve ( )
61
+ }
62
+ } )
63
+ } )
64
+ }
65
+
66
+ }
Original file line number Diff line number Diff line change
1
+ import {
2
+ NativeModules ,
3
+ DeviceEventEmitter ,
4
+ NativeAppEventEmitter ,
5
+ } from 'react-native'
6
+
7
+ const RNFetchBlob = NativeModules . RNFetchBlob
8
+ const emitter = DeviceEventEmitter
9
+
10
+ export default class RNFetchBlobWriteStream {
11
+
12
+ id : string ;
13
+ encoding : string ;
14
+ append : bool ;
15
+
16
+ constructor ( streamId :string , encoding :string , append :string ) {
17
+ this . id = streamId
18
+ this . encoding = encoding
19
+ this . append = append
20
+ }
21
+
22
+ write ( data :string ) {
23
+ return new Promise ( ( resolve , reject ) => {
24
+ try {
25
+ let method = this . encoding === 'ascii' ? 'writeArrayChunk' : 'writeChunk'
26
+ if ( this . encoding . toLocaleLowerCase ( ) === 'ascii' && ! Array . isArray ( data ) ) {
27
+ reject ( 'ascii input data must be an Array' )
28
+ return
29
+ }
30
+ RNFetchBlob [ method ] ( this . id , data , ( error ) => {
31
+ if ( error )
32
+ reject ( error )
33
+ else
34
+ resolve ( )
35
+ } )
36
+ } catch ( err ) {
37
+ reject ( err )
38
+ }
39
+ } )
40
+ }
41
+
42
+ close ( ) {
43
+ return new Promise ( ( resolve , reject ) => {
44
+ try {
45
+ RNFetchBlob . closeStream ( this . id , ( ) => {
46
+ resolve ( )
47
+ } )
48
+ } catch ( err ) {
49
+ reject ( err )
50
+ }
51
+ } )
52
+ }
53
+
54
+ }
You can’t perform that action at this time.
0 commit comments