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 FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_SYNTAX_SHALLOW_BUILDER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_SYNTAX_SHALLOW_BUILDER_H
18 
19 #include <functional>
20 
21 #include "base/memory/ace_type.h"
22 #include "base/utils/utils.h"
23 #include "core/components_ng/base/ui_node.h"
24 
25 namespace OHOS::Ace::NG {
26 
27 // ShallowBuilder对象用于ListItem和GridItem内部内容懒加载能力。
28 // 在最小化更新场景下,ListItem、GridItem这些组件采用懒加载方式,内部内容在js执行时不会创建仅保存创建方法。
29 class ACE_EXPORT ShallowBuilder : public virtual AceType {
30     DECLARE_ACE_TYPE(ShallowBuilder, AceType);
31 
32 public:
33     using DeepRenderFunc = std::function<RefPtr<UINode>()>;
34 
ShallowBuilder(DeepRenderFunc && deepRenderFunc)35     explicit ShallowBuilder(DeepRenderFunc&& deepRenderFunc) : deepRenderFunc_(std::move(deepRenderFunc)) {}
36     ~ShallowBuilder() override = default;
37 
IsExecuteDeepRenderDone()38     bool IsExecuteDeepRenderDone() const
39     {
40         return executeDeepRenderDone_;
41     }
42 
MarkIsExecuteDeepRenderDone(bool executeDeepRenderDone)43     void MarkIsExecuteDeepRenderDone(bool executeDeepRenderDone)
44     {
45         executeDeepRenderDone_ = executeDeepRenderDone;
46     }
47 
ExecuteDeepRender()48     RefPtr<UINode> ExecuteDeepRender()
49     {
50         CHECK_NULL_RETURN(deepRenderFunc_, nullptr);
51         executeDeepRenderDone_ = true;
52         return deepRenderFunc_();
53     }
54 
55 private:
56     DeepRenderFunc deepRenderFunc_;
57     bool executeDeepRenderDone_ = false;
58 
59     ACE_DISALLOW_COPY_AND_MOVE(ShallowBuilder);
60 };
61 
62 } // namespace OHOS::Ace::NG
63 
64 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_SYNTAX_SHALLOW_BUILDER_H
65