Sin descripción

RNFetchBlobSession.js 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /**
  2. * Session class
  3. * @class RNFetchBlobSession
  4. */
  5. import {
  6. NativeModules,
  7. DeviceEventEmitter,
  8. NativeAppEventEmitter,
  9. } from 'react-native'
  10. const RNFetchBlob = NativeModules.RNFetchBlob
  11. const emitter = DeviceEventEmitter
  12. export default class RNFetchBlobSession {
  13. add : (path:string) => RNFetchBlobSession;
  14. remove : (path:string) => RNFetchBlobSession;
  15. dispose : () => Promise;
  16. list : () => Array<string>;
  17. name : string;
  18. constructor(name:string, list:Array<string>) {
  19. this.name = name
  20. if(!sessions[name]) {
  21. if(Array.isArray(list))
  22. sessions[name] = list
  23. else
  24. sessions[name] = []
  25. }
  26. }
  27. add(path:string):RNFetchBlobSession {
  28. sessions[this.name].push(path)
  29. return this
  30. }
  31. remove(path:string):RNFetchBlobSession {
  32. let list = sessions[this.name]
  33. for(let i in list) {
  34. if(list[i] === path) {
  35. sessions[this.name].splice(i, 1)
  36. break;
  37. }
  38. }
  39. return this
  40. }
  41. list():Array<string> {
  42. return sessions[this.name]
  43. }
  44. dispose():Promise {
  45. return new Promise((resolve, reject) => {
  46. RNFetchBlob.removeSession(sessions[this.name], (err) => {
  47. if(err)
  48. reject(err)
  49. else {
  50. delete sessions[this.name]
  51. resolve()
  52. }
  53. })
  54. })
  55. }
  56. }