1 /*
2 * Copyright (c) 2021 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 "lazy_load_manager.h"
17 #include "ace_log.h"
18 #include "component.h"
19 #include "component_utils.h"
20 #include "lazy_load_watcher.h"
21
22 namespace OHOS {
23 namespace ACELite {
LazyLoadManager()24 LazyLoadManager::LazyLoadManager() : state_(LazyLoadState::INIT)
25 {
26 }
27
~LazyLoadManager()28 LazyLoadManager::~LazyLoadManager()
29 {
30 ResetWatchers();
31 }
32
ResetWatchers()33 void LazyLoadManager::ResetWatchers()
34 {
35 ListNode<LazyLoadWatcher *> *node = lazyWatchersList_.Begin();
36 while (node != lazyWatchersList_.End()) {
37 if (node->data_ != nullptr) {
38 delete node->data_;
39 node->data_ = nullptr;
40 }
41 node = node->next_;
42 }
43 lazyWatchersList_.Clear();
44 state_ = LazyLoadState::INIT;
45 }
46
RenderLazyLoadWatcher()47 void LazyLoadManager::RenderLazyLoadWatcher()
48 {
49 ListNode<LazyLoadWatcher *> *node = lazyWatchersList_.Begin();
50 while (node != lazyWatchersList_.End()) {
51 if (node->data_ != nullptr) {
52 // handle it
53 RenderSingleLazyWatcher(*(node->data_));
54 delete node->data_;
55 node->data_ = nullptr;
56 }
57 node = node->next_;
58 }
59 lazyWatchersList_.Clear();
60 state_ = LazyLoadState::DONE;
61 }
62
RenderSingleLazyWatcher(const LazyLoadWatcher & watcher) const63 void LazyLoadManager::RenderSingleLazyWatcher(const LazyLoadWatcher &watcher) const
64 {
65 jerry_value_t attrName = watcher.GetAttrName();
66 jerry_value_t attrGetter = watcher.GetAttrGetter();
67 uint16_t attrKeyID = watcher.GetKeyId();
68 Component *component = ComponentUtils::GetComponentFromBindingObject(watcher.GetNativeElement());
69 jerry_value_t latestValue =
70 (component == nullptr) ? UNDEFINED : component->AddWatcherItem(attrName, attrGetter, true);
71 if (attrKeyID == K_UNKNOWN) {
72 // try to parse from attr name directly
73 attrKeyID = ParseKeyIdFromJSString(attrName);
74 }
75
76 if ((!IS_UNDEFINED(latestValue)) && (attrKeyID != K_UNKNOWN)) {
77 // need to update the view with the latest value, in case the value is already changed
78 if (component->UpdateView(attrKeyID, latestValue)) {
79 component->Invalidate();
80 }
81 }
82 // the new value has been calculated out by ParseExpression, need to be released
83 jerry_release_value(latestValue);
84 }
85
AddLazyLoadWatcher(jerry_value_t nativeElement,jerry_value_t attrName,jerry_value_t getter)86 void LazyLoadManager::AddLazyLoadWatcher(jerry_value_t nativeElement,
87 jerry_value_t attrName,
88 jerry_value_t getter)
89 {
90 // pass key ID as UNKNOWN, and will be calculated out from attrName when using
91 AddLazyLoadWatcher(nativeElement, attrName, getter, K_UNKNOWN);
92 }
93
AddLazyLoadWatcher(jerry_value_t nativeElement,jerry_value_t attrName,jerry_value_t getter,uint16_t keyId)94 void LazyLoadManager::AddLazyLoadWatcher(jerry_value_t nativeElement,
95 jerry_value_t attrName,
96 jerry_value_t getter,
97 uint16_t keyId)
98 {
99 if (nativeElement == UNDEFINED || attrName == UNDEFINED || getter == UNDEFINED) {
100 return;
101 }
102
103 LazyLoadWatcher *watcher = new LazyLoadWatcher(nativeElement, attrName, getter, keyId);
104 if (watcher == nullptr) {
105 HILOG_ERROR(HILOG_MODULE_ACE, "create watcher errpr");
106 return;
107 }
108 lazyWatchersList_.PushBack(watcher);
109 // The state must be ready if any watcher lazy loading request was added, otherwise, in some cases,
110 // the js_ability may not be able to know there are watchers need to be loaded.
111 state_ = LazyLoadState::READY;
112 }
113
RemoveLazyWatcher(jerry_value_t nativeElement)114 void LazyLoadManager::RemoveLazyWatcher(jerry_value_t nativeElement)
115 {
116 if (lazyWatchersList_.IsEmpty()) {
117 return;
118 }
119
120 ListNode<LazyLoadWatcher *> *node = lazyWatchersList_.Begin();
121 while (node != lazyWatchersList_.End()) {
122 if (node->data_ == nullptr) {
123 node = node->next_;
124 continue;
125 }
126 if (node->data_->GetNativeElement() == nativeElement) {
127 // found, remove the node
128 delete node->data_;
129 node->data_ = nullptr;
130 node = lazyWatchersList_.Remove(node);
131 continue;
132 }
133 node = node->next_;
134 }
135 }
136 } // namespace ACELite
137 } // namespace OHOS
138