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 * ObservedPropertySimplePU 18 * 19 * class that holds an actual property value of type T 20 * uses its base class to manage subscribers to this 21 * property. 22 * 23 * all definitions in this file are framework internal 24*/ 25class ObservedPropertySimplePU<T> extends ObservedPropertySimpleAbstractPU<T> 26 implements ISinglePropertyChangeSubscriber<T> { 27 28 private wrappedValue_: T; 29 30 constructor(value: T, owningView: IPropertySubscriber, propertyName: PropertyInfo) { 31 super(owningView, propertyName); 32 if (typeof value === "object") { 33 throw new SyntaxError("ObservedPropertySimple value must not be an object")! 34 } 35 this.setValueInternal(value); 36 } 37 38 aboutToBeDeleted(unsubscribeMe?: IPropertySubscriber) { 39 if (unsubscribeMe) { 40 this.unlinkSuscriber(unsubscribeMe.id__()); 41 } 42 super.aboutToBeDeleted(); 43 } 44 45 hasChanged(newValue: T): void { 46 stateMgmtConsole.debug(`ObservedPropertySimple[${this.id__()}, '${this.info() || "unknown"}']: hasChanged`); 47 this.notifyHasChanged(this.wrappedValue_); 48 } 49 50 /* 51 actually update this.wrappedValue_ 52 called needs to do value change check 53 and also notify with this.aboutToChange(); 54 */ 55 private setValueInternal(newValue: T): void { 56 stateMgmtConsole.debug(`ObservedPropertySimple[${this.id__()}, '${this.info() || "unknown"}'] new value is of simple type`); 57 this.wrappedValue_ = newValue; 58 } 59 60 public getUnmonitored(): T { 61 stateMgmtConsole.debug(`ObservedPropertySimple[${this.id__()}, '${this.info() || "unknown"}']: getUnmonitored returns '${JSON.stringify(this.wrappedValue_)}' .`); 62 // unmonitored get access , no call to otifyPropertyRead ! 63 return this.wrappedValue_; 64 } 65 66 public get(): T { 67 stateMgmtConsole.debug(`ObservedPropertySimple[${this.id__()}, '${this.info() || "unknown"}']: get returns '${JSON.stringify(this.wrappedValue_)}' .`); 68 this.notifyPropertyRead(); 69 return this.wrappedValue_; 70 } 71 72 public set(newValue: T): void { 73 if (this.wrappedValue_ == newValue) { 74 stateMgmtConsole.debug(`ObservedPropertySimple[${this.id__()}, '${this.info() || "unknown"}']: set with unchanged value - ignoring.`); 75 return; 76 } 77 stateMgmtConsole.debug(`ObservedPropertySimple[${this.id__()}, '${this.info() || "unknown"}']: set, changed from '${JSON.stringify(this.wrappedValue_)}' to '${JSON.stringify(newValue)}.`); 78 this.setValueInternal(newValue); 79 this.notifyHasChanged(newValue); 80 } 81} 82