1 /*
2 * Copyright (c) 2021-2022 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 BASE_EVENTHANDLER_FRAMEWORKS_NAPI_INCLUDE_JS_EMITTER_H
17 #define BASE_EVENTHANDLER_FRAMEWORKS_NAPI_INCLUDE_JS_EMITTER_H
18
19 #include <map>
20 #include <memory>
21 #include <string>
22 #include <vector>
23
24 #include "event_handler.h"
25 #include "event_runner.h"
26 #include "event_queue.h"
27 #include "inner_event.h"
28 #include <unordered_set>
29 #include "napi/native_api.h"
30 #include "napi/native_node_api.h"
31
32 namespace OHOS {
33 namespace AppExecFwk {
34 using Priority = EventQueue::Priority;
35 static const int32_t ARGC_NUM = 2;
36 static const int32_t NAPI_VALUE_STRING_LEN = 10240;
37
38 enum class DataType: uint32_t {
39 BOOL = 0,
40 INT,
41 STRING,
42 };
43
44 struct Val {
45 DataType type;
46
47 union {
48 bool bValue;
49 int32_t nValue;
50 char cValue[NAPI_VALUE_STRING_LEN] = {0};
51 } value;
52 };
53
54 napi_value EmitterInit(napi_env env, napi_value exports);
55 napi_value JS_On(napi_env env, napi_callback_info cbinfo);
56 napi_value JS_Off(napi_env env, napi_callback_info cbinfo);
57 napi_value JS_Once(napi_env env, napi_callback_info cbinfo);
58 napi_value JS_Emit(napi_env env, napi_callback_info cbinfo);
59
GetNapiType(napi_env env,napi_value param)60 static inline napi_valuetype GetNapiType(napi_env env, napi_value param)
61 {
62 napi_valuetype type;
63 napi_typeof(env, param, &type);
64 return type;
65 }
66 } // namespace AppExecFwk
67 } // namespace OHOS
68
69 #endif // BASE_EVENTHANDLER_FRAMEWORKS_NAPI_INCLUDE_JS_EMITTER_H
70