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 * SynchedPropertyNesedObjectPU 18 * 19 * all definitions in this file are framework internal 20 * 21 */ 22class SynchedPropertyNesedObjectPU<C extends Object> 23 extends ObservedPropertyObjectAbstractPU<C> 24 implements ISinglePropertyChangeSubscriber<C> { 25 26 private obsObject_: C; 27 28 /** 29 * Construct a Property of a su component that links to a variable of parent view that holds an ObservedObject 30 * example 31 * this.b.$a with b of type PC and a of type C, or 32 * this.$b[5] with this.b of type PC and array item b[5] of type C; 33 * 34 * @param subscribeMe 35 * @param propName 36 */ 37 constructor(obsObject: C, 38 owningChildView: IPropertySubscriber, propertyName: PropertyInfo) { 39 super(owningChildView, propertyName); 40 this.obsObject_ = obsObject; 41 42 // register to the ObservedObject 43 ObservedObject.addOwningProperty(this.obsObject_, this); 44 } 45 46 /* 47 like a destructor, need to call this before deleting 48 the property. 49 */ 50 aboutToBeDeleted() { 51 // unregister from the ObservedObject 52 ObservedObject.removeOwningProperty(this.obsObject_, this); 53 super.aboutToBeDeleted(); 54 } 55 56 57 // this object is subscriber to ObservedObject 58 // will call this cb function when property has changed 59 hasChanged(newValue: C): void { 60 stateMgmtConsole.debug(`SynchedPropertyNesedObjectPU[${this.id__()}, '${this.info() || "unknown"}']: contained ObservedObject hasChanged'.`) 61 this.notifyHasChanged(this.obsObject_); 62 } 63 64 65 public getUnmonitored(): C { 66 // stateMgmtConsole.debug(`SynchedPropertyNesedObject[${this.id()}, '${this.info() || "unknown"}']: getUnmonitored returns '${JSON.stringify(this.wrappedValue_)}' .`); 67 // unmonitored get access , no call to otifyPropertyRead ! 68 return this.obsObject_; 69 } 70 71 // get 'read through` from the ObservedProperty 72 public get(): C { 73 stateMgmtConsole.debug(`SynchedPropertyNesedObjectPU[${this.id__()}, '${this.info() || "unknown"}']: get`) 74 this.notifyPropertyRead(); 75 return this.obsObject_; 76 } 77 78 // set 'writes through` to the ObservedProperty 79 public set(newValue: C): void { 80 if (this.obsObject_ == newValue) { 81 stateMgmtConsole.debug(`SynchedPropertyNesedObjectPu[${this.id__()}IP, '${this.info() || "unknown"}']: set with unchanged value '${newValue}'- ignoring.`); 82 return; 83 } 84 85 stateMgmtConsole.debug(`SynchedPropertyNesedObjectPU[${this.id__()}, '${this.info() || "unknown"}']: set to newValue: '${newValue}'.`); 86 87 // unsubscribe from the old value ObservedObject 88 ObservedObject.removeOwningProperty(this.obsObject_, this); 89 90 this.obsObject_ = newValue; 91 92 // subscribe to the new value ObservedObject 93 ObservedObject.addOwningProperty(this.obsObject_, this); 94 95 // notify value change to subscribing View 96 this.notifyHasChanged(this.obsObject_); 97 } 98} 99