• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2022-2024 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 // test the parser period of LazyForEach
16class BasicDataSource implements IDataSource {
17  private listeners: DataChangeListener[] = [];
18  private originDataArray: string[] = [];
19
20  public totalCount(): number {
21    return this.originDataArray.length;
22  }
23
24  public getData(index: number): string {
25    return this.originDataArray[index];
26  }
27
28  registerDataChangeListener(listener: DataChangeListener): void {
29    if (this.listeners.indexOf(listener) < 0) {
30      console.info('add listener');
31      this.listeners.push(listener);
32    }
33  }
34
35  unregisterDataChangeListener(listener: DataChangeListener): void {
36    const pos = this.listeners.indexOf(listener);
37    if (pos >= 0) {
38      console.info('remove listener');
39      this.listeners.splice(pos, 1);
40    }
41  }
42
43  notifyDataAdd(index: number): void {
44    this.listeners.forEach(listener => {
45      listener.onDataAdd(index);
46    });
47  }
48}
49
50class MyDataSource extends BasicDataSource {
51  private dataArray: string[] = [];
52
53  public totalCount(): number {
54    return this.dataArray.length;
55  }
56
57  public getData(index: number): string {
58    return this.dataArray[index];
59  }
60
61  public pushData(data: string): void {
62    this.dataArray.push(data);
63    this.notifyDataAdd(this.dataArray.length - 1);
64  }
65}
66
67@Entry
68@Component
69struct LazyForEachDemo {
70  @Prop props;
71  data: MyDataSource = new MyDataSource();
72  aboutToAppear() {
73    for (let i = 0; i < 3; i++) {
74      this.data.pushData(i.toString());
75    }
76  }
77  build() {
78    Column() {
79      List() {
80        LazyForEach((()=>{return this.data})(), (item:string, index:number)=>{
81          ListItem() {
82            Text(item)
83          }
84        })
85      }
86    }
87  }
88}