• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 #include "napi_rdb_statistics_observer.h"
17 
18 #include "napi_rdb_js_utils.h"
19 
20 namespace OHOS::RelationalStoreJsKit {
NapiStatisticsObserver(napi_env env,napi_value callback,std::shared_ptr<AppDataMgrJsKit::UvQueue> queue)21 NapiStatisticsObserver::NapiStatisticsObserver(
22     napi_env env, napi_value callback, std::shared_ptr<AppDataMgrJsKit::UvQueue> queue)
23     : env_(env), queue_(queue)
24 {
25     napi_create_reference(env, callback, 1, &callback_);
26 }
27 
~NapiStatisticsObserver()28 NapiStatisticsObserver::~NapiStatisticsObserver()
29 {
30 }
31 
Clear()32 void NapiStatisticsObserver::Clear()
33 {
34     if (callback_ == nullptr) {
35         return;
36     }
37     napi_delete_reference(env_, callback_);
38     callback_ = nullptr;
39 }
40 
operator ==(napi_value value)41 bool NapiStatisticsObserver::operator==(napi_value value)
42 {
43     return JSUtils::Equal(env_, callback_, value);
44 }
45 
OnStatistic(const SqlExecutionInfo & sqlExeInfo)46 void NapiStatisticsObserver::OnStatistic(const SqlExecutionInfo &sqlExeInfo)
47 {
48     auto queue = queue_;
49     if (queue == nullptr) {
50         return;
51     }
52     queue->AsyncCall({ [observer = shared_from_this()](napi_env env) -> napi_value {
53         if (observer->callback_ == nullptr) {
54             return nullptr;
55         }
56         napi_value callback = nullptr;
57         napi_get_reference_value(env, observer->callback_, &callback);
58         return callback;
59     } },
60         [infos = std::move(sqlExeInfo)](napi_env env, int &argc, napi_value *argv) {
61             argc = 1;
62             argv[0] = JSUtils::Convert2JSValue(env, infos);
63         });
64 }
65 
NapiPerfStatObserver(napi_env env,napi_value callback,std::shared_ptr<AppDataMgrJsKit::UvQueue> queue)66 NapiPerfStatObserver::NapiPerfStatObserver(
67     napi_env env, napi_value callback, std::shared_ptr<AppDataMgrJsKit::UvQueue> queue)
68     : env_(env), queue_(queue)
69 {
70     napi_create_reference(env, callback, 1, &callback_);
71 }
72 
~NapiPerfStatObserver()73 NapiPerfStatObserver::~NapiPerfStatObserver()
74 {
75 }
76 
Clear()77 void NapiPerfStatObserver::Clear()
78 {
79     if (callback_ == nullptr) {
80         return;
81     }
82     napi_delete_reference(env_, callback_);
83     callback_ = nullptr;
84 }
85 
operator ==(napi_value value)86 bool NapiPerfStatObserver::operator==(napi_value value)
87 {
88     return JSUtils::Equal(env_, callback_, value);
89 }
90 
OnStatistic(const SqlExecutionInfo & sqlExeInfo)91 void NapiPerfStatObserver::OnStatistic(const SqlExecutionInfo &sqlExeInfo)
92 {
93     auto queue = queue_;
94     if (queue == nullptr) {
95         return;
96     }
97     queue->AsyncCall({ [observer = shared_from_this()](napi_env env) -> napi_value {
98         if (observer->callback_ == nullptr) {
99             return nullptr;
100         }
101         napi_value callback = nullptr;
102         napi_get_reference_value(env, observer->callback_, &callback);
103         return callback;
104     } },
105         [infos = std::move(sqlExeInfo)](napi_env env, int &argc, napi_value *argv) {
106             argc = 1;
107             argv[0] = JSUtils::Convert2JSValue(env, infos);
108         });
109 }
110 } // namespace OHOS::RelationalStoreJsKit