• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     virtual ~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     struct ObserverWorker {
37         std::weak_ptr<NapiObserver> observer_;
38         std::function<napi_value(napi_env)> getParam;
ObserverWorkerObserverWorker39         explicit ObserverWorker(std::shared_ptr<NapiObserver> observerIn) : observer_(observerIn) {}
40     };
41     static void CallbackFunc(ObserverWorker *observerWorker);
42     napi_env env_ = nullptr;
43     napi_ref ref_ = nullptr;
44     uv_loop_s *loop_ = nullptr;
45 };
46 
47 class NapiRdbObserver final: public NapiObserver, public std::enable_shared_from_this<NapiRdbObserver> {
48 public:
NapiRdbObserver(napi_env env,napi_value callback)49     NapiRdbObserver(napi_env env, napi_value callback) : NapiObserver(env, callback) {};
50     void OnChange(const RdbChangeNode &changeNode);
51 };
52 
53 class NapiPublishedObserver final: public NapiObserver, public std::enable_shared_from_this<NapiPublishedObserver> {
54 public:
NapiPublishedObserver(napi_env env,napi_value callback)55     NapiPublishedObserver(napi_env env, napi_value callback) : NapiObserver(env, callback) {};
56     void OnChange(PublishedDataChangeNode &changeNode);
57 };
58 } // namespace DataShare
59 } // namespace OHOS
60 #endif //NAPI_RDB_OBSERVER_H
61