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