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,const std::unordered_map<std::string,RefPtr<NG::UINode>> & cachedItems)58 std::pair<std::string, RefPtr<NG::UINode>> OnGetChildByIndex( 59 int32_t index, const std::unordered_map<std::string, RefPtr<NG::UINode>>& 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; 75 return info; 76 } 77 78 NG::ScopedViewStackProcessor scopedViewStackProcessor; 79 auto* viewStack = NG::ViewStackProcessor::GetInstance(); 80 if (parentView_) { 81 parentView_->MarkLazyForEachProcess(key); 82 } 83 viewStack->PushKey(key); 84 itemGenFunc_->Call(JSRef<JSObject>(), 2, params); 85 viewStack->PopKey(); 86 if (parentView_) { 87 parentView_->ResetLazyForEachProcess(); 88 } 89 info.first = key; 90 info.second = viewStack->Finish(); 91 return info; 92 } 93 ReleaseChildGroupById(const std::string & id)94 void ReleaseChildGroupById(const std::string& id) override 95 { 96 JSLazyForEachActuator::ReleaseChildGroupByComposedId(id); 97 } 98 RegisterDataChangeListener(const RefPtr<V2::DataChangeListener> & listener)99 void RegisterDataChangeListener(const RefPtr<V2::DataChangeListener>& listener) override 100 { 101 JSLazyForEachActuator::RegisterListener(listener); 102 } 103 UnregisterDataChangeListener(const RefPtr<V2::DataChangeListener> & listener)104 void UnregisterDataChangeListener(const RefPtr<V2::DataChangeListener>& listener) override 105 { 106 JSLazyForEachActuator::UnregisterListener(listener); 107 } 108 109 ACE_DISALLOW_COPY_AND_MOVE(JSLazyForEachBuilder); 110 }; 111 112 } // namespace OHOS::Ace::Framework 113 114 #endif // FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_VIEW_JS_LAZY_FOREACH_BUILDER_H 115