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) { 72 super(parent, __localStorage, elmtId); 73 this.label = 'ViewA1'; 74 this.__a = new SynchedPropertyNesedObjectPU(params.a, this, "a"); 75 this.setInitiallyProvidedValue(params); 76 } 77 setInitiallyProvidedValue(params) { 78 if (params.label !== undefined) { 79 this.label = params.label; 80 } 81 this.__a.set(params.a); 82 } 83 updateStateVars(params) { 84 this.__a.set(params.a); 85 } 86 purgeVariableDependenciesOnElmtId(rmElmtId) { 87 this.__a.purgeDependencyOnElmtId(rmElmtId); 88 } 89 aboutToBeDeleted() { 90 this.__a.aboutToBeDeleted(); 91 SubscriberManager.Get().delete(this.id__()); 92 this.aboutToBeDeletedInternal(); 93 } 94 get a() { 95 return this.__a.get(); 96 } 97 initialRender() { 98 this.observeComponentCreation((elmtId, isInitialRender) => { 99 ViewStackProcessor.StartGetAccessRecordingFor(elmtId); 100 Row.create(); 101 Row.margin({ top: 10 }); 102 if (!isInitialRender) { 103 Row.pop(); 104 } 105 ViewStackProcessor.StopGetAccessRecording(); 106 }); 107 this.observeComponentCreation((elmtId, isInitialRender) => { 108 ViewStackProcessor.StartGetAccessRecordingFor(elmtId); 109 Button.createWithLabel('ViewA' + JSON.stringify(this.label) + 'this.a.c=' + JSON.stringify(this.a.c)); 110 Button.onClick(() => { 111 this.a.c += 1; 112 }); 113 if (!isInitialRender) { 114 Button.pop(); 115 } 116 ViewStackProcessor.StopGetAccessRecording(); 117 }); 118 Button.pop(); 119 Row.pop(); 120 } 121 rerender() { 122 this.updateDirtyElements(); 123 } 124} 125class ViewB extends ViewPU { 126 constructor(parent, params, __localStorage, elmtId = -1) { 127 super(parent, __localStorage, elmtId); 128 this.__arrA = new ObservedPropertyObjectPU([new import_Observed_1.ClassB(0), new import_Observed_1.ClassB(0)], this, "arrA"); 129 this.setInitiallyProvidedValue(params); 130 } 131 setInitiallyProvidedValue(params) { 132 if (params.arrA !== undefined) { 133 this.arrA = params.arrA; 134 } 135 } 136 updateStateVars(params) { 137 } 138 purgeVariableDependenciesOnElmtId(rmElmtId) { 139 this.__arrA.purgeDependencyOnElmtId(rmElmtId); 140 } 141 aboutToBeDeleted() { 142 this.__arrA.aboutToBeDeleted(); 143 SubscriberManager.Get().delete(this.id__()); 144 this.aboutToBeDeletedInternal(); 145 } 146 get arrA() { 147 return this.__arrA.get(); 148 } 149 set arrA(newValue) { 150 this.__arrA.set(newValue); 151 } 152 initialRender() { 153 this.observeComponentCreation((elmtId, isInitialRender) => { 154 ViewStackProcessor.StartGetAccessRecordingFor(elmtId); 155 Column.create(); 156 Column.width('100%'); 157 if (!isInitialRender) { 158 Column.pop(); 159 } 160 ViewStackProcessor.StopGetAccessRecording(); 161 }); 162 this.observeComponentCreation((elmtId, isInitialRender) => { 163 ViewStackProcessor.StartGetAccessRecordingFor(elmtId); 164 ForEach.create(); 165 const forEachItemGenFunction = _item => { 166 const item = _item; 167 { 168 this.observeComponentCreation((elmtId, isInitialRender) => { 169 ViewStackProcessor.StartGetAccessRecordingFor(elmtId); 170 if (isInitialRender) { 171 ViewPU.create(new ViewA(this, { label: JSON.stringify(item.id), a: item }, undefined, elmtId)); 172 } 173 else { 174 this.updateStateVarsOfChildByElmtId(elmtId, { 175 a: item 176 }); 177 } 178 ViewStackProcessor.StopGetAccessRecording(); 179 }); 180 } 181 }; 182 this.forEachUpdateFunction(elmtId, this.arrA, forEachItemGenFunction, (item) => item.id.toString(), false, false); 183 if (!isInitialRender) { 184 ForEach.pop(); 185 } 186 ViewStackProcessor.StopGetAccessRecording(); 187 }); 188 ForEach.pop(); 189 { 190 this.observeComponentCreation((elmtId, isInitialRender) => { 191 ViewStackProcessor.StartGetAccessRecordingFor(elmtId); 192 if (isInitialRender) { 193 ViewPU.create(new ViewA(this, { label: JSON.stringify(this.arrA[0]), a: this.arrA[0] }, undefined, elmtId)); 194 } 195 else { 196 this.updateStateVarsOfChildByElmtId(elmtId, { 197 a: this.arrA[0] 198 }); 199 } 200 ViewStackProcessor.StopGetAccessRecording(); 201 }); 202 } 203 { 204 this.observeComponentCreation((elmtId, isInitialRender) => { 205 ViewStackProcessor.StartGetAccessRecordingFor(elmtId); 206 if (isInitialRender) { 207 ViewPU.create(new ViewA(this, { label: JSON.stringify(this.arrA[this.arrA.length - 1]), a: this.arrA[this.arrA.length - 1] }, undefined, elmtId)); 208 } 209 else { 210 this.updateStateVarsOfChildByElmtId(elmtId, { 211 a: this.arrA[this.arrA.length - 1] 212 }); 213 } 214 ViewStackProcessor.StopGetAccessRecording(); 215 }); 216 } 217 this.observeComponentCreation((elmtId, isInitialRender) => { 218 ViewStackProcessor.StartGetAccessRecordingFor(elmtId); 219 Button.createWithLabel('ViewB: reset array'); 220 Button.margin({ top: 10 }); 221 Button.onClick(() => { 222 this.arrA = [new import_Observed_1.ClassB(0), new import_Observed_1.ClassB(0)]; 223 }); 224 if (!isInitialRender) { 225 Button.pop(); 226 } 227 ViewStackProcessor.StopGetAccessRecording(); 228 }); 229 Button.pop(); 230 this.observeComponentCreation((elmtId, isInitialRender) => { 231 ViewStackProcessor.StartGetAccessRecordingFor(elmtId); 232 Button.createWithLabel('ViewB: push'); 233 Button.margin({ top: 10 }); 234 Button.onClick(() => { 235 this.arrA.push(new import_Observed_1.ClassB(0)); 236 }); 237 if (!isInitialRender) { 238 Button.pop(); 239 } 240 ViewStackProcessor.StopGetAccessRecording(); 241 }); 242 Button.pop(); 243 this.observeComponentCreation((elmtId, isInitialRender) => { 244 ViewStackProcessor.StartGetAccessRecordingFor(elmtId); 245 Button.createWithLabel('ViewB: shift'); 246 Button.margin({ top: 10 }); 247 Button.onClick(() => { 248 this.arrA.shift(); 249 }); 250 if (!isInitialRender) { 251 Button.pop(); 252 } 253 ViewStackProcessor.StopGetAccessRecording(); 254 }); 255 Button.pop(); 256 Column.pop(); 257 } 258 rerender() { 259 this.updateDirtyElements(); 260 } 261} 262ViewStackProcessor.StartGetAccessRecordingFor(ViewStackProcessor.AllocateNewElmetIdForNextComponent()); 263loadDocument(new ViewB(undefined, {})); 264ViewStackProcessor.StopGetAccessRecording(); 265` 266