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, param?: Object) => 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 private isPending_: boolean = false; 54 private isChanged_: boolean = false; 55 56 constructor(params: { updateFunc: UpdateFunc, classObject?: UIClassObject, node?: ArkComponent }) { 57 this.updateFunc_ = params.updateFunc; 58 this.classObject_ = params.classObject; 59 this.node_ = params.node; 60 } 61 62 public getUpdateFunc(): UpdateFunc | undefined { 63 return this.updateFunc_; 64 } 65 66 public getComponentClass(): UIClassObject | undefined { 67 return this.classObject_; 68 } 69 70 public getComponentName(): string { 71 return (this.classObject_ && ('name' in this.classObject_)) ? Reflect.get(this.classObject_, 'name') as string : 'unspecified UINode'; 72 } 73 74 public getPopFunc(): () => void { 75 return (this.classObject_ && 'pop' in this.classObject_) ? this.classObject_.pop! : (): void => { }; 76 } 77 78 public getNode(): ArkComponent | undefined { 79 return this.node_; 80 } 81 82 public setNode(node: ArkComponent | undefined): void { 83 this.node_ = node; 84 } 85 86 public isPending(): boolean { 87 return this.isPending_; 88 } 89 90 public setPending(isPending: boolean): void { 91 this.isPending_ = isPending; 92 } 93 94 public isChanged(): boolean { 95 return this.isChanged_; 96 } 97 98 public setIsChanged(isChanged: boolean): void { 99 this.isChanged_ = isChanged; 100 } 101 } // UpdateFuncRecord 102 103 104class UpdateFuncsByElmtId { 105 106 private map_ = new Map<number, UpdateFuncRecord>(); 107 108 public delete(elmtId: number): boolean { 109 return this.map_.delete(elmtId); 110 } 111 112 public set(elmtId: number, params: UpdateFunc | { updateFunc: UpdateFunc, classObject?: UIClassObject, node?: ArkComponent }): void { 113 (typeof params === 'object') ? 114 this.map_.set(elmtId, new UpdateFuncRecord(params)) : 115 this.map_.set(elmtId, new UpdateFuncRecord({ updateFunc: params as UpdateFunc })); 116 } 117 118 public get(elmtId: number): UpdateFuncRecord | undefined { 119 return this.map_.get(elmtId); 120 } 121 122 public has(elmtId: number): boolean { 123 return this.map_.has(elmtId); 124 } 125 126 public keys(): IterableIterator<number> { 127 return this.map_.keys(); 128 } 129 130 public clear(): void { 131 return this.map_.clear(); 132 } 133 134 public get size(): number { 135 return this.map_.size; 136 } 137 138 public forEach(callbackfn: (value: UpdateFuncRecord, key: number, map: Map<number, UpdateFuncRecord>) => void): void { 139 this.map_.forEach(callbackfn); 140 } 141 142 // dump info about known elmtIds to a string 143 // use function only for debug output and DFX. 144 public debugInfoRegisteredElmtIds(): string { 145 let result: string = ''; 146 let sepa: string = ''; 147 this.map_.forEach((value: UpdateFuncRecord, elmtId: number) => { 148 result += `${sepa}${value.getComponentName()}[${elmtId}]`; 149 sepa = ', '; 150 }); 151 return result; 152 } 153 154 public debugInfoElmtId(elmtId: number): string { 155 const updateFuncEntry = this.map_.get(elmtId); 156 return updateFuncEntry ? `${updateFuncEntry!.getComponentName()}[${elmtId}]` : `unknown component type[${elmtId}]`; 157 } 158 } // class UpdateFuncByElmtId 159