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 * Update recycle custom node element id. 73 */ 74 public static UpdateRecycleElmtId(oldId: number, newId: number): boolean { 75 return SubscriberManager.GetInstance().updateRecycleElmtId(oldId, newId); 76 } 77 78 /** 79 * 80 * @returns a globally unique id to be assigned to a IPropertySubscriber objet 81 * Use MakeId() to assign a IPropertySubscriber object an id before calling @see add() . 82 * 83 * @since 9 84 */ 85 public static MakeId(): number { 86 return SubscriberManager.GetInstance().makeId(); 87 } 88 89 /** 90 * Check number of registered Subscriber / registered IDs. 91 * @returns number of registered unique ids. 92 * 93 * @since 9 94 */ 95 96 public static NumberOfSubscribers(): number { 97 return SubscriberManager.GetInstance().numberOfSubscribers(); 98 } 99 100 /** 101 * 102 * internal (non-SDK) methods below 103 * 104 */ 105 106 /** 107 * Get singleton, create it on first call 108 * @returns SubscriberManager singleton 109 * 110 * internal function 111 * This function will be removed soon, use static functions instead! 112 * Note: Fnction gets used by transpiler output for both full update and partial update 113 */ 114 public static Get() : SubscriberManager { 115 if (!SubscriberManager.instance_) { 116 SubscriberManager.instance_ = new SubscriberManager(); 117 } 118 return SubscriberManager.instance_; 119 } 120 121 /** 122 * Get singleton, create it on first call 123 * @returns SubscriberManager singleton 124 * 125 * internal function 126 */ 127 private static GetInstance() : SubscriberManager { 128 if (!SubscriberManager.instance_) { 129 SubscriberManager.instance_ = new SubscriberManager(); 130 } 131 return SubscriberManager.instance_; 132 } 133 134 /** 135 * for debug purposes dump all known subscriber's info to comsole 136 * 137 * not a public / sdk function 138 */ 139 public static DumpSubscriberInfo(): void { 140 SubscriberManager.GetInstance().dumpSubscriberInfo(); 141 } 142 143 /** 144 * not a public / sdk function 145 * @see Has 146 */ 147 public has(id: number): boolean { 148 return this.subscriberById_.has(id); 149 } 150 151 /** 152 * not a public / sdk function 153 * @see Get 154 */ 155 public get(id: number): IPropertySubscriber { 156 return this.subscriberById_.get(id); 157 } 158 159 /** 160 * not a public / sdk function 161 * @see Delete 162 */ 163 public delete(id: number): boolean { 164 if (!this.has(id)) { 165 stateMgmtConsole.warn(`SubscriberManager.delete unknown id ${id} `); 166 return false; 167 } 168 return this.subscriberById_.delete(id); 169 } 170 171 /** 172 * not a public / sdk function 173 * @see Add 174 */ 175 public add(newSubsriber: IPropertySubscriber): boolean { 176 if (this.has(newSubsriber.id__())) { 177 return false; 178 } 179 this.subscriberById_.set(newSubsriber.id__(), newSubsriber); 180 return true; 181 } 182 183 public updateRecycleElmtId(oldId: number, newId: number): boolean { 184 if (!this.has(oldId)) { 185 return false; 186 } 187 const subscriber = this.get(oldId); 188 this.subscriberById_.delete(oldId); 189 this.subscriberById_.set(newId, subscriber); 190 return true; 191 } 192 193 /** 194 * Method for testing purposes 195 * @returns number of subscribers 196 * 197 * not a public / sdk function 198 */ 199 public numberOfSubscribers(): number { 200 return this.subscriberById_.size; 201 } 202 203 /** 204 * for debug purposes dump all known subscriber's info to comsole 205 * 206 * not a public / sdk function 207 */ 208 public dumpSubscriberInfo(): void { 209 stateMgmtConsole.debug("Dump of SubscriberManager +++ (sart)") 210 for (let [id, subscriber] of this.subscriberById_) { 211 stateMgmtConsole.debug(`Id: ${id} -> ${subscriber['info'] ? subscriber['info']() : 'unknown'}`) 212 } 213 stateMgmtConsole.debug("Dump of SubscriberManager +++ (end)") 214 } 215 216 /** 217 * 218 * @returns a globally unique id to be assigned to a Subscriber 219 */ 220 makeId(): number { 221 return ViewStackProcessor.MakeUniqueId(); 222 } 223 224 /** 225 * SubscriberManager is a singleton created by the framework 226 * do not use 227 * 228 * internal method 229 */ 230 private constructor() { 231 this.subscriberById_ = new Map<number, IPropertySubscriber>(); 232 stateMgmtConsole.debug("SubscriberManager has been created."); 233 } 234} 235