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 SynchedPropertyObjectTwoWay<C extends Object> 17 extends ObservedPropertyObjectAbstract<C> 18 implements ISinglePropertyChangeSubscriber<C> { 19 20 private linkedParentProperty_: ObservedPropertyObjectAbstract<C>; 21 private contentStoragelinkedParentProperty_: ObservedPropertyObjectAbstract<C>; 22 23 constructor(linkSource: ObservedPropertyObjectAbstract<C>, 24 owningChildView: IPropertySubscriber, 25 thisPropertyName: PropertyInfo, 26 contentStoragelinkedParentProperty?: ObservedPropertyObjectAbstract<C>) { 27 super(owningChildView, thisPropertyName); 28 this.linkedParentProperty_ = linkSource; 29 // register to the parent property 30 this.linkedParentProperty_.subscribeMe(this); 31 if (contentStoragelinkedParentProperty) { 32 this.contentStoragelinkedParentProperty_ = contentStoragelinkedParentProperty; 33 } 34 // register to the ObservedObject 35 ObservedObject.addOwningProperty(this.getObject(), this); 36 } 37 38 /* 39 like a destructor, need to call this before deleting 40 the property. 41 */ 42 aboutToBeDeleted() { 43 // unregister from parent of this link 44 this.linkedParentProperty_.unlinkSuscriber(this.id__()); 45 46 // unregister from the ObservedObject 47 ObservedObject.removeOwningProperty(this.getObject(), this); 48 super.aboutToBeDeleted(); 49 } 50 51 private getObject(): C { 52 this.notifyPropertyRead(); 53 return this.linkedParentProperty_.get(); 54 } 55 56 private setObject(newValue: C): void { 57 this.linkedParentProperty_.set(newValue) 58 } 59 60 // this object is subscriber to ObservedObject 61 // will call this cb function when property has changed 62 hasChanged(newValue: C): void { 63 console.debug(`SynchedPropertyObjectTwoWay[${this.id__()}, '${this.info() || "unknown"}']: contained ObservedObject hasChanged'.`) 64 this.notifyHasChanged(this.getObject()); 65 } 66 67 68 69 // get 'read through` from the ObservedProperty 70 public get(): C { 71 console.debug(`SynchedPropertyObjectTwoWay[${this.id__()}, '${this.info() || "unknown"}']: get`) 72 if (this.contentStoragelinkedParentProperty_) { 73 return this.contentStoragelinkedParentProperty_.get(); 74 } 75 return this.getObject(); 76 } 77 78 // set 'writes through` to the ObservedProperty 79 public set(newValue: C): void { 80 if (this.contentStoragelinkedParentProperty_) { 81 this.contentStoragelinkedParentProperty_.set(newValue); 82 return; 83 } 84 if (this.getObject() == newValue) { 85 console.debug(`SynchedPropertyObjectTwoWay[${this.id__()}IP, '${this.info() || "unknown"}']: set with unchanged value '${newValue}'- ignoring.`); 86 return; 87 } 88 89 console.debug(`SynchedPropertyObjectTwoWay[${this.id__()}, '${this.info() || "unknown"}']: set to newValue: '${newValue}'.`); 90 91 ObservedObject.removeOwningProperty(this.getObject(), this); 92 this.setObject(newValue); 93 ObservedObject.addOwningProperty(this.getObject(), this); 94 95 this.notifyHasChanged(newValue); 96 } 97 98 /** 99 * These functions are meant for use in connection with the App Stoage and 100 * business logic implementation. 101 * the created Link and Prop will update when 'this' property value 102 * changes. 103 */ 104 public createLink(subscribeOwner?: IPropertySubscriber, 105 linkPropName?: PropertyInfo, contentStoragelinkedParentProperty?: ObservedPropertyObjectAbstract<C>): ObservedPropertyAbstract<C> { 106 return new SynchedPropertyObjectTwoWay(this, subscribeOwner, linkPropName, contentStoragelinkedParentProperty); 107 } 108 public createProp(subscribeOwner?: IPropertySubscriber, 109 linkPropName?: PropertyInfo, contentStoragelinkedParentProperty?: ObservedPropertyObjectAbstract<C>): ObservedPropertyAbstract<C> { 110 throw new Error("Creating a 'Prop' proerty is unsuppoeted for Object type prperty value."); 111 } 112 113} 114