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