• 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_log_observer.h"
17 
18 #include "napi_rdb_js_utils.h"
19 
20 namespace OHOS::RelationalStoreJsKit {
NapiLogObserver(napi_env env,napi_value callback,std::shared_ptr<AppDataMgrJsKit::UvQueue> queue)21 NapiLogObserver::NapiLogObserver(
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 
~NapiLogObserver()28 NapiLogObserver::~NapiLogObserver()
29 {
30 }
31 
Clear()32 void NapiLogObserver::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 NapiLogObserver::operator==(napi_value value)
42 {
43     return JSUtils::Equal(env_, callback_, value);
44 }
45 
OnErrorLog(const ExceptionMessage & exceptionMessage)46 void NapiLogObserver::OnErrorLog(const ExceptionMessage &exceptionMessage)
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(exceptionMessage)](napi_env env, int &argc, napi_value *argv) {
61             argc = 1;
62             argv[0] = JSUtils::Convert2JSValue(env, infos);
63         });
64 }
65 } // namespace OHOS::RelationalStoreJsKit