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 FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_VIEW_JS_LAZY_FOREACH_BUILDER_H 17 #define FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_VIEW_JS_LAZY_FOREACH_BUILDER_H 18 19 #include <functional> 20 #include <set> 21 #include <string> 22 23 #include "base/memory/ace_type.h" 24 #include "bridge/declarative_frontend/jsview/js_lazy_foreach_actuator.h" 25 #include "bridge/declarative_frontend/jsview/js_view.h" 26 #include "core/components_ng/base/view_stack_processor.h" 27 #include "core/components_ng/syntax/lazy_for_each_builder.h" 28 29 namespace OHOS::Ace::Framework { 30 31 class JSLazyForEachBuilder : public NG::LazyForEachBuilder, public JSLazyForEachActuator { 32 DECLARE_ACE_TYPE(JSLazyForEachBuilder, NG::LazyForEachBuilder, JSLazyForEachActuator); 33 34 public: 35 JSLazyForEachBuilder() = default; 36 ~JSLazyForEachBuilder() override = default; 37 OnGetTotalCount()38 int32_t OnGetTotalCount() override 39 { 40 return GetTotalIndexCount(); 41 } 42 OnExpandChildrenOnInitialInNG()43 void OnExpandChildrenOnInitialInNG() override 44 { 45 auto totalIndex = GetTotalIndexCount(); 46 auto* stack = NG::ViewStackProcessor::GetInstance(); 47 JSRef<JSVal> params[2]; 48 for (auto index = 0; index < totalIndex; index++) { 49 params[0] = CallJSFunction(getDataFunc_, dataSourceObj_, index); 50 params[1] = JSRef<JSVal>::Make(ToJSValue(index)); 51 std::string key = keyGenFunc_(params[0], index); 52 stack->PushKey(key); 53 itemGenFunc_->Call(JSRef<JSObject>(), 2, params); 54 stack->PopKey(); 55 } 56 } 57 OnGetChildByIndex(int32_t index,std::unordered_map<std::string,NG::LazyForEachCacheChild> & cachedItems)58 std::pair<std::string, RefPtr<NG::UINode>> OnGetChildByIndex( 59 int32_t index, std::unordered_map<std::string, NG::LazyForEachCacheChild>& cachedItems) override 60 { 61 std::pair<std::string, RefPtr<NG::UINode>> info; 62 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(executionContext_, info); 63 if (getDataFunc_.IsEmpty()) { 64 return info; 65 } 66 67 JSRef<JSVal> params[2]; 68 params[0] = CallJSFunction(getDataFunc_, dataSourceObj_, index); 69 params[1] = JSRef<JSVal>::Make(ToJSValue(index)); 70 std::string key = keyGenFunc_(params[0], index); 71 auto cachedIter = cachedItems.find(key); 72 if (cachedIter != cachedItems.end()) { 73 info.first = key; 74 info.second = cachedIter->second.second; 75 cachedItems.erase(cachedIter); 76 return info; 77 } 78 79 NG::ScopedViewStackProcessor scopedViewStackProcessor; 80 auto* viewStack = NG::ViewStackProcessor::GetInstance(); 81 if (parentView_) { 82 parentView_->MarkLazyForEachProcess(key); 83 } 84 viewStack->PushKey(key); 85 itemGenFunc_->Call(JSRef<JSObject>(), 2, params); 86 viewStack->PopKey(); 87 if (parentView_) { 88 parentView_->ResetLazyForEachProcess(); 89 } 90 info.first = key; 91 info.second = viewStack->Finish(); 92 return info; 93 } 94 ReleaseChildGroupById(const std::string & id)95 void ReleaseChildGroupById(const std::string& id) override 96 { 97 JSLazyForEachActuator::ReleaseChildGroupByComposedId(id); 98 } 99 RegisterDataChangeListener(const RefPtr<V2::DataChangeListener> & listener)100 void RegisterDataChangeListener(const RefPtr<V2::DataChangeListener>& listener) override 101 { 102 JSLazyForEachActuator::RegisterListener(listener); 103 } 104 UnregisterDataChangeListener(const RefPtr<V2::DataChangeListener> & listener)105 void UnregisterDataChangeListener(const RefPtr<V2::DataChangeListener>& listener) override 106 { 107 JSLazyForEachActuator::UnregisterListener(listener); 108 } 109 110 ACE_DISALLOW_COPY_AND_MOVE(JSLazyForEachBuilder); 111 }; 112 113 } // namespace OHOS::Ace::Framework 114 115 #endif // FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_VIEW_JS_LAZY_FOREACH_BUILDER_H 116