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 * SynchedPropertyNestedObjectPU 18 * implementation of @ObjectLink decorated variables 19 * 20 * all definitions in this file are framework internal 21 * 22 */ 23class SynchedPropertyNestedObjectPU<C extends Object> 24 extends ObservedPropertyAbstractPU<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 this.setValueInternal(obsObject); 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 public debugInfoDecorator() : string { 56 return `@ObjectLink (class SynchedPropertyNestedObjectPU)`; 57 } 58 59 public objectPropertyHasChangedPU(eventSource: ObservedObject<C>, changedPropertyName: string) { 60 stateMgmtConsole.debug(`${this.debugInfo()}: objectPropertyHasChangedPU: property '${changedPropertyName}' \ 61 of object value has changed.`) 62 this.notifyPropertyHasChangedPU(); 63 } 64 65 66 public objectPropertyHasBeenReadPU(sourceObject: ObservedObject<C>, changedPropertyName : string) { 67 stateMgmtConsole.debug(`${this.debugInfo()}: property '${changedPropertyName}' of object value has been read.`); 68 this.notifyPropertyHasBeenReadPU(); 69 } 70 71 public getUnmonitored(): C { 72 stateMgmtConsole.propertyAccess(`${this.debugInfo()}: getUnmonitored.`); 73 // unmonitored get access , no call to notifyPropertyRead ! 74 return this.obsObject_; 75 } 76 77 // get 'read through` from the ObservedProperty 78 public get(): C { 79 stateMgmtConsole.propertyAccess(`${this.debugInfo()}: get`) 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(`SynchedPropertyNestedObjectPU[${this.id__()}IP, '${this.info() || "unknown"}']: set @ObjectLink with unchanged value - nothing to do.`); 88 return; 89 } 90 91 stateMgmtConsole.propertyAccess(`${this.debugInfo()}: set: value about to change.`); 92 93 if (this.setValueInternal(newValue)) { 94 // notify value change to subscribing View 95 this.notifyPropertyHasChangedPU(); 96 } 97 } 98 99 100 private setValueInternal(newValue: C): boolean { 101 if (!this.checkIsObject(newValue)) { 102 return false; 103 } 104 105 if (this.obsObject_ != undefined) { 106 if (this.obsObject_ instanceof SubscribableAbstract) { 107 // unregister from SubscribableAbstract object 108 (this.obsObject_ as SubscribableAbstract).removeOwningProperty(this); 109 } else if (ObservedObject.IsObservedObject(this.obsObject_)) { 110 // unregister from the ObservedObject 111 ObservedObject.removeOwningProperty(this.obsObject_, this); 112 } 113 } 114 115 this.obsObject_ = newValue; 116 117 if (this.obsObject_ != undefined) { 118 if (this.obsObject_ instanceof SubscribableAbstract) { 119 // register to SubscribableAbstract object 120 (this.obsObject_ as SubscribableAbstract).addOwningProperty(this); 121 } else if (ObservedObject.IsObservedObject(this.obsObject_)) { 122 // register to the ObservedObject 123 ObservedObject.addOwningProperty(this.obsObject_, this); 124 } else { 125 stateMgmtConsole.applicationError(`${this.debugInfo()}: set/init (method setValueInternal): assigned value is neither ObservedObject nor SubscribableAbstract. \ 126 value changes will bot be observed and UI will not update. forgot @Observed class decorator? Application error.`); 127 } 128 } 129 return true; 130 } 131} 132 133/** backward compatibility after typo in classname fix */ 134class SynchedPropertyNesedObjectPU<C extends Object> extends SynchedPropertyNestedObjectPU<C> { 135 136}