No Description

RNFetchBlobSession.js 1.6KB

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