• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 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
16// eslint-disable-next-line @typescript-eslint/no-unused-vars
17class FetchedRegistry {
18  private fetchedIndexes: Set<number> = new Set();
19  private rangeToFetchInternal: IndexRange = new IndexRange(0, 0);
20  private missedIndexes: Set<number> = new Set();
21
22  get rangeToFetch(): IndexRange {
23    return this.rangeToFetchInternal;
24  }
25
26  addFetched(index: number): void {
27    if (this.rangeToFetch.contains(index)) {
28      this.fetchedIndexes.add(index);
29      this.missedIndexes.delete(index);
30    }
31  }
32
33  removeFetched(index: number): void {
34    if (this.rangeToFetch.contains(index)) {
35      this.fetchedIndexes.delete(index);
36      this.missedIndexes.add(index);
37    }
38  }
39
40  has(index: number): boolean {
41    return this.fetchedIndexes.has(index);
42  }
43
44  getFetchedInRange(range: IndexRange): number {
45    let fetched = 0;
46    range.forEachIndex((index) => {
47      fetched += this.fetchedIndexes.has(index) ? 1 : 0;
48    });
49    return fetched;
50  }
51
52  updateRangeToFetch(fetchRange: IndexRange): void {
53    this.rangeToFetch.subtract(fetchRange).forEachIndex((index) => {
54      this.fetchedIndexes.delete(index);
55    });
56    this.rangeToFetchInternal = fetchRange;
57    this.missedIndexes.clear();
58    this.rangeToFetch.forEachIndex((index) => {
59      if (!this.fetchedIndexes.has(index)) {
60        this.missedIndexes.add(index);
61      }
62    });
63  }
64
65  getItemsToFetch(): Set<number> {
66    return new Set(this.missedIndexes);
67  }
68
69  incrementFetchedGreaterThen(value: number, newFetchRange: IndexRange): void {
70    this.offsetAllGreaterThen(value, 1);
71    this.updateRangeToFetch(newFetchRange);
72  }
73
74  decrementFetchedGreaterThen(value: number, newFetchRange: IndexRange): void {
75    this.offsetAllGreaterThen(value, -1);
76    this.updateRangeToFetch(newFetchRange);
77  }
78
79  private offsetAllGreaterThen(value: number, offset: number): void {
80    const updated = new Set<number>();
81    this.fetchedIndexes.forEach((index) => {
82      updated.add(index > value ? index + offset : index);
83    });
84    this.fetchedIndexes = updated;
85  }
86
87  clearFetched(newFetchRange: IndexRange): void {
88    this.fetchedIndexes.clear();
89    this.updateRangeToFetch(newFetchRange);
90  }
91}
92