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