• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 COMMUNICATIONNETSTACK_EVENT_LISTENER_H
17 #define COMMUNICATIONNETSTACK_EVENT_LISTENER_H
18 
19 #include <string>
20 
21 #include "napi/native_api.h"
22 #include "nocopyable.h"
23 #include "uv.h"
24 
25 namespace OHOS::NetStack {
26 class EventListener {
27 public:
28     EventListener() = delete;
29 
30     EventListener(const EventListener &listener);
31 
32     EventListener(napi_env env, std::string type, napi_value callback, bool once, bool asyncCallback);
33 
34     ~EventListener();
35 
36     EventListener &operator=(const EventListener &listener);
37 
38     void Emit(const std::string &eventType, size_t argc, napi_value *argv) const;
39 
40     [[nodiscard]] bool Match(const std::string &type, napi_value callback) const;
41 
42     [[nodiscard]] bool MatchOnce(const std::string &type) const;
43 
44     [[nodiscard]] bool MatchType(const std::string &type) const;
45 
46     [[nodiscard]] bool IsAsyncCallback() const;
47 
48     void EmitByUv(const std::string &type, void *data, void(Handler)(uv_work_t *, int status)) const;
49 
50     [[nodiscard]] napi_env GetEnv() const;
51 
52     [[nodiscard]] napi_ref GetCallbackRef() const;
53 
54 private:
55     napi_env env_;
56 
57     std::string type_;
58 
59     bool once_;
60 
61     napi_ref callbackRef_;
62 
63     bool asyncCallback_;
64 };
65 } // namespace OHOS::NetStack
66 
67 #endif /* COMMUNICATIONNETSTACK_EVENT_LISTENER_H */
68