@@ -937,9 +937,11 @@ extension DownloadManagerModule {
937937
938938 func pollProgress( ) {
939939 guard hasListeners else { return }
940- queue. sync ( flags: . barrier) {
941- let activeDownloads = downloads. filter { $0. value. status == " running " || $0. value. status == " pending " || $0. value. status == " paused " }
942- for (downloadId, var info) in activeDownloads {
940+ queue. async ( flags: . barrier) { [ weak self] in
941+ guard let self = self else { return }
942+ let activeDownloads = self . downloads. filter { $0. value. status == " running " || $0. value. status == " pending " || $0. value. status == " paused " }
943+ var events : [ [ String : Any ] ] = [ ]
944+ for (_, var info) in activeDownloads {
943945 if info. isMultiFile {
944946 var aggregateBytes : Int64 = 0
945947 var aggregateTotal : Int64 = 0
@@ -961,17 +963,24 @@ extension DownloadManagerModule {
961963 if task. countOfBytesExpectedToReceive > 0 {
962964 info. totalBytes = task. countOfBytesExpectedToReceive
963965 }
964- info. status = statusString ( from: task. state)
966+ info. status = self . statusString ( from: task. state)
967+ }
968+ self . downloads [ info. downloadId] = info
969+ events. append ( [
970+ " downloadId " : info. downloadId,
971+ " fileName " : info. fileName,
972+ " modelId " : info. modelId,
973+ " bytesDownloaded " : NSNumber ( value: info. bytesDownloaded) ,
974+ " totalBytes " : NSNumber ( value: info. totalBytes) ,
975+ " status " : info. status
976+ ] as [ String : Any ] )
977+ }
978+ if !events. isEmpty {
979+ DispatchQueue . main. async { [ weak self] in
980+ for event in events {
981+ self ? . sendEvent ( withName: " DownloadProgress " , body: event)
982+ }
965983 }
966- downloads [ downloadId] = info
967- sendEvent ( withName: " DownloadProgress " , body: [
968- " downloadId " : info. downloadId,
969- " fileName " : info. fileName,
970- " modelId " : info. modelId,
971- " bytesDownloaded " : NSNumber ( value: info. bytesDownloaded) ,
972- " totalBytes " : NSNumber ( value: info. totalBytes) ,
973- " status " : info. status
974- ] as [ String : Any ] )
975984 }
976985 }
977986 }
0 commit comments