• 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 "flat_object_store.h"
21 #include "napi/native_api.h"
22 #include "napi/native_node_api.h"
23 #include "uv_queue.h"
24 
25 namespace OHOS::ObjectStore {
26 class JSWatcher;
27 struct EventHandler {
28     napi_ref callbackRef = nullptr;
29     EventHandler *next = nullptr;
30 };
31 
32 class EventListener {
33 public:
EventListener()34     EventListener() : handlers_(nullptr)
35     {
36     }
37 
~EventListener()38     virtual ~EventListener()
39     {
40     }
41 
IsEmpty()42     bool IsEmpty()
43     {
44         return handlers_ == nullptr;
45     }
46 
47     virtual bool Add(napi_env env, napi_value handler);
48 
49     virtual bool Del(napi_env env, napi_value handler);
50 
51     virtual void Clear(napi_env env);
52 
53     EventHandler *Find(napi_env env, napi_value handler);
54     EventHandler *handlers_;
55 };
56 
57 class ChangeEventListener : public EventListener {
58 public:
59     ChangeEventListener(JSWatcher *watcher, DistributedObjectStore *objectStore, DistributedObject *object);
60 
61     bool Add(napi_env env, napi_value handler) override;
62 
63     bool Del(napi_env env, napi_value handler) override;
64 
65     void Clear(napi_env env) override;
66 private:
67     bool isWatched_ = false;
68     DistributedObjectStore *objectStore_;
69     DistributedObject *object_;
70     JSWatcher *watcher_;
71 };
72 
73 class StatusEventListener : public EventListener {
74 public:
75     StatusEventListener(JSWatcher *watcher, const std::string &sessionId);
76     bool Add(napi_env env, napi_value handler) override;
77 
78     bool Del(napi_env env, napi_value handler) override;
79 
80     void Clear(napi_env env) override;
81 private:
82     JSWatcher *watcher_;
83     std::string sessionId_;
84 };
85 
86 class JSWatcher : public UvQueue {
87 public:
88     JSWatcher(const napi_env env, DistributedObjectStore *objectStore, DistributedObject *object);
89 
90     ~JSWatcher();
91 
92     bool On(const char *type, napi_value handler);
93 
94     void Off(const char *type, napi_value handler = nullptr);
95 
96     void Emit(const char *type, const std::string &sessionId, const std::vector<std::string> &changeData);
97 
98     void Emit(const char *type, const std::string &sessionId, const std::string &networkId, const std::string &status);
99 
100     bool IsEmpty();
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(JSWatcher * watcher)126     WatcherImpl(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     JSWatcher *watcher_ = nullptr;
136 };
137 } // namespace OHOS::ObjectStore
138 
139 #endif // JSWATCHER_H
140