No Description

RNFetchBlobWriteStream.js 1.4KB

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