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_DATASHARE_INNER_OBSERVER_H
17 #define NAPI_DATASHARE_INNER_OBSERVER_H
18 
19 #include <uv.h>
20 
21 #include "datashare_helper.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 NAPIInnerObserver : public std::enable_shared_from_this<NAPIInnerObserver> {
29 public:
30     NAPIInnerObserver(napi_env env, napi_value callback);
31     void OnChange(const DataShareObserver::ChangeInfo &changeInfo = {}, bool isNotifyDetails = false);
32     void DeleteReference();
33     napi_ref GetCallback();
34 
35 private:
36     static void OnComplete(uv_work_t *work, int status);
37     struct ObserverWorker {
38         std::weak_ptr<NAPIInnerObserver> observer_;
39         DataShareObserver::ChangeInfo result_;
40         bool isNotifyDetails_ = false;
41         ObserverWorker(std::shared_ptr<NAPIInnerObserver> observerIn, DataShareObserver::ChangeInfo resultIn = {})
42             : observer_(observerIn), result_(resultIn) {}
43     };
44 
45     napi_env env_ = nullptr;
46     napi_ref ref_ = nullptr;
47     uv_loop_s *loop_ = nullptr;
48 };
49 }  // namespace DataShare
50 }  // namespace OHOS
51 #endif //NAPI_DATASHARE_INNER_OBSERVER_H