forked from wkh237/react-native-fetch-blob
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRNFetchBlobWriteStream.js
58 lines (50 loc) · 1.31 KB
/
RNFetchBlobWriteStream.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Copyright 2016 wkh237@github. All rights reserved.
// Use of this source code is governed by a MIT-style license that can be
// found in the LICENSE file.
import {
NativeModules,
DeviceEventEmitter,
NativeAppEventEmitter,
} from 'react-native'
const RNFetchBlob = NativeModules.RNFetchBlob
const emitter = DeviceEventEmitter
export default class RNFetchBlobWriteStream {
id : string;
encoding : string;
append : bool;
constructor(streamId:string, encoding:string, append:string) {
this.id = streamId
this.encoding = encoding
this.append = append
}
write(data:string) {
return new Promise((resolve, reject) => {
try {
let method = this.encoding === 'ascii' ? 'writeArrayChunk' : 'writeChunk'
if(this.encoding.toLocaleLowerCase() === 'ascii' && !Array.isArray(data)) {
reject('ascii input data must be an Array')
return
}
RNFetchBlob[method](this.id, data, (error) => {
if(error)
reject(error)
else
resolve()
})
} catch(err) {
reject(err)
}
})
}
close() {
return new Promise((resolve, reject) => {
try {
RNFetchBlob.closeStream(this.id, () => {
resolve()
})
} catch (err) {
reject(err)
}
})
}
}