@@ -137,6 +137,11 @@ class DownloadService {
137137 callbacks_;
138138 std::mutex callbacks_mutex_;
139139
140+ std::unordered_map<std::string,
141+ std::chrono::time_point<std::chrono::steady_clock>>
142+ event_emit_map_;
143+ std::mutex event_emit_map_mutex;
144+
140145 void WorkerThread ();
141146
142147 bool IsTaskTerminated (const std::string& task_id);
@@ -154,18 +159,17 @@ class DownloadService {
154159 return 0 ;
155160 }
156161
157- if (downloading_data->download_service == nullptr ) {
162+ auto dl_srv = downloading_data->download_service ;
163+ if (dl_srv == nullptr ) {
158164 return 0 ;
159165 }
160166
161167 // Lock during the update and event emission
162- std::lock_guard<std::mutex> lock (
163- downloading_data->download_service ->active_tasks_mutex_ );
168+ std::lock_guard<std::mutex> lock (dl_srv->active_tasks_mutex_ );
164169
165170 // Find and update the task
166- if (auto task_it = downloading_data->download_service ->active_tasks_ .find (
167- downloading_data->task_id );
168- task_it != downloading_data->download_service ->active_tasks_ .end ()) {
171+ if (auto task_it = dl_srv->active_tasks_ .find (downloading_data->task_id );
172+ task_it != dl_srv->active_tasks_ .end ()) {
169173 auto & task = task_it->second ;
170174 // Find the specific item in the task
171175 for (auto & item : task->items ) {
@@ -182,22 +186,38 @@ class DownloadService {
182186 item.bytes = dltotal;
183187 item.downloadedBytes = dlnow;
184188
185- static auto last_event_time = std::chrono::steady_clock::now ();
186- auto current_time = std::chrono::steady_clock::now ();
187- auto time_since_last_event =
188- std::chrono::duration_cast<std::chrono::milliseconds>(
189- current_time - last_event_time)
190- .count ();
191-
192- // throttle event by 1 sec
193- if (time_since_last_event >= 1000 ) {
194- downloading_data->download_service ->event_queue_ ->enqueue (
195- EventType::DownloadEvent,
196- DownloadEvent{.type_ = DownloadEventType::DownloadUpdated,
197- .download_task_ = *task});
198-
199- // Update the last event time
200- last_event_time = current_time;
189+ // Emit the event
190+ {
191+ std::lock_guard<std::mutex> event_lock (dl_srv->event_emit_map_mutex );
192+ // find the task id in event_emit_map
193+ // if not found, add it to the map
194+ auto should_emit_event{false };
195+ if (dl_srv->event_emit_map_ .find (task->id ) !=
196+ dl_srv->event_emit_map_ .end ()) {
197+ auto last_event_time = dl_srv->event_emit_map_ [task->id ];
198+ auto current_time = std::chrono::steady_clock::now ();
199+
200+ auto time_since_last_event =
201+ std::chrono::duration_cast<std::chrono::milliseconds>(
202+ current_time - last_event_time)
203+ .count ();
204+ if (time_since_last_event >= 1000 ) {
205+ // if the time since last event is more than 1 sec, emit the event
206+ should_emit_event = true ;
207+ }
208+ } else {
209+ // if the task id is not found in the map, emit
210+ should_emit_event = true ;
211+ }
212+
213+ if (should_emit_event) {
214+ dl_srv->event_queue_ ->enqueue (
215+ EventType::DownloadEvent,
216+ DownloadEvent{.type_ = DownloadEventType::DownloadUpdated,
217+ .download_task_ = *task});
218+ dl_srv->event_emit_map_ [task->id ] =
219+ std::chrono::steady_clock::now ();
220+ }
201221 }
202222
203223 break ;
0 commit comments