1/* 2 * Copyright (c) 2021-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 * SynchedPropertyNesedObject 18 * 19 * all definitions in this file are framework internal 20 */ 21class SynchedPropertyNesedObject<C extends Object> 22 extends ObservedPropertyObjectAbstract<C> 23 implements ISinglePropertyChangeSubscriber<C> { 24 25 private obsObject_: C; 26 27 /** 28 * Construct a Property of a su component that links to a variable of parent view that holds an ObservedObject 29 * example 30 * this.b.$a with b of type PC and a of type C, or 31 * this.$b[5] with this.b of type PC and array item b[5] of type C; 32 * 33 * @param subscribeMe 34 * @param propName 35 */ 36 constructor(obsObject: C, 37 owningChildView: IPropertySubscriber, propertyName: PropertyInfo) { 38 super(owningChildView, propertyName); 39 this.obsObject_ = obsObject; 40 41 // register to the ObservedObject 42 ObservedObject.addOwningProperty(this.obsObject_, this); 43 } 44 45 /* 46 like a destructor, need to call this before deleting 47 the property. 48 */ 49 aboutToBeDeleted() { 50 // unregister from the ObservedObject 51 ObservedObject.removeOwningProperty(this.obsObject_, this); 52 super.aboutToBeDeleted(); 53 } 54 55 56 // this object is subscriber to ObservedObject 57 // will call this cb function when property has changed 58 hasChanged(newValue: C): void { 59 stateMgmtConsole.debug(`SynchedPropertyNesedObject[${this.id__()}, '${this.info() || "unknown"}']: contained ObservedObject hasChanged'.`) 60 this.notifyHasChanged(this.obsObject_); 61 } 62 63 64 65 // get 'read through` from the ObservedProperty 66 public get(): C { 67 stateMgmtConsole.debug(`SynchedPropertyNesedObject[${this.id__()}, '${this.info() || "unknown"}']: get`) 68 this.notifyPropertyRead(); 69 return this.obsObject_; 70 } 71 72 // set 'writes through` to the ObservedProperty 73 public set(newValue: C): void { 74 if (this.obsObject_ == newValue) { 75 stateMgmtConsole.debug(`SynchedPropertyNesedObject[${this.id__()}IP, '${this.info() || "unknown"}']: set with unchanged value '${newValue}'- ignoring.`); 76 return; 77 } 78 79 stateMgmtConsole.debug(`SynchedPropertyNesedObject[${this.id__()}, '${this.info() || "unknown"}']: set to newValue: '${newValue}'.`); 80 81 // unsubscribe from the old value ObservedObject 82 ObservedObject.removeOwningProperty(this.obsObject_, this); 83 84 this.obsObject_ = newValue; 85 86 // subscribe to the new value ObservedObject 87 ObservedObject.addOwningProperty(this.obsObject_, this); 88 89 // notify value change to subscribing View 90 this.notifyHasChanged(this.obsObject_); 91 } 92 93 /** 94 * These functions are meant for use in connection with the App Stoage and 95 * business logic implementation. 96 * the created Link and Prop will update when 'this' property value 97 * changes. 98 */ 99 public createLink(subscribeOwner?: IPropertySubscriber, 100 linkPropName?: PropertyInfo): ObservedPropertyAbstract<C> { 101 throw new Error("Method not supported for property linking to a nested objects."); 102 } 103 public createProp(subscribeOwner?: IPropertySubscriber, 104 linkPropName?: PropertyInfo): ObservedPropertyAbstract<C> { 105 throw new Error("Creating a 'Prop' proerty is unsuppoeted for Object type prperty value."); 106 } 107} 108