• 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
16/*
17  class that holds an actual property value of type T
18  uses its base class to manage subscribers to this
19  property.
20*/
21class ObservedPropertySimple<T> extends ObservedPropertySimpleAbstract<T>
22  implements ISinglePropertyChangeSubscriber<T> {
23
24  private wrappedValue_: T;
25
26  constructor(value: T, owningView: IPropertySubscriber, propertyName: PropertyInfo) {
27    super(owningView, propertyName);
28    if (typeof value === "object") {
29      throw new SyntaxError("ObservedPropertySimple value must not be an object")!
30    }
31    this.setValueInternal(value);
32  }
33
34  aboutToBeDeleted(unsubscribeMe?: IPropertySubscriber) {
35    if (unsubscribeMe) {
36      this.unlinkSuscriber(unsubscribeMe.id__());
37    }
38    super.aboutToBeDeleted();
39  }
40
41  hasChanged(newValue: T): void {
42    console.debug(`ObservedPropertySimple[${this.id__()}, '${this.info() || "unknown"}']: hasChanged`);
43    this.notifyHasChanged(this.wrappedValue_);
44  }
45
46  /*
47    actually update this.wrappedValue_
48    called needs to do value change check
49    and also notify with this.aboutToChange();
50  */
51  private setValueInternal(newValue: T): void {
52    console.debug(`ObservedPropertySimple[${this.id__()}, '${this.info() || "unknown"}'] new value is of simple type`);
53    this.wrappedValue_ = newValue;
54  }
55
56
57  public get(): T {
58    console.debug(`ObservedPropertySimple[${this.id__()}, '${this.info() || "unknown"}']: get returns '${JSON.stringify(this.wrappedValue_)}' .`);
59    this.notifyPropertyRead();
60    return this.wrappedValue_;
61  }
62
63  public set(newValue: T): void {
64    if (this.wrappedValue_ == newValue) {
65      console.debug(`ObservedPropertySimple[${this.id__()}, '${this.info() || "unknown"}']: set with unchanged value - ignoring.`);
66      return;
67    }
68    console.debug(`ObservedPropertySimple[${this.id__()}, '${this.info() || "unknown"}']: set, changed from '${JSON.stringify(this.wrappedValue_)}' to '${JSON.stringify(newValue)}.`);
69    this.setValueInternal(newValue);
70    this.notifyHasChanged(newValue);
71  }
72
73  /**
74 * These functions are meant for use in connection with the App Stoage and
75 * business logic implementation.
76 * the created Link and Prop will update when 'this' property value
77 * changes.
78 */
79  public createLink(subscribeOwner?: IPropertySubscriber,
80    linkPropName?: PropertyInfo, contentObserver?: ObservedPropertyAbstract<T>): ObservedPropertyAbstract<T> {
81    return new SynchedPropertySimpleTwoWay(this, subscribeOwner, linkPropName, contentObserver);
82  }
83  public createProp(subscribeOwner?: IPropertySubscriber,
84    linkPropName?: PropertyInfo, contentObserver?: ObservedPropertyAbstract<T>): ObservedPropertyAbstract<T> {
85    return new SynchedPropertySimpleOneWaySubscribing(this, subscribeOwner, linkPropName, contentObserver);
86  }
87}
88