Brak opisu

RNFetchBlobSession.js 1.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. let sessions = {}
  11. export default class RNFetchBlobSession {
  12. name : string;
  13. static getSession(name:string):any {
  14. return sessions[name]
  15. }
  16. static setSession(name:string, val:any) {
  17. sessions[name] = val
  18. }
  19. static removeSession(name:string) {
  20. delete sessions[name]
  21. }
  22. constructor(name:string, list:Array<string>) {
  23. this.name = name
  24. if(!sessions[name]) {
  25. if(Array.isArray(list))
  26. sessions[name] = list
  27. else
  28. sessions[name] = []
  29. }
  30. }
  31. add(path:string):RNFetchBlobSession {
  32. sessions[this.name].push(path)
  33. return this
  34. }
  35. remove(path:string):RNFetchBlobSession {
  36. let list = sessions[this.name]
  37. for(let i of list) {
  38. if(list[i] === path) {
  39. sessions[this.name].splice(i, 1)
  40. break;
  41. }
  42. }
  43. return this
  44. }
  45. list():Array<string> {
  46. return sessions[this.name]
  47. }
  48. dispose():Promise {
  49. return new Promise((resolve, reject) => {
  50. RNFetchBlob.removeSession(sessions[this.name], (err) => {
  51. if(err)
  52. reject(new Error(err))
  53. else {
  54. delete sessions[this.name]
  55. resolve()
  56. }
  57. })
  58. })
  59. }
  60. }