• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 SynchedPropertySimpleTwoWay<T> extends ObservedPropertySimpleAbstract<T>
17  implements ISinglePropertyChangeSubscriber<T> {
18
19  private source_: ObservedPropertyAbstract<T>;
20  private contentObserver_?: ObservedPropertyAbstract<T>;
21
22  constructor(source: ObservedPropertyAbstract<T>, owningView: IPropertySubscriber, owningViewPropNme: PropertyInfo, contentObserver?: ObservedPropertyAbstract<T>) {
23    super(owningView, owningViewPropNme);
24    this.source_ = source;
25    this.source_.subscribeMe(this);
26    if (contentObserver) {
27      this.contentObserver_ = contentObserver;
28    }
29  }
30
31  /*
32  like a destructor, need to call this before deleting
33  the property.
34*/
35  aboutToBeDeleted() {
36    this.source_.unlinkSuscriber(this.id__());
37    this.source_ = undefined;
38    super.aboutToBeDeleted();
39  }
40
41  // this object is subscriber to  SynchedPropertySimpleTwoWay
42  // will call this cb function when property has changed
43  // a set (newValue) is not done because get reads through for the source_
44  hasChanged(newValue: T): void {
45    console.debug(`SynchedPropertySimpleTwoWay[${this.id__()}, '${this.info() || "unknown"}']: hasChanged to '${newValue}'.`)
46    this.notifyHasChanged(newValue);
47  }
48
49  // get 'read through` from the ObservedProperty
50  public get(): T {
51    console.debug(`SynchedPropertySimpleTwoWay[${this.id__()}IP, '${this.info() || "unknown"}']: get`)
52    this.notifyPropertyRead();
53    if (this.contentObserver_) {
54      return this.contentObserver_.get();
55    }
56    return this.source_.get();
57  }
58
59  // set 'writes through` to the ObservedProperty
60  public set(newValue: T): void {
61    if (this.contentObserver_) {
62      this.contentObserver_.set(newValue);
63      return;
64    }
65
66    if (this.source_.get() == newValue) {
67      console.debug(`SynchedPropertySimpleTwoWay[${this.id__()}IP, '${this.info() || "unknown"}']: set with unchanged value '${newValue}'- ignoring.`);
68      return;
69    }
70
71    console.debug(`SynchedPropertySimpleTwoWay[${this.id__()}IP, '${this.info() || "unknown"}']: set to newValue: '${newValue}'.`);
72    // the source_ ObservedProeprty will call: this.hasChanged(newValue);
73    this.notifyHasChanged(newValue);
74    return this.source_.set(newValue);
75  }
76
77  /**
78* These functions are meant for use in connection with the App Stoage and
79* business logic implementation.
80* the created Link and Prop will update when 'this' property value
81* changes.
82*/
83  public createLink(subscribeOwner?: IPropertySubscriber,
84    linkPropName?: PropertyInfo, contentObserver?: ObservedPropertyAbstract<T>): ObservedPropertyAbstract<T> {
85    return new SynchedPropertySimpleTwoWay(this, subscribeOwner, linkPropName, contentObserver);
86  }
87
88  public createProp(subscribeOwner?: IPropertySubscriber,
89    propPropName?: PropertyInfo, contentObserver?: ObservedPropertyAbstract<T>): ObservedPropertyAbstract<T> {
90    return new SynchedPropertySimpleOneWaySubscribing(this, subscribeOwner, propPropName, contentObserver);
91  }
92
93}
94