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