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 16exports.source = ` 17import { ClassB as ClassA } from './test/pages/import@Observed' 18@Component 19struct ViewA { 20 label: string = 'ViewA1' 21 @ObjectLink a: ClassA 22 23 build() { 24 Row() { 25 Button('ViewA'+ JSON.stringify(this.label) + 'this.a.c='+JSON.stringify(this.a.c)) 26 .onClick(() => { 27 this.a.c += 1 28 }) 29 }.margin({ top: 10 }) 30 } 31} 32 33@Entry 34@Component 35struct ViewB { 36 @State arrA: ClassA[] = [new ClassA(0), new ClassA(0)] 37 38 build() { 39 Column() { 40 ForEach(this.arrA, (item) => { 41 ViewA({ label: JSON.stringify(item.id), a: item }) 42 }, (item) => item.id.toString()) 43 ViewA({ label: JSON.stringify(this.arrA[0]), a: this.arrA[0] }) 44 ViewA({ label: JSON.stringify(this.arrA[this.arrA.length - 1]), a: this.arrA[this.arrA.length - 1] }) 45 46 Button('ViewB: reset array') 47 .margin({ top: 10 }) 48 .onClick(() => { 49 this.arrA = [new ClassA(0), new ClassA(0)] 50 }) 51 Button('ViewB: push') 52 .margin({ top: 10 }) 53 .onClick(() => { 54 this.arrA.push(new ClassA(0)) 55 }) 56 Button('ViewB: shift') 57 .margin({ top: 10 }) 58 .onClick(() => { 59 this.arrA.shift() 60 }) 61 }.width('100%') 62 } 63} 64` 65 66exports.expectResult = 67`"use strict"; 68Object.defineProperty(exports, "__esModule", { value: true }); 69const import_Observed_1 = require("./test/pages/import@Observed"); 70class ViewA extends ViewPU { 71 constructor(parent, params, __localStorage, elmtId = -1, paramsLambda = undefined, extraInfo) { 72 super(parent, __localStorage, elmtId, extraInfo); 73 if (typeof paramsLambda === "function") { 74 this.paramsGenerator_ = paramsLambda; 75 } 76 this.label = 'ViewA1'; 77 this.__a = new SynchedPropertyNesedObjectPU(params.a, this, "a"); 78 this.setInitiallyProvidedValue(params); 79 } 80 setInitiallyProvidedValue(params) { 81 if (params.label !== undefined) { 82 this.label = params.label; 83 } 84 this.__a.set(params.a); 85 } 86 updateStateVars(params) { 87 this.__a.set(params.a); 88 } 89 purgeVariableDependenciesOnElmtId(rmElmtId) { 90 this.__a.purgeDependencyOnElmtId(rmElmtId); 91 } 92 aboutToBeDeleted() { 93 this.__a.aboutToBeDeleted(); 94 SubscriberManager.Get().delete(this.id__()); 95 this.aboutToBeDeletedInternal(); 96 } 97 get a() { 98 return this.__a.get(); 99 } 100 initialRender() { 101 this.observeComponentCreation2((elmtId, isInitialRender) => { 102 Row.create(); 103 Row.margin({ top: 10 }); 104 }, Row); 105 this.observeComponentCreation2((elmtId, isInitialRender) => { 106 Button.createWithLabel('ViewA' + JSON.stringify(this.label) + 'this.a.c=' + JSON.stringify(this.a.c)); 107 Button.onClick(() => { 108 this.a.c += 1; 109 }); 110 }, Button); 111 Button.pop(); 112 Row.pop(); 113 } 114 rerender() { 115 this.updateDirtyElements(); 116 } 117} 118class ViewB extends ViewPU { 119 constructor(parent, params, __localStorage, elmtId = -1, paramsLambda = undefined, extraInfo) { 120 super(parent, __localStorage, elmtId, extraInfo); 121 if (typeof paramsLambda === "function") { 122 this.paramsGenerator_ = paramsLambda; 123 } 124 this.__arrA = new ObservedPropertyObjectPU([new import_Observed_1.ClassB(0), new import_Observed_1.ClassB(0)], this, "arrA"); 125 this.setInitiallyProvidedValue(params); 126 } 127 setInitiallyProvidedValue(params) { 128 if (params.arrA !== undefined) { 129 this.arrA = params.arrA; 130 } 131 } 132 updateStateVars(params) { 133 } 134 purgeVariableDependenciesOnElmtId(rmElmtId) { 135 this.__arrA.purgeDependencyOnElmtId(rmElmtId); 136 } 137 aboutToBeDeleted() { 138 this.__arrA.aboutToBeDeleted(); 139 SubscriberManager.Get().delete(this.id__()); 140 this.aboutToBeDeletedInternal(); 141 } 142 get arrA() { 143 return this.__arrA.get(); 144 } 145 set arrA(newValue) { 146 this.__arrA.set(newValue); 147 } 148 initialRender() { 149 this.observeComponentCreation2((elmtId, isInitialRender) => { 150 Column.create(); 151 Column.width('100%'); 152 }, Column); 153 this.observeComponentCreation2((elmtId, isInitialRender) => { 154 ForEach.create(); 155 const forEachItemGenFunction = _item => { 156 const item = _item; 157 { 158 this.observeComponentCreation2((elmtId, isInitialRender) => { 159 if (isInitialRender) { 160 let componentCall = new ViewA(this, { label: JSON.stringify(item.id), a: item }, undefined, elmtId, () => { }, { page: "import@Observed.ets", line: 26 }); 161 ViewPU.create(componentCall); 162 let paramsLambda = () => { 163 return { 164 label: JSON.stringify(item.id), 165 a: item 166 }; 167 }; 168 componentCall.paramsGenerator_ = paramsLambda; 169 } 170 else { 171 this.updateStateVarsOfChildByElmtId(elmtId, { 172 a: item 173 }); 174 } 175 }, { name: "ViewA" }); 176 } 177 }; 178 this.forEachUpdateFunction(elmtId, this.arrA, forEachItemGenFunction, (item) => item.id.toString(), false, false); 179 }, ForEach); 180 ForEach.pop(); 181 { 182 this.observeComponentCreation2((elmtId, isInitialRender) => { 183 if (isInitialRender) { 184 let componentCall = new ViewA(this, { label: JSON.stringify(this.arrA[0]), a: this.arrA[0] }, undefined, elmtId, () => { }, { page: "import@Observed.ets", line: 28 }); 185 ViewPU.create(componentCall); 186 let paramsLambda = () => { 187 return { 188 label: JSON.stringify(this.arrA[0]), 189 a: this.arrA[0] 190 }; 191 }; 192 componentCall.paramsGenerator_ = paramsLambda; 193 } 194 else { 195 this.updateStateVarsOfChildByElmtId(elmtId, { 196 a: this.arrA[0] 197 }); 198 } 199 }, { name: "ViewA" }); 200 } 201 { 202 this.observeComponentCreation2((elmtId, isInitialRender) => { 203 if (isInitialRender) { 204 let componentCall = new ViewA(this, { label: JSON.stringify(this.arrA[this.arrA.length - 1]), a: this.arrA[this.arrA.length - 1] }, undefined, elmtId, () => { }, { page: "import@Observed.ets", line: 29 }); 205 ViewPU.create(componentCall); 206 let paramsLambda = () => { 207 return { 208 label: JSON.stringify(this.arrA[this.arrA.length - 1]), 209 a: this.arrA[this.arrA.length - 1] 210 }; 211 }; 212 componentCall.paramsGenerator_ = paramsLambda; 213 } 214 else { 215 this.updateStateVarsOfChildByElmtId(elmtId, { 216 a: this.arrA[this.arrA.length - 1] 217 }); 218 } 219 }, { name: "ViewA" }); 220 } 221 this.observeComponentCreation2((elmtId, isInitialRender) => { 222 Button.createWithLabel('ViewB: reset array'); 223 Button.margin({ top: 10 }); 224 Button.onClick(() => { 225 this.arrA = [new import_Observed_1.ClassB(0), new import_Observed_1.ClassB(0)]; 226 }); 227 }, Button); 228 Button.pop(); 229 this.observeComponentCreation2((elmtId, isInitialRender) => { 230 Button.createWithLabel('ViewB: push'); 231 Button.margin({ top: 10 }); 232 Button.onClick(() => { 233 this.arrA.push(new import_Observed_1.ClassB(0)); 234 }); 235 }, Button); 236 Button.pop(); 237 this.observeComponentCreation2((elmtId, isInitialRender) => { 238 Button.createWithLabel('ViewB: shift'); 239 Button.margin({ top: 10 }); 240 Button.onClick(() => { 241 this.arrA.shift(); 242 }); 243 }, Button); 244 Button.pop(); 245 Column.pop(); 246 } 247 rerender() { 248 this.updateDirtyElements(); 249 } 250} 251ViewStackProcessor.StartGetAccessRecordingFor(ViewStackProcessor.AllocateNewElmetIdForNextComponent()); 252loadDocument(new ViewB(undefined, {})); 253ViewStackProcessor.StopGetAccessRecording(); 254` 255