• 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/**
17 * ObservedPropertySimplePU
18 * implementation of @State and @Provide decorated variables of types (T=) boolean | number | string | enum
19 *
20 * Holds an actual property value of type T
21 * uses its base class to manage subscribers to this
22 * property.
23 *
24 * all definitions in this file are framework internal
25*/
26class ObservedPropertySimplePU<T> extends ObservedPropertySimpleAbstractPU<T>
27  implements ISinglePropertyChangeSubscriber<T>, PeerChangeEventReceiverPU<T> {
28
29  private wrappedValue_: T;
30
31  constructor(value: T, owningView: IPropertySubscriber, propertyName: PropertyInfo) {
32    super(owningView, propertyName);
33    if (typeof value === "object") {
34      throw new SyntaxError("ObservedPropertySimple value must not be an object")!
35    }
36    this.setValueInternal(value);
37  }
38
39  aboutToBeDeleted(unsubscribeMe?: IPropertySubscriber) {
40    if (unsubscribeMe) {
41      this.unlinkSuscriber(unsubscribeMe.id__());
42    }
43    super.aboutToBeDeleted();
44  }
45
46  /**
47 * Called by a @Link - SynchedPropertySimpleTwoWay that uses this as sync peer when it has changed
48 * @param eventSource
49 */
50  syncPeerHasChanged(eventSource: ObservedPropertyAbstractPU<T>) {
51    stateMgmtConsole.debug(`ObservedPropertySimple[${this.id__()}, '${this.info() || "unknown"}']: syncPeerHasChanged peer '${eventSource.info()}'.`);
52    this.notifyPropertyHasChangedPU();
53  }
54
55  /*
56    actually update this.wrappedValue_
57    called needs to do value change check
58    and also notify with this.aboutToChange();
59  */
60  private setValueInternal(newValue: T): void {
61    stateMgmtConsole.debug(`ObservedPropertySimple[${this.id__()}, '${this.info() || "unknown"}'] new value is of simple type`);
62    this.wrappedValue_ = newValue;
63  }
64
65  public getUnmonitored(): T {
66    stateMgmtConsole.debug(`ObservedPropertySimple[${this.id__()}, '${this.info() || "unknown"}']: getUnmonitored returns '${JSON.stringify(this.wrappedValue_)}' .`);
67    // unmonitored get access , no call to otifyPropertyRead !
68    return this.wrappedValue_;
69  }
70
71  public get(): T {
72    stateMgmtConsole.debug(`ObservedPropertySimple[${this.id__()}, '${this.info() || "unknown"}']: get returns '${JSON.stringify(this.wrappedValue_)}' .`);
73    this.notifyPropertyHasBeenReadPU()
74    return this.wrappedValue_;
75  }
76
77  public set(newValue: T): void {
78    if (this.wrappedValue_ == newValue) {
79      stateMgmtConsole.debug(`ObservedPropertySimple[${this.id__()}, '${this.info() || "unknown"}']: set with unchanged value - ignoring.`);
80      return;
81    }
82    stateMgmtConsole.debug(`ObservedPropertySimple[${this.id__()}, '${this.info() || "unknown"}']: set, changed from '${JSON.stringify(this.wrappedValue_)}' to '${JSON.stringify(newValue)}.`);
83    this.setValueInternal(newValue);
84    this.notifyPropertyHasChangedPU();
85
86  }
87}
88