1/* 2 * Copyright (c) 2023 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 17/* UINodeRegisterProxy singeleton class 18 19Acts on behave on ElementRegister to hold elmtIds of deleted UINodes 20until these get unregistered from a ViewPU. 21 22Two flows inside the framework that involve UINodeRegisterProxy: 23 24Flow A: 251. UINode Destructor calls ElementRegister.RemoveItem(UINode's elmtId) - processing stops: 26 272. Some time later on next re-render of a ViewPU: 283. ViewPU.purgeDeletedElmtIds calls C+++ UINodeRegisterProxy.obtainElementIds(), 294. UINodeRegisterProxy.obtainElementIds() calls C++ ElementRegister::MoveRemovedItems to move elmtIds of deleted UINodes UINodeRegisterProxy 30 (those added by RemoveItems(elmtId) in step 1). 315. UINodeRegisterProxy.unregisterElmtIdsFromViewPUs: unregister the removedElementIds from the all the viewpus: 32 1) purge the update function in viewpu 33 2) purge the dependent element if from statevariable 34 35Flow B: 361. CustomNode destructor calls deleteFunction calls ViewPU.aboutToBeDeleted 37 note that CustomNode destructor may get called before child UINodes' destructor. 38 1) unregister its own element ids 39 2) call UINodeRegisterProxy.obtainElementIds() to get the removed elementIds from cpp and unregister from all the viewpus. 40 it may make the first viewpu aboutToBeDeleted execution time longer than before, but for rest viewpu may be faster. 41 It's function equals with purgeDeletedElmtIdsRecursively, so it is not necessary to execute purgeDeletedElmtIds for all its child recursively. 42 432. some time later or when idle: PipelineContext::OnIdle will CallJSCleanUpIdleTaskFunc to do the clean up for the removed elements which have not unregisted 44 from the stateMgmt side. 45*/ 46 47type RemovedElementInfo = { elmtId : number, tag : string }; 48// defined a globle function to clean up the removeItems when idle 49function uiNodeCleanUpIdleTask(): void { 50 stateMgmtConsole.debug(`UINodeRegisterProxy. static uiNodeCleanUpIdleTask:`); 51 UINodeRegisterProxy.obtainDeletedElmtIds(); 52 UINodeRegisterProxy.unregisterElmtIdsFromViewPUs(); 53} 54 55class UINodeRegisterProxy { 56 public static obtainDeletedElmtIds(): void { 57 stateMgmtConsole.debug(`UINodeRegisterProxy. static obtainDeletedElmtIds:`); 58 UINodeRegisterProxy.instance_.obtainDeletedElmtIds(); 59 } 60 61 public static unregisterElmtIdsFromViewPUs(): void { 62 stateMgmtConsole.debug('UINodeRegisterProxy.unregisterElmtIdsFromViewPUs elmtIds'); 63 UINodeRegisterProxy.instance_.unregisterElmtIdsFromViewPUs(); 64 } 65 66 /* just get the remove items from the native side 67 */ 68 private obtainDeletedElmtIds(): void { 69 stateMgmtConsole.debug('UINodeRegisterProxy.obtainDeletedElmtIds: '); 70 let removedElementsInfo = new Array<RemovedElementInfo>(); 71 ViewStackProcessor.moveDeletedElmtIds(removedElementsInfo); 72 stateMgmtConsole.debug(` ... ${removedElementsInfo.length} elmtIds newly obtained from ElementRegister: ${JSON.stringify(removedElementsInfo)} .`); 73 this.removeElementsInfo_ = removedElementsInfo; 74 } 75 76 public unregisterElmtIdsFromViewPUs(): void { 77 stateMgmtConsole.debug(`${this.removeElementsInfo_.length} elmtIds newly obtained from ElementRegister: ${JSON.stringify(this.removeElementsInfo_)} .`); 78 79 if (this.removeElementsInfo_.length == 0) { 80 stateMgmtConsole.debug(`${this.removeElementsInfo_.length} elmtIds needs to purgeDelete. } .`); 81 return; 82 } 83 let owningView : ViewPU | undefined; 84 this.removeElementsInfo_.forEach((rmElmtInfo : RemovedElementInfo) => { 85 const owningViewPUWeak : WeakRef<ViewPU> = UINodeRegisterProxy.ElementIdToOwningViewPU_.get(rmElmtInfo.elmtId ); 86 if (owningViewPUWeak != undefined) { 87 owningView = owningViewPUWeak.deref(); 88 if (owningView) { 89 owningView.purgeDeleteElmtId(rmElmtInfo.elmtId); 90 } else { 91 stateMgmtConsole.warn(`elmtIds ${rmElmtInfo.elmtId} tag: ${rmElmtInfo.tag} has not been removed because of failure of updating the weakptr of viewpu. Internal error!.`); 92 } 93 } else { 94 stateMgmtConsole.warn(`elmtIds ${rmElmtInfo.elmtId} tag: ${rmElmtInfo.tag} cannot find its owning viewpu, maybe this viewpu has already been abouttobedeleted. Internal error!`) 95 } 96 }) 97 98 this.removeElementsInfo_.length = 0; 99 } 100 101 public static instance_: UINodeRegisterProxy = new UINodeRegisterProxy(); 102 public removeElementsInfo_: Array<RemovedElementInfo> = new Array<RemovedElementInfo>(); 103 public static ElementIdToOwningViewPU_: Map<number, WeakRef<ViewPU>> = new Map<number, WeakRef<ViewPU>>(); 104} 105