From fe29371f5de619cc4250ccdef7e5ac0a930f3f97 Mon Sep 17 00:00:00 2001 From: Littlegnal <8847263+littleGnAl@users.noreply.github.com> Date: Thu, 10 Oct 2024 16:21:16 +0800 Subject: [PATCH] fix: Fix missing `OnEvent` callbacks with `IrisMethodChannel` used by multiple packages (#108) Resolved an issue where `OnEvent` callbacks were not received when `IrisMethodChannel` was used by multiple packages (e.g., `agora_rtc_engine` and `agora_rtm`) simultaneously. This fix ensures that events are properly handled even after disposal, maintaining consistent behavior across packages. --- src/iris_event.cc | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/iris_event.cc b/src/iris_event.cc index 0097ce2..bb0f671 100644 --- a/src/iris_event.cc +++ b/src/iris_event.cc @@ -199,23 +199,32 @@ class DartMessageHandlerManager : public DartMessageHandlerBase std::mutex message_handler_mutex_; std::unique_ptr dartMessageHandlerManager_ = nullptr; +int init_dart_api_times_ = 0; // Initialize `dart_api_dl.h` EXPORT intptr_t InitDartApiDL(void *data) { std::lock_guard lock(message_handler_mutex_); - if (!dartMessageHandlerManager_) + int ret = 0; + if (init_dart_api_times_ == 0 && !dartMessageHandlerManager_) { dartMessageHandlerManager_ = std::make_unique(); + ret = dartMessageHandlerManager_->InitDartApiDL(data); } - return dartMessageHandlerManager_->InitDartApiDL(data); + ++init_dart_api_times_; + + return ret; } EXPORT void Dispose() { std::lock_guard lock(message_handler_mutex_); - dartMessageHandlerManager_.reset(); + --init_dart_api_times_; + if (init_dart_api_times_ == 0) + { + dartMessageHandlerManager_.reset(); + } } EXPORT void OnEvent(EventParam *param)