• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 * SynchedPropertySimpleOneWayPU
17 *
18 * all definitions in this file are framework internal
19 */
20
21 class SynchedPropertySimpleOneWayPU<T> extends ObservedPropertySimpleAbstractPU<T>
22    implements ISinglePropertyChangeSubscriber<T>  {
23
24  private wrappedValue_: T;
25  private source_: ObservedPropertyAbstract<T>;
26
27  constructor(source: ObservedPropertyAbstract<T> | T, subscribeMe?: IPropertySubscriber, thisPropertyName?: PropertyInfo) {
28    super(subscribeMe, thisPropertyName)
29
30    if (source && (typeof (source) === "object") && ("notifyHasChanged" in source) && ("subscribeMe" in source)) {
31      // code path for @(Local)StorageProp
32      this.source_ = source as ObservedPropertyAbstract<T>;
33      // subscribe to receive value chnage updates from LocalStorge source property
34      this.source_.subscribeMe(this);
35    } else {
36      // code path for @Prop
37      this.source_ = new ObservedPropertySimple<T>(source as T, this, thisPropertyName);
38    }
39
40    // use own backing store for value to avoid
41    // value changes to be propagated back to source
42    this.wrappedValue_ = this.source_.getUnmonitored();
43  }
44
45  /*
46    like a destructor, need to call this before deleting
47    the property.
48  */
49  aboutToBeDeleted() {
50    if (this.source_) {
51      this.source_.unlinkSuscriber(this.id__());
52      this.source_ = undefined;
53    }
54    super.aboutToBeDeleted();
55  }
56
57  // implements  ISinglePropertyChangeSubscriber<T>:
58  // this object is subscriber to this.source_
59  // when source notifies a change, copy its value to local backing store
60  public hasChanged(newValue: T): void {
61    stateMgmtConsole.debug(`SynchedPropertySimpleOneWayPU[${this.id__()}, '${this.info() || "unknown"}']: hasChanged to '${newValue}'.`)
62    this.wrappedValue_ = newValue;
63    this.notifyHasChanged(newValue);
64  }
65
66  public getUnmonitored(): T {
67    stateMgmtConsole.debug(`SynchedPropertySimpleOneWayPU[${this.id__()}, '${this.info() || "unknown"}']: getUnmonitored returns '${JSON.stringify(this.wrappedValue_)}' .`);
68    // unmonitored get access , no call to otifyPropertyRead !
69    return this.wrappedValue_;
70  }
71
72  // get 'read through` from the ObservedProperty
73  public get(): T {
74    stateMgmtConsole.debug(`SynchedPropertySimpleOneWayPU[${this.id__()}, '${this.info() || "unknown"}']: get returns '${this.wrappedValue_}'`);
75    this.notifyPropertyRead();
76    return this.wrappedValue_;
77  }
78
79  public set(newValue: T): void {
80    if (this.wrappedValue_ == newValue) {
81      stateMgmtConsole.debug(`SynchedPropertySimpleOneWayPU[${this.id__()}, '${this.info() || "unknown"}']: set with unchanged value '${this.wrappedValue_}'- ignoring.`);
82      return;
83    }
84
85    stateMgmtConsole.debug(`SynchedPropertySimpleOneWayPU[${this.id__()}, '${this.info() || "unknown"}']: set from '${this.wrappedValue_} to '${newValue}'.`);
86    this.wrappedValue_ = newValue;
87    this.notifyHasChanged(newValue);
88  }
89
90  public reset(sourceChangedValue: T): void {
91    stateMgmtConsole.debug(`SynchedPropertySimpleOneWayPU[${this.id__()}, '${this.info() || "unknown"}']: reset from '${this.wrappedValue_} to '${sourceChangedValue}'.`);
92    // if set causes an actual change, then, ObservedPropertySimple source_ will call hasChanged
93    this.source_.set(sourceChangedValue);
94  }
95}
96
97