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