Ingen beskrivning

RNFetchBlobWriteStream.js 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. const emitter = DeviceEventEmitter
  11. export default class RNFetchBlobWriteStream {
  12. id : string;
  13. encoding : string;
  14. append : bool;
  15. constructor(streamId:string, encoding:string, append:string) {
  16. this.id = streamId
  17. this.encoding = encoding
  18. this.append = append
  19. }
  20. write(data:string) {
  21. return new Promise((resolve, reject) => {
  22. try {
  23. let method = this.encoding === 'ascii' ? 'writeArrayChunk' : 'writeChunk'
  24. if(this.encoding.toLocaleLowerCase() === 'ascii' && !Array.isArray(data)) {
  25. reject('ascii input data must be an Array')
  26. return
  27. }
  28. RNFetchBlob[method](this.id, data, (error) => {
  29. if(error)
  30. reject(error)
  31. else
  32. resolve()
  33. })
  34. } catch(err) {
  35. reject(err)
  36. }
  37. })
  38. }
  39. close() {
  40. return new Promise((resolve, reject) => {
  41. try {
  42. RNFetchBlob.closeStream(this.id, () => {
  43. resolve()
  44. })
  45. } catch (err) {
  46. reject(err)
  47. }
  48. })
  49. }
  50. }