• 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 #include "bridge/declarative_frontend/jsview/models/view_partial_update_model_impl.h"
17 
18 #include "core/components/grid_layout/grid_layout_item_element.h"
19 #include "core/components/ifelse/if_else_element.h"
20 #include "core/components_v2/common/element_proxy.h"
21 
22 namespace OHOS::Ace::Framework {
23 
CreateNode(NodeInfoPU && info)24 RefPtr<AceType> ViewPartialUpdateModelImpl::CreateNode(NodeInfoPU&& info)
25 {
26     ACE_SCOPED_TRACE("JSViewPartialUpdate::CreateSpecializedComponent");
27     // create component, return new something, need to set proper ID
28 
29     const auto reservedElementId = ViewStackProcessor::GetInstance()->ClaimElementId();
30     const std::string key = std::to_string(reservedElementId);
31     auto composedComponent = AceType::MakeRefPtr<ComposedComponent>(key, "view");
32     composedComponent->SetElementId(reservedElementId);
33     auto isStatic = info.isStatic;
34     if (info.updateViewIdFunc) {
35         info.updateViewIdFunc(key);
36     }
37 
38     auto renderFunction = [renderFunc = std::move(info.renderFunc), updateFunc = std::move(info.updateFunc)](
39                               const RefPtr<Component>& component) -> RefPtr<Component> {
40         bool isTimeout = false;
41         auto node = renderFunc ? renderFunc(0, isTimeout) : nullptr;
42         if (updateFunc) {
43             updateFunc();
44         }
45         return AceType::DynamicCast<Component>(node);
46     };
47 
48     auto elementFunction = [renderFunction = std::move(renderFunction), nodeInfo = std::move(info)](
49                                const RefPtr<ComposedElement>& element) mutable {
50         if (nodeInfo.appearFunc) {
51             nodeInfo.appearFunc();
52         }
53         if (nodeInfo.updateNodeFunc) {
54             nodeInfo.updateNodeFunc(element);
55         }
56 
57         // add render function callback to element. when the element rebuilds due
58         // to state update it will call this callback to get the new child component.
59         if (element) {
60             element->SetRenderFunction(std::move(renderFunction));
61             element->SetRemoveFunction(std::move(nodeInfo.removeFunc));
62             if (nodeInfo.pageTransitionFunc) {
63                 auto pageTransitionFunction = [transitionFunc =
64                                                       std::move(nodeInfo.pageTransitionFunc)]() -> RefPtr<Component> {
65                     transitionFunc();
66                     auto pageTransitionComponent = ViewStackProcessor::GetInstance()->GetPageTransitionComponent();
67                     ViewStackProcessor::GetInstance()->ClearPageTransitionComponent();
68                     return pageTransitionComponent;
69                 };
70                 element->SetPageTransitionFunction(std::move(pageTransitionFunction));
71             }
72         }
73     };
74 
75     composedComponent->SetElementFunction(std::move(elementFunction));
76 
77     if (isStatic) {
78         composedComponent->SetStatic();
79     }
80 
81     return composedComponent;
82 }
83 
MarkNeedUpdate(const WeakPtr<AceType> & node)84 bool ViewPartialUpdateModelImpl::MarkNeedUpdate(const WeakPtr<AceType>& node)
85 {
86     ACE_SCOPED_TRACE("JSView::MarkNeedUpdate");
87     auto weakElement = AceType::DynamicCast<ComposedElement>(node);
88     if (weakElement.Invalid()) {
89         LOGE("Invalid Element weak ref, internal error");
90         return false;
91     }
92     auto element = weakElement.Upgrade();
93     if (element) {
94         element->MarkDirty();
95     }
96     return true;
97 }
98 
FlushUpdateTask(const UpdateTask & task)99 void ViewPartialUpdateModelImpl::FlushUpdateTask(const UpdateTask& task)
100 {
101     const int32_t elmtId = std::get<0>(task);
102     const RefPtr<Component> outmostWrappingComponent = AceType::DynamicCast<Component>(std::get<1>(task)); // stop at
103     const RefPtr<Component> mainComponent = AceType::DynamicCast<Component>(std::get<2>(task));            // has elmtId
104 
105     ACE_DCHECK(mainComponent != nullptr);
106     ACE_DCHECK(elmtId != ElementRegister::UndefinedElementId);
107 
108     RefPtr<Element> element = ElementRegister::GetInstance()->GetElementById(elmtId);
109     if (element != nullptr) {
110         // special case, because new IfElement will be created
111         if (AceType::DynamicCast<IfElseElement>(element) != nullptr) {
112             IfElseElement::ComponentToElementLocalizedUpdate(mainComponent, element);
113         } else if (AceType::DynamicCast<GridLayoutItemElement>(element) != nullptr) {
114             // VSP::Finish returns swapped compared to reg
115             AceType::DynamicCast<SoleChildElement>(element)->LocalizedUpdateWithItemComponent(
116                 mainComponent, outmostWrappingComponent);
117         } else {
118             element->LocalizedUpdateWithComponent(mainComponent, outmostWrappingComponent);
119         }
120         return;
121     }
122 
123     auto elementProxy = ElementRegister::GetInstance()->GetElementProxyById(elmtId);
124     if (elementProxy != nullptr) {
125         elementProxy->LocalizedUpdate(mainComponent, outmostWrappingComponent);
126         return;
127     }
128 
129     LOGE("No suitable Element/ElementProxy with elmtId %{public}d found to update from %{public}s,"
130          " elmtId exists in ElementRegister "
131          "'%{public}s'.",
132         elmtId, (mainComponent ? AceType::TypeName(mainComponent) : "no Component error"),
133         (ElementRegister::GetInstance()->Exists(elmtId) ? "exists" : "missing"));
134 }
135 
FinishUpdate(const WeakPtr<AceType> & viewNode,int32_t id,std::function<void (const UpdateTask &)> && emplaceTaskFunc)136 void ViewPartialUpdateModelImpl::FinishUpdate(
137     const WeakPtr<AceType>& viewNode, int32_t id, std::function<void(const UpdateTask&)>&& emplaceTaskFunc)
138 {
139     auto componentsPair = ViewStackProcessor::GetInstance()->FinishReturnMain();
140     if ((componentsPair.first == nullptr) || (componentsPair.second == nullptr)) {
141         LOGE("outmost wrapping component is null");
142         return;
143     }
144     // chk main component componentsPair.second elmtId
145     ACE_DCHECK(componentsPair.second->GetElementId() == id);
146     // push the result of the update function with elmtId added on the list of pending updates, triple:
147     // 0: elmtId
148     // 1: outmost wrapping Component (most keep reference until localized updates done to avoid smart pointer auto
149     // deletion!) 2: main Component
150     if (emplaceTaskFunc) {
151         emplaceTaskFunc(std::make_tuple(id, componentsPair.first, componentsPair.second));
152     }
153 
154     // FlushBuild on UI thread side
155     // will call MakeElementUpdatesToCompleteRerender
156     auto element = AceType::DynamicCast<ComposedElement>(viewNode.Upgrade());
157     if (element) {
158         element->MarkDirty();
159     } else {
160         LOGE("Internal error, element is is null");
161     }
162 }
163 
164 } // namespace OHOS::Ace::Framework
165