diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/ani/watch_event_listener.cpp b/interfaces/kits/js/src/mod_fs/class_watcher/ani/watch_event_listener.cpp index 36ac7517d6d286f627a4ffd4e5c05002e49928d0..fad6aeaccdb1d24be3f5fec802790a727e693bf6 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/ani/watch_event_listener.cpp +++ b/interfaces/kits/js/src/mod_fs/class_watcher/ani/watch_event_listener.cpp @@ -52,24 +52,9 @@ bool WatchEventListener::IsStrictEquals(const shared_ptr &othe return isSame; } -void WatchEventListener::InvokeCallback(const string &fileName, uint32_t event, uint32_t cookie) const -{ - auto watchEvent = CreateSharedPtr(); - if (watchEvent == nullptr) { - HILOGE("Failed to request heap memory."); - return; - } - - watchEvent->fileName = fileName; - watchEvent->event = event; - watchEvent->cookie = cookie; - auto task = [this, watchEvent]() { SendWatchEvent(*watchEvent); }; - AniHelper::SendEventToMainThread(task); -} - inline static const int32_t ANI_SCOPE_SIZE = 16; -void WatchEventListener::SendWatchEvent(const WatchEvent &watchEvent) const +static void SendWatchEvent(ani_vm *vm, ani_fn_object callback, const WatchEvent &watchEvent) { if (vm == nullptr) { HILOGE("Cannot send WatchEvent because the vm is null."); @@ -98,8 +83,7 @@ void WatchEventListener::SendWatchEvent(const WatchEvent &watchEvent) const vector args = { static_cast(evtObj) }; auto argc = args.size(); ani_ref result; - auto cbObj = static_cast(callback); - status = env->FunctionalObject_Call(cbObj, argc, args.data(), &result); + status = env->FunctionalObject_Call(callback, argc, args.data(), &result); if (status != ANI_OK) { HILOGE("Failed to call FunctionalObject_Call, status: %{public}d", static_cast(status)); // continue execution and not exit. @@ -111,4 +95,23 @@ void WatchEventListener::SendWatchEvent(const WatchEvent &watchEvent) const } } +void WatchEventListener::InvokeCallback(const string &fileName, uint32_t event, uint32_t cookie) const +{ + auto watchEvent = CreateSharedPtr(); + if (watchEvent == nullptr) { + HILOGE("Failed to request heap memory."); + return; + } + + watchEvent->fileName = fileName; + watchEvent->event = event; + watchEvent->cookie = cookie; + auto localVm = vm; + auto localCallback = static_cast(callback); + auto task = [localVm, localCallback, watchEvent]() { + SendWatchEvent(localVm, localCallback, *watchEvent); + }; + AniHelper::SendEventToMainThread(task); +} + } // namespace OHOS::FileManagement::ModuleFileIO::ANI diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/ani/watch_event_listener.h b/interfaces/kits/js/src/mod_fs/class_watcher/ani/watch_event_listener.h index 919739228b992670dccb5441781c8fe0db0fe63c..3e7e849c41f6370bb629fa0b63e641bdfb6e9790 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/ani/watch_event_listener.h +++ b/interfaces/kits/js/src/mod_fs/class_watcher/ani/watch_event_listener.h @@ -38,8 +38,6 @@ public: private: inline static const std::string className_ = "WatchEventListener"; - void SendWatchEvent(const WatchEvent &watchEvent) const; - ani_vm *vm; ani_ref callback; };