• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.unregisterElmtIdsFromIViews: 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.unregisterElmtIdsFromIViews();
53}
54
55class UINodeRegisterProxy {
56    public static readonly notRecordingDependencies : number = -1;
57    public static readonly monitorIllegalV1V2StateAccess : number = -2;
58
59    public static obtainDeletedElmtIds(): void {
60        stateMgmtConsole.debug(`UINodeRegisterProxy. static obtainDeletedElmtIds:`);
61        if ((!UINodeRegisterProxy.instance_.obtainDeletedElmtIds) || typeof UINodeRegisterProxy.instance_.obtainDeletedElmtIds !== 'function') {
62            stateMgmtConsole.error(`UINodeRegisterProxy obtainDeletedElmtIds is not a function: ${UINodeRegisterProxy.instance_.obtainDeletedElmtIds}.` );
63        } else {
64            UINodeRegisterProxy.instance_.obtainDeletedElmtIds();
65        }
66    }
67
68    // FIXME unregisterElmtIdsFromIViews needs adaptation
69    public static unregisterElmtIdsFromIViews(): void {
70        stateMgmtConsole.debug('UINodeRegisterProxy.unregisterElmtIdsFromIViews elmtIds');
71        UINodeRegisterProxy.instance_.unregisterElmtIdsFromIViews();
72    }
73
74    // unregisters all the received removedElements in func parameter
75    public static unregisterRemovedElmtsFromViewPUs(removedElements: Array<number>): void {
76        stateMgmtConsole.debug(`UINodeRegisterProxy.unregisterRemovedElmtsFromViewPUs elmtIds ${removedElements}`);
77        UINodeRegisterProxy.instance_.populateRemoveElementInfo(removedElements);
78        UINodeRegisterProxy.instance_.unregisterElmtIdsFromIViews();
79    }
80
81    private populateRemoveElementInfo(removedElements: Array<number>) {
82        for (const elmtId of removedElements) {
83            this.removeElementsInfo_.push(elmtId);
84        }
85    }
86
87    /* just get the remove items from the native side
88    */
89    private obtainDeletedElmtIds(): void {
90        stateMgmtConsole.debug('UINodeRegisterProxy.obtainDeletedElmtIds: ');
91        let removedElementsInfo = new Array<number>();
92        ViewStackProcessor.moveDeletedElmtIds(removedElementsInfo);
93        stateMgmtConsole.debug(`   ... ${removedElementsInfo.length} elmtIds newly obtained from ElementRegister: ${JSON.stringify(removedElementsInfo)} .`);
94        this.removeElementsInfo_ = removedElementsInfo;
95    }
96
97    public unregisterElmtIdsFromIViews(): void {
98        stateMgmtConsole.debug(`${this.removeElementsInfo_.length} elmtIds newly obtained from ElementRegister: ${JSON.stringify(this.removeElementsInfo_)} .`);
99
100        if (this.removeElementsInfo_.length === 0) {
101            stateMgmtConsole.debug(`${this.removeElementsInfo_.length} elmtIds needs to purgeDelete. } .`);
102            return;
103        }
104        let owningView : ViewBuildNodeBase | undefined;
105        this.removeElementsInfo_.forEach((elmtId: number) => {
106            const owningViewPUWeak : WeakRef<ViewBuildNodeBase> | undefined = UINodeRegisterProxy.ElementIdToOwningViewPU_.get(elmtId);
107            if (owningViewPUWeak !== undefined) {
108                owningView = owningViewPUWeak.deref();
109                if (owningView) {
110                    owningView.purgeDeleteElmtId(elmtId);
111                } else {
112                    stateMgmtConsole.debug(`elmtIds ${elmtId} has not been removed because of failure of updating the weakptr of viewpu. Internal error!.`);
113                }
114            } else {
115                stateMgmtConsole.debug(`elmtIds ${elmtId} cannot find its owning ViewPU, maybe this ViewPu has already been aboutToBeDeleted. Internal error!`);
116            }
117
118            ObserveV2.getObserve().clearBinding(elmtId);
119        })
120
121        this.removeElementsInfo_.length = 0;
122    }
123
124    public static cleanUpDeadReferences(): void {
125        stateMgmtConsole.debug('UINodeRegisterProxy.cleanUpDeadReferences');
126        ObserveV2.getObserve().cleanUpDeadReferences();
127    }
128
129    public static instance_: UINodeRegisterProxy = new UINodeRegisterProxy();
130    public removeElementsInfo_: Array<number> = new Array<number>();
131    public static ElementIdToOwningViewPU_: Map<number, WeakRef<ViewBuildNodeBase>> = new Map<number, WeakRef<ViewBuildNodeBase>>();
132}
133