• 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
16/**
17 * Singleton class SubscriberManager implements IPropertySubscriberLookup
18 * public API to manage IPropertySubscriber
19 */
20
21class SubscriberManager {
22
23  private subscriberById_: Map<number, IPropertySubscriber>;
24
25  private static instance_: SubscriberManager;
26
27  private static nextId_: number = 0;
28
29  /**
30    * check subscriber is known
31    * same as ES6 Map.prototype.has()
32    *
33    * @since 9
34    */
35  public static Has(id: number): boolean {
36    return SubscriberManager.GetInstance().has(id);
37  }
38
39  /**
40   *
41   * retrieve subscriber by id
42   * same as ES6 Map.prototype.get()
43   *
44   *  @since 9
45   */
46  public static Find(id: number): IPropertySubscriber {
47    return SubscriberManager.GetInstance().get(id);
48  }
49
50  /**
51   * unregister a subscriber
52   * same as ES6 Map.prototype.delete()
53   * @return boolean success or failure to delete
54   *
55   *  @since 9
56   */
57  public static Delete(id: number): boolean {
58    return SubscriberManager.GetInstance().delete(id);
59  }
60
61  /**
62  * add a new subscriber.
63  * The subscriber must have a new (unused) id (@see MakeId() )
64  * for add() to succeed.
65  * same as Map.prototype.set()
66  *
67  *  @since 9
68  */
69  public static Add(newSubsriber: IPropertySubscriber): boolean {
70    return SubscriberManager.GetInstance().add(newSubsriber);
71  }
72
73  /**
74   * Update recycle custom node element id.
75   */
76  public static UpdateRecycleElmtId(oldId: number, newId: number): boolean {
77    return SubscriberManager.GetInstance().updateRecycleElmtId(oldId, newId);
78  }
79
80  /**
81  *
82  * @returns a globally unique id to be assigned to a IPropertySubscriber objet
83  * Use MakeId() to assign a IPropertySubscriber object an id before calling @see add() .
84  *
85  *  @since 9
86 */
87  public static MakeId(): number {
88    return SubscriberManager.GetInstance().makeId();
89  }
90
91  /**
92   *
93   * @returns a global unique id for state variables.
94   * Unlike MakeId, no need to get id from native side.
95   *
96   * @since 12
97   */
98  public static MakeStateVariableId(): number {
99    return SubscriberManager.nextId_--;
100  }
101
102  /**
103   * Check number of registered Subscriber / registered IDs.
104   * @returns number of registered unique ids.
105   *
106   *  @since 9
107   */
108
109  public static NumberOfSubscribers(): number {
110    return SubscriberManager.GetInstance().numberOfSubscribers();
111  }
112
113  /**
114   *
115   * internal (non-SDK) methods below
116   *
117  */
118
119    /**
120   * Get singleton, create it on first call
121   * @returns SubscriberManager singleton
122   *
123   * internal function
124   * This function will be removed soon, use static functions instead!
125   * Note: Fnction gets used by transpiler output for both full update and partial update
126   */
127   public static Get() : SubscriberManager {
128    if (!SubscriberManager.instance_) {
129      SubscriberManager.instance_ = new SubscriberManager();
130    }
131    return SubscriberManager.instance_;
132   }
133
134  /**
135   * Get singleton, create it on first call
136   * @returns SubscriberManager singleton
137   *
138   * internal function
139   */
140  private static GetInstance() : SubscriberManager {
141    if (!SubscriberManager.instance_) {
142      SubscriberManager.instance_ = new SubscriberManager();
143    }
144    return SubscriberManager.instance_;
145  }
146
147  /**
148   * for debug purposes dump all known subscriber's info to comsole
149   *
150   * not a public / sdk function
151   */
152  public static DumpSubscriberInfo(): void {
153    SubscriberManager.GetInstance().dumpSubscriberInfo();
154  }
155
156  /**
157   * not a public / sdk function
158   * @see Has
159   */
160  public has(id: number): boolean {
161    return this.subscriberById_.has(id);
162  }
163
164  /**
165   * not a public / sdk function
166   * @see Get
167   */
168  public get(id: number): IPropertySubscriber {
169    return this.subscriberById_.get(id);
170  }
171
172  /**
173 * not a public / sdk function
174 * @see Delete
175 */
176  public delete(id: number): boolean {
177    if (!this.has(id)) {
178      stateMgmtConsole.warn(`SubscriberManager.delete unknown id ${id} `);
179      return false;
180    }
181    return this.subscriberById_.delete(id);
182  }
183
184  /**
185 * not a public / sdk function
186 * @see Add
187 */
188  public add(newSubsriber: IPropertySubscriber): boolean {
189    if (this.has(newSubsriber.id__())) {
190      return false;
191    }
192    this.subscriberById_.set(newSubsriber.id__(), newSubsriber);
193    return true;
194  }
195
196  public updateRecycleElmtId(oldId: number, newId: number): boolean {
197    if (!this.has(oldId)) {
198      return false;
199    }
200    const subscriber = this.get(oldId);
201    this.subscriberById_.delete(oldId);
202    this.subscriberById_.set(newId, subscriber);
203    return true;
204  }
205
206  /**
207   * Method for testing purposes
208   * @returns number of subscribers
209   *
210   * not a public / sdk function
211   */
212  public numberOfSubscribers(): number {
213    return this.subscriberById_.size;
214  }
215
216  /**
217   * for debug purposes dump all known subscriber's info to comsole
218   *
219   * not a public / sdk function
220   */
221  public dumpSubscriberInfo(): void {
222    stateMgmtConsole.debug('Dump of SubscriberManager +++ (sart)');
223    for (let [id, subscriber] of this.subscriberById_) {
224      stateMgmtConsole.debug(`Id: ${id} -> ${subscriber['info'] ? subscriber['info']() : 'unknown'}`);
225    }
226    stateMgmtConsole.debug('Dump of SubscriberManager +++ (end)');
227  }
228
229  /**
230   *
231   * @returns a globally unique id to be assigned to a Subscriber
232   */
233  makeId(): number {
234    return ViewStackProcessor.MakeUniqueId();
235  }
236
237  /**
238   * SubscriberManager is a singleton created by the framework
239   * do not use
240   *
241   * internal method
242   */
243  private constructor() {
244    this.subscriberById_ = new Map<number, IPropertySubscriber>();
245    stateMgmtConsole.debug('SubscriberManager has been created.');
246  }
247}
248