• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2021 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 = `
17@Component
18struct MyLinkTestComponent {
19    @Link myLink1?: any
20    @Link myLink2?: number
21    @Link myLink3?: boolean
22    @Link myLink4?: string
23
24    private myVar: number = 0
25    private myVar2: number
26
27    build() {
28
29    }
30}
31
32@Entry
33@Component
34struct LinkTest {
35    @State myState1: any = { count: 0 }
36    @State myState2: number = 0
37    @State myState3: boolean = false
38    @State myState4: string = 'Home'
39
40    build() {
41        Row() {
42            MyLinkTestComponent({
43                myLink1: $myState1,
44                myLink2: this.$myState2,
45                myLink3: $myState3,
46                myLink4: this.$myState4,
47                myVar: 100,
48                myVar2: 100
49            })
50        }
51    }
52}`
53
54exports.expectResult =
55`"use strict";
56class MyLinkTestComponent extends View {
57    constructor(compilerAssignedUniqueChildId, parent, params) {
58        super(compilerAssignedUniqueChildId, parent);
59        this.__myLink1 = new SynchedPropertyObjectTwoWay(params.myLink1, this, "myLink1");
60        this.__myLink2 = new SynchedPropertySimpleTwoWay(params.myLink2, this, "myLink2");
61        this.__myLink3 = new SynchedPropertySimpleTwoWay(params.myLink3, this, "myLink3");
62        this.__myLink4 = new SynchedPropertySimpleTwoWay(params.myLink4, this, "myLink4");
63        this.myVar = 0;
64        this.myVar2 = undefined;
65        this.updateWithValueParams(params);
66    }
67    updateWithValueParams(params) {
68        if (params.myVar !== undefined) {
69            this.myVar = params.myVar;
70        }
71        if (params.myVar2 !== undefined) {
72            this.myVar2 = params.myVar2;
73        }
74    }
75    aboutToBeDeleted() {
76        this.__myLink1.aboutToBeDeleted();
77        this.__myLink2.aboutToBeDeleted();
78        this.__myLink3.aboutToBeDeleted();
79        this.__myLink4.aboutToBeDeleted();
80        SubscriberManager.Get().delete(this.id());
81    }
82    get myLink1() {
83        return this.__myLink1.get();
84    }
85    set myLink1(newValue) {
86        this.__myLink1.set(newValue);
87    }
88    get myLink2() {
89        return this.__myLink2.get();
90    }
91    set myLink2(newValue) {
92        this.__myLink2.set(newValue);
93    }
94    get myLink3() {
95        return this.__myLink3.get();
96    }
97    set myLink3(newValue) {
98        this.__myLink3.set(newValue);
99    }
100    get myLink4() {
101        return this.__myLink4.get();
102    }
103    set myLink4(newValue) {
104        this.__myLink4.set(newValue);
105    }
106    render() {
107    }
108}
109class LinkTest extends View {
110    constructor(compilerAssignedUniqueChildId, parent, params) {
111        super(compilerAssignedUniqueChildId, parent);
112        this.__myState1 = new ObservedPropertyObject({ count: 0 }, this, "myState1");
113        this.__myState2 = new ObservedPropertySimple(0, this, "myState2");
114        this.__myState3 = new ObservedPropertySimple(false, this, "myState3");
115        this.__myState4 = new ObservedPropertySimple('Home', this, "myState4");
116        this.updateWithValueParams(params);
117    }
118    updateWithValueParams(params) {
119        if (params.myState1 !== undefined) {
120            this.myState1 = params.myState1;
121        }
122        if (params.myState2 !== undefined) {
123            this.myState2 = params.myState2;
124        }
125        if (params.myState3 !== undefined) {
126            this.myState3 = params.myState3;
127        }
128        if (params.myState4 !== undefined) {
129            this.myState4 = params.myState4;
130        }
131    }
132    aboutToBeDeleted() {
133        this.__myState1.aboutToBeDeleted();
134        this.__myState2.aboutToBeDeleted();
135        this.__myState3.aboutToBeDeleted();
136        this.__myState4.aboutToBeDeleted();
137        SubscriberManager.Get().delete(this.id());
138    }
139    get myState1() {
140        return this.__myState1.get();
141    }
142    set myState1(newValue) {
143        this.__myState1.set(newValue);
144    }
145    get myState2() {
146        return this.__myState2.get();
147    }
148    set myState2(newValue) {
149        this.__myState2.set(newValue);
150    }
151    get myState3() {
152        return this.__myState3.get();
153    }
154    set myState3(newValue) {
155        this.__myState3.set(newValue);
156    }
157    get myState4() {
158        return this.__myState4.get();
159    }
160    set myState4(newValue) {
161        this.__myState4.set(newValue);
162    }
163    render() {
164        Row.create();
165        let earlierCreatedChild_2 = this.findChildById("2");
166        if (earlierCreatedChild_2 == undefined) {
167            View.create(new MyLinkTestComponent("2", this, {
168                myLink1: this.__myState1,
169                myLink2: this.__myState2,
170                myLink3: this.__myState3,
171                myLink4: this.__myState4,
172                myVar: 100,
173                myVar2: 100
174            }));
175        }
176        else {
177            earlierCreatedChild_2.updateWithValueParams({
178                myVar: 100,
179                myVar2: 100
180            });
181            View.create(earlierCreatedChild_2);
182        }
183        Row.pop();
184    }
185}
186loadDocument(new LinkTest("1", undefined, {}));
187`
188