• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 JSWATCHER_H
17 #define JSWATCHER_H
18 
19 #include "distributed_objectstore.h"
20 #include "uv_queue.h"
21 
22 namespace OHOS::ObjectStore {
23 class JSWatcher;
24 struct EventHandler {
25     napi_ref callbackRef = nullptr;
26     EventHandler *next = nullptr;
27 };
28 
29 class EventListener {
30 public:
EventListener()31     EventListener() : handlers_(nullptr)
32     {
33     }
34 
~EventListener()35     virtual ~EventListener()
36     {
37     }
38 
IsEmpty()39     bool IsEmpty()
40     {
41         return handlers_ == nullptr;
42     }
43 
44     virtual bool Add(napi_env env, napi_value handler);
45 
46     virtual bool Del(napi_env env, napi_value handler);
47 
48     virtual void Clear(napi_env env);
49 
50     EventHandler *Find(napi_env env, napi_value handler);
51     EventHandler *handlers_;
52 };
53 
54 class ChangeEventListener : public EventListener {
55 public:
56     ChangeEventListener(
57         std::weak_ptr<JSWatcher> watcher, DistributedObjectStore *objectStore, DistributedObject *object);
58 
59     bool Add(napi_env env, napi_value handler) override;
60 
61     bool Del(napi_env env, napi_value handler) override;
62 
63     void Clear(napi_env env) override;
64 private:
65     bool isWatched_ = false;
66     DistributedObjectStore *objectStore_;
67     DistributedObject *object_;
68     std::weak_ptr<JSWatcher> watcher_;
69 };
70 
71 class StatusEventListener : public EventListener {
72 public:
73     StatusEventListener(std::weak_ptr<JSWatcher> watcher, const std::string &sessionId);
74     bool Add(napi_env env, napi_value handler) override;
75 
76     bool Del(napi_env env, napi_value handler) override;
77 
78     void Clear(napi_env env) override;
79 private:
80     std::weak_ptr<JSWatcher> watcher_;
81     std::string sessionId_;
82 };
83 
84 class JSWatcher : public UvQueue {
85 public:
86     JSWatcher(const napi_env env, DistributedObjectStore *objectStore, DistributedObject *object);
87 
88     ~JSWatcher();
89 
90     bool On(const char *type, napi_value handler);
91 
92     void Off(const char *type, napi_value handler = nullptr);
93 
94     void Emit(const char *type, const std::string &sessionId, const std::vector<std::string> &changeData);
95 
96     void Emit(const char *type, const std::string &sessionId, const std::string &networkId, const std::string &status);
97 
98     bool IsEmpty();
99 
100     void SetListener(ChangeEventListener *changeEventListener, StatusEventListener *statusEventListener);
101 private:
102     struct ChangeArgs {
103         ChangeArgs(const napi_ref callback, const std::string &sessionId, const std::vector<std::string> &changeData);
104         napi_ref callback_;
105         const std::string sessionId_;
106         const std::vector<std::string> changeData_;
107     };
108     struct StatusArgs {
109         StatusArgs(const napi_ref callback, const std::string &sessionId, const std::string &networkId,
110             const std::string &status);
111         napi_ref callback_;
112         const std::string sessionId_;
113         const std::string networkId_;
114         const std::string status_;
115     };
116     EventListener *Find(const char *type);
117     static void ProcessChange(napi_env env, std::list<void *> &args);
118     static void ProcessStatus(napi_env env, std::list<void *> &args);
119     napi_env env_;
120     ChangeEventListener *changeEventListener_;
121     StatusEventListener *statusEventListener_;
122 };
123 
124 class WatcherImpl : public ObjectWatcher {
125 public:
WatcherImpl(std::weak_ptr<JSWatcher> watcher)126     WatcherImpl(std::weak_ptr<JSWatcher> watcher) : watcher_(watcher)
127     {
128     }
129 
130     virtual ~WatcherImpl();
131 
132     void OnChanged(const std::string &sessionid, const std::vector<std::string> &changedData) override;
133 
134 private:
135     std::weak_ptr<JSWatcher> watcher_;
136 };
137 } // namespace OHOS::ObjectStore
138 
139 #endif // JSWATCHER_H
140