1/* 2 * Copyright (c) 2021 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 16class SubscriberManager implements IPropertySubscriberLookup { 17 private subscriberById_: Map<number, IPropertySubscriber>; 18 private nextFreeId_: number; 19 20 private static INSTANCE_: SubscriberManager = new SubscriberManager(); 21 22 public static Get() { return SubscriberManager.INSTANCE_; } 23 24 constructor() { 25 this.subscriberById_ = new Map<number, IPropertySubscriber>(); 26 this.nextFreeId_ = 0; 27 console.debug("SubscriberManager has been created."); 28 } 29 30 public has(id: number): boolean { 31 return this.subscriberById_.has(id); 32 } 33 34 public get(id: number): IPropertySubscriber { 35 return this.subscriberById_.get(id); 36 } 37 38 public delete(id: number): boolean { 39 return this.subscriberById_.delete(id); 40 } 41 42 public add(newSubsriber: IPropertySubscriber): boolean { 43 if (this.has(newSubsriber.id__())) { 44 return false; 45 } 46 this.subscriberById_.set(newSubsriber.id__(), newSubsriber); 47 return true; 48 } 49 50 /** 51 * Method for testing purposes 52 * @returns number of subscribers 53 */ 54 public numberOfSubscrbers(): number { 55 return this.subscriberById_.size; 56 } 57 58 /** 59 * for debug purposes dump all known subscriber's info to comsole 60 */ 61 public dumpSubscriberInfo(): void { 62 console.debug("Dump of SubscriberManager +++ (sart)") 63 for (let [id, subscriber] of this.subscriberById_) { 64 console.debug(`Id: ${id} -> ${subscriber['info'] ? subscriber['info']() : 'unknown'}`) 65 } 66 console.debug("Dump of SubscriberManager +++ (end)") 67 } 68 69 MakeId() { 70 return this.nextFreeId_++; 71 } 72} 73