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