• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2023 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 *  * RecycleManager - Recycle cache manager
16 *
17* all definitions in this file are framework internal
18*/
19
20/**
21 * @class RecycleManager
22 * @description manage the JS object cached of current node
23 */
24class RecycleManager {
25  // key: recycle node name
26  // value: recycle node JS object
27  private cachedRecycleNodes_: Map<string, Array<ViewPU>> = undefined;
28  private biMap_: BidirectionalMap = undefined;
29
30  constructor() {
31    this.cachedRecycleNodes_ = new Map<string, Array<ViewPU>>();
32    this.biMap_ = new BidirectionalMap();
33  }
34
35  public updateNodeId(oldElmtId: number, newElmtId: number): void {
36    this.biMap_.delete(oldElmtId);
37    this.biMap_.add([oldElmtId, newElmtId]);
38  }
39
40  public proxyNodeId(oldElmtId: number): number {
41    const proxy = this.biMap_.get(oldElmtId);
42    if (!proxy) {
43      return oldElmtId;
44    }
45    return proxy;
46  }
47
48  public pushRecycleNode(name: string, node: ViewPU): void {
49    if (!this.cachedRecycleNodes_.get(name)) {
50      this.cachedRecycleNodes_.set(name, new Array<ViewPU>());
51    }
52    this.cachedRecycleNodes_.get(name)?.push(node);
53  }
54
55  public popRecycleNode(name: string): ViewPU {
56    return this.cachedRecycleNodes_.get(name)?.pop();
57  }
58
59  // When parent JS View is deleted, release all cached nodes
60  public purgeAllCachedRecycleNode(): void {
61    this.cachedRecycleNodes_.forEach((nodes, _) => {
62      nodes.forEach((node) => {
63        node.resetRecycleCustomNode();
64      });
65    })
66    this.cachedRecycleNodes_.clear();
67  }
68
69  // Set active status for all cached nodes
70  public setActive(active): void {
71    this.cachedRecycleNodes_.forEach((nodes, _) => {
72      nodes.forEach((node) => {
73        node.setActiveInternal(active, true);
74      })
75    })
76  }
77}
78
79class BidirectionalMap {
80  private fwdMap_: Map<number, number> = undefined;
81  private revMap_: Map<number, number> = undefined;
82
83  constructor() {
84    this.fwdMap_ = new Map<number, number>();
85    this.revMap_ = new Map<number, number>();
86  }
87
88  delete(key: number) {
89    if (!this.fwdMap_[key]) {
90      return;
91    }
92    const rev = this.fwdMap_[key];
93    this.fwdMap_.delete(key);
94    this.revMap_.delete(rev);
95  }
96
97  get(key: number): number | undefined {
98    return this.fwdMap_[key] || this.revMap_[key];
99  }
100
101  add(pair: [number, number]) {
102    this.fwdMap_[pair[0]] = pair[1];
103    this.revMap_[pair[1]] = pair[0];
104  }
105}
106