• 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 SynchedPropertySimpleOneWay<T> extends ObservedPropertySimpleAbstract<T> {
17
18  private wrappedValue_: T;
19  private contentStorageLinkedParentProperty_: ObservedPropertySimpleAbstract<T>;
20
21  constructor(value: T, subscribeMe?: IPropertySubscriber, info?: PropertyInfo,
22    contentObserver?: ObservedPropertySimpleAbstract<T>) {
23    super(subscribeMe, info);
24    // TODO prop is only supported for simple types
25    // add a test here that T is a simple type
26    this.wrappedValue_ = value;
27    if (contentObserver) {
28      this.contentStorageLinkedParentProperty_ = contentObserver;
29    }
30  }
31
32  /*
33    like a destructor, need to call this before deleting
34    the property.
35  */
36  aboutToBeDeleted() {
37    super.aboutToBeDeleted();
38  }
39
40
41  // get 'read through` from the ObservedProperty
42  public get(): T {
43    console.debug(`SynchedPropertySimpleOneWay[${this.id__()}, '${this.info() || "unknown"}']: get returns '${this.wrappedValue_}'`);
44    this.notifyPropertyRead();
45    if (this.contentStorageLinkedParentProperty_) {
46      return this.contentStorageLinkedParentProperty_.get();
47    }
48    return this.wrappedValue_;
49  }
50
51  public set(newValue: T): void {
52    if (this.contentStorageLinkedParentProperty_) {
53      this.contentStorageLinkedParentProperty_.set(newValue);
54      return;
55    }
56    if (this.wrappedValue_ == newValue) {
57      console.debug(`SynchedPropertySimpleOneWay[${this.id__()}, '${this.info() || "unknown"}']: set with unchanged value '${this.wrappedValue_}'- ignoring.`);
58      return;
59    }
60
61    console.debug(`SynchedPropertySimpleOneWay[${this.id__()}, '${this.info() || "unknown"}']: set from '${this.wrappedValue_} to '${newValue}'.`);
62    this.wrappedValue_ = newValue;
63    this.notifyHasChanged(newValue);
64  }
65
66  /**
67   * These functions are meant for use in connection with the App Stoage and
68   * business logic implementation.
69   * the created Link and Prop will update when 'this' property value
70   * changes.
71   */
72  public createLink(subscribeOwner?: IPropertySubscriber,
73    linkPropName?: PropertyInfo, contentObserver?: ObservedPropertyAbstract<T>): ObservedPropertyAbstract<T> {
74    throw new Error("Can not create a 'Link' from a 'Prop' property. ");
75  }
76  public createProp(subscribeOwner?: IPropertySubscriber,
77    linkPropName?: PropertyInfo, contentObserver?: ObservedPropertyAbstract<T>): ObservedPropertyAbstract<T> {
78    throw new Error("Method not supported, create a SynchedPropertySimpleOneWaySubscribing from, where to create a Prop.");
79  }
80}
81
82
83/*
84  This exrension of SynchedPropertySimpleOneWay needs to be used for AppStorage
85  because it needs to be notified about the source property changing
86  ( there is no re-render process as in Views to update the wrappedValue )
87*/
88class SynchedPropertySimpleOneWaySubscribing<T> extends SynchedPropertySimpleOneWay<T>
89  implements ISinglePropertyChangeSubscriber<T> {
90
91  private linkedParentProperty_: ObservedPropertySimpleAbstract<T>;
92
93  constructor(linkedProperty: ObservedPropertySimpleAbstract<T>, subscribeMe?: IPropertySubscriber, info?: PropertyInfo,
94    contentObserver?: ObservedPropertyAbstract<T>) {
95    super(linkedProperty.get(), subscribeMe, info, contentObserver);
96    this.linkedParentProperty_ = linkedProperty;
97    this.linkedParentProperty_.subscribeMe(this);
98  }
99
100
101  aboutToBeDeleted() {
102    // unregister from parent of this prop
103    this.linkedParentProperty_.unlinkSuscriber(this.id__());
104    super.aboutToBeDeleted();
105  }
106
107
108  hasChanged(newValue: T): void {
109    console.debug(`SynchedPropertySimpleOneWaySubscribing[${this.id__()}, '${this.info() || "unknown"}']: source property hasChanged'.`)
110    this.set(newValue);
111  }
112
113  /**
114   * These functions are meant for use in connection with the App Stoage and
115   * business logic implementation.
116   * the created Link and Prop will update when 'this' property value
117   * changes.
118   */
119  public createLink(subscribeOwner?: IPropertySubscriber,
120    linkPropName?: PropertyInfo, contentObserver?: ObservedPropertyAbstract<T>): ObservedPropertyAbstract<T> {
121    throw new Error("Can not create a 'Link' from a 'Prop' property. ");
122  }
123  public createProp(subscribeOwner?: IPropertySubscriber,
124    propPropName?: PropertyInfo, contentObserver?: ObservedPropertyAbstract<T>): ObservedPropertyAbstract<T> {
125    return new SynchedPropertySimpleOneWaySubscribing<T>(this, subscribeOwner, propPropName, contentObserver);
126  }
127}
128