• 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 });
69const import_Observed_1 = require("./test/pages/import@Observed");
70class ViewA extends ViewPU {
71    constructor(parent, params, __localStorage, elmtId = -1, paramsLambda = undefined) {
72        super(parent, __localStorage, elmtId);
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) {
120        super(parent, __localStorage, elmtId);
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 paramsLambda = () => {
161                                return {
162                                    label: JSON.stringify(item.id),
163                                    a: item
164                                };
165                            };
166                            ViewPU.create(new ViewA(this, { label: JSON.stringify(item.id), a: item }, undefined, elmtId, paramsLambda));
167                        }
168                        else {
169                            this.updateStateVarsOfChildByElmtId(elmtId, {
170                                a: item
171                            });
172                        }
173                    }, null);
174                }
175            };
176            this.forEachUpdateFunction(elmtId, this.arrA, forEachItemGenFunction, (item) => item.id.toString(), false, false);
177        }, ForEach);
178        ForEach.pop();
179        {
180            this.observeComponentCreation2((elmtId, isInitialRender) => {
181                if (isInitialRender) {
182                    let paramsLambda = () => {
183                        return {
184                            label: JSON.stringify(this.arrA[0]),
185                            a: this.arrA[0]
186                        };
187                    };
188                    ViewPU.create(new ViewA(this, { label: JSON.stringify(this.arrA[0]), a: this.arrA[0] }, undefined, elmtId, paramsLambda));
189                }
190                else {
191                    this.updateStateVarsOfChildByElmtId(elmtId, {
192                        a: this.arrA[0]
193                    });
194                }
195            }, null);
196        }
197        {
198            this.observeComponentCreation2((elmtId, isInitialRender) => {
199                if (isInitialRender) {
200                    let paramsLambda = () => {
201                        return {
202                            label: JSON.stringify(this.arrA[this.arrA.length - 1]),
203                            a: this.arrA[this.arrA.length - 1]
204                        };
205                    };
206                    ViewPU.create(new ViewA(this, { label: JSON.stringify(this.arrA[this.arrA.length - 1]), a: this.arrA[this.arrA.length - 1] }, undefined, elmtId, paramsLambda));
207                }
208                else {
209                    this.updateStateVarsOfChildByElmtId(elmtId, {
210                        a: this.arrA[this.arrA.length - 1]
211                    });
212                }
213            }, null);
214        }
215        this.observeComponentCreation2((elmtId, isInitialRender) => {
216            Button.createWithLabel('ViewB: reset array');
217            Button.margin({ top: 10 });
218            Button.onClick(() => {
219                this.arrA = [new import_Observed_1.ClassB(0), new import_Observed_1.ClassB(0)];
220            });
221        }, Button);
222        Button.pop();
223        this.observeComponentCreation2((elmtId, isInitialRender) => {
224            Button.createWithLabel('ViewB: push');
225            Button.margin({ top: 10 });
226            Button.onClick(() => {
227                this.arrA.push(new import_Observed_1.ClassB(0));
228            });
229        }, Button);
230        Button.pop();
231        this.observeComponentCreation2((elmtId, isInitialRender) => {
232            Button.createWithLabel('ViewB: shift');
233            Button.margin({ top: 10 });
234            Button.onClick(() => {
235                this.arrA.shift();
236            });
237        }, Button);
238        Button.pop();
239        Column.pop();
240    }
241    rerender() {
242        this.updateDirtyElements();
243    }
244}
245ViewStackProcessor.StartGetAccessRecordingFor(ViewStackProcessor.AllocateNewElmetIdForNextComponent());
246loadDocument(new ViewB(undefined, {}));
247ViewStackProcessor.StopGetAccessRecording();
248`
249