No Description

RNFetchBlobSession.js 1.6KB

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