1 /*
2 * Copyright (c) 2024 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 "BasicServicesKit/oh_commonevent.h"
17 #include "BasicServicesKit/oh_commonevent_support.h"
18 #include "common.h"
19 #include "hilog/log.h"
20 #include "napi/native_api.h"
21
Publish(napi_env env,napi_callback_info info)22 static napi_value Publish(napi_env env, napi_callback_info info) {
23 OH_LOG_Print(LOG_APP, LOG_INFO, 1, "CES_TEST", "start");
24 const char *event = COMMON_EVENT_TEST;
25 int32_t ret = OH_CommonEvent_Publish(event);
26 OH_LOG_Print(LOG_APP, LOG_INFO, 1, "CES_TEST", "unSubscribe result %{public}d", ret);
27 napi_value res = nullptr;
28 napi_create_int32(env, ret, &res);
29 return res;
30 }
31
PublishWithInfo(napi_env env,napi_callback_info info)32 static napi_value PublishWithInfo(napi_env env, napi_callback_info info) {
33 const char *event = COMMON_EVENT_TEST;
34 bool ordered = true;
35 auto publishInfo = OH_CommonEvent_CreatePublishInfo(ordered);
36 int32_t ret = OH_CommonEvent_PublishWithInfo(event, publishInfo);
37 OH_LOG_Print(LOG_APP, LOG_INFO, 1, "CES_TEST", "unSubscribe result %{public}d", ret);
38 if (publishInfo != nullptr) {
39 OH_CommonEvent_DestroyPublishInfo(publishInfo);
40 }
41 publishInfo = nullptr;
42 napi_value res = nullptr;
43 napi_create_int32(env, ret, &res);
44 return res;
45 }