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/** 17 * 18 * This file includes only framework internal classes and functions 19 * non are part of SDK. Do not access from app. 20 * 21 * Helper classes used by ViewPU and ViewV2 22 * 23 */ 24 25/** 26 * WeakRef 27 * ref to an Object that does not prevent the Object from getting GC'ed 28 * current version of tsc does not know about WeakRef 29 * but Ark runtime supports it 30 * 31 */ 32declare class WeakRef<T extends Object> { 33 constructor(o: T); 34 deref(): T; 35 } 36 37 declare class DumpLog { 38 static print(depth: number, content: string): void; 39 } 40 41 // function type of partial update function 42 type UpdateFunc = (elmtId: number, isFirstRender: boolean) => void; 43 type UIClassObject = { prototype: Object, pop?: () => void }; 44 45 // UpdateFuncRecord: misc framework-internal info related to updating of a UINode C++ object 46 // that TS side needs to know. 47 // updateFunc_ lambda function to update the UINode 48 // JS interface class reference (it only has static functions) 49 class UpdateFuncRecord { 50 private updateFunc_: UpdateFunc; 51 private classObject_: UIClassObject; 52 private node_?: ArkComponent; 53 54 constructor(params: { updateFunc: UpdateFunc, classObject?: UIClassObject, node?: ArkComponent }) { 55 this.updateFunc_ = params.updateFunc; 56 this.classObject_ = params.classObject; 57 this.node_ = params.node; 58 } 59 60 public getUpdateFunc(): UpdateFunc | undefined { 61 return this.updateFunc_; 62 } 63 64 public getComponentClass(): UIClassObject | undefined { 65 return this.classObject_; 66 } 67 68 public getComponentName(): string { 69 return (this.classObject_ && ('name' in this.classObject_)) ? Reflect.get(this.classObject_, 'name') as string : 'unspecified UINode'; 70 } 71 72 public getPopFunc(): () => void { 73 return (this.classObject_ && 'pop' in this.classObject_) ? this.classObject_.pop! : (): void => { }; 74 } 75 76 public getNode(): ArkComponent | undefined { 77 return this.node_; 78 } 79 80 public setNode(node: ArkComponent | undefined): void { 81 this.node_ = node; 82 } 83 } // UpdateFuncRecord 84 85 86class UpdateFuncsByElmtId { 87 88 private map_ = new Map<number, UpdateFuncRecord>(); 89 90 public delete(elmtId: number): boolean { 91 return this.map_.delete(elmtId); 92 } 93 94 public set(elmtId: number, params: UpdateFunc | { updateFunc: UpdateFunc, classObject?: UIClassObject, node?: ArkComponent }): void { 95 (typeof params === 'object') ? 96 this.map_.set(elmtId, new UpdateFuncRecord(params)) : 97 this.map_.set(elmtId, new UpdateFuncRecord({ updateFunc: params as UpdateFunc })); 98 } 99 100 public get(elmtId: number): UpdateFuncRecord | undefined { 101 return this.map_.get(elmtId); 102 } 103 104 public has(elmtId: number): boolean { 105 return this.map_.has(elmtId); 106 } 107 108 public keys(): IterableIterator<number> { 109 return this.map_.keys(); 110 } 111 112 public clear(): void { 113 return this.map_.clear(); 114 } 115 116 public get size(): number { 117 return this.map_.size; 118 } 119 120 public forEach(callbackfn: (value: UpdateFuncRecord, key: number, map: Map<number, UpdateFuncRecord>) => void): void { 121 this.map_.forEach(callbackfn); 122 } 123 124 // dump info about known elmtIds to a string 125 // use function only for debug output and DFX. 126 public debugInfoRegisteredElmtIds(): string { 127 let result: string = ''; 128 let sepa: string = ''; 129 this.map_.forEach((value: UpdateFuncRecord, elmtId: number) => { 130 result += `${sepa}${value.getComponentName()}[${elmtId}]`; 131 sepa = ', '; 132 }); 133 return result; 134 } 135 136 public debugInfoElmtId(elmtId: number): string { 137 const updateFuncEntry = this.map_.get(elmtId); 138 return updateFuncEntry ? `${updateFuncEntry!.getComponentName()}[${elmtId}]` : `'unknown component type'[${elmtId}]`; 139 } 140 } // class UpdateFuncByElmtId 141