1 /* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef NAPI_RDB_OBSERVER_H 17 #define NAPI_RDB_OBSERVER_H 18 19 #include <uv.h> 20 21 #include "datashare_template.h" 22 #include "napi/native_api.h" 23 #include "napi/native_common.h" 24 #include "napi/native_node_api.h" 25 26 namespace OHOS { 27 namespace DataShare { 28 class NapiObserver { 29 public: 30 NapiObserver(napi_env env, napi_value callback); 31 ~NapiObserver(); 32 virtual bool operator==(const NapiObserver &rhs) const; 33 virtual bool operator!=(const NapiObserver &rhs) const; 34 NapiObserver& operator=(NapiObserver &&rhs) = default; 35 protected: 36 static void CallbackFunc(uv_work_t *work, int status); 37 napi_env env_ = nullptr; 38 napi_ref ref_ = nullptr; 39 uv_loop_s *loop_ = nullptr; 40 }; 41 42 class NapiRdbObserver final: public NapiObserver, public std::enable_shared_from_this<NapiRdbObserver> { 43 public: NapiRdbObserver(napi_env env,napi_value callback)44 NapiRdbObserver(napi_env env, napi_value callback) : NapiObserver(env, callback) {}; 45 void OnChange(const RdbChangeNode &changeNode); 46 }; 47 48 class NapiPublishedObserver final: public NapiObserver, public std::enable_shared_from_this<NapiPublishedObserver> { 49 public: NapiPublishedObserver(napi_env env,napi_value callback)50 NapiPublishedObserver(napi_env env, napi_value callback) : NapiObserver(env, callback) {}; 51 void OnChange(PublishedDataChangeNode &changeNode); 52 }; 53 54 struct ObserverWorker { 55 std::weak_ptr<NapiObserver> observer_; 56 std::function<napi_value(napi_env)> getParam; ObserverWorkerObserverWorker57 explicit ObserverWorker(std::shared_ptr<NapiObserver> observerIn) : observer_(observerIn) {} 58 }; 59 } // namespace DataShare 60 } // namespace OHOS 61 #endif //NAPI_RDB_OBSERVER_H 62