MCPcopy Index your code
hub / github.com/NativeScript/NativeScript / Observable

Class Observable

packages/core/data/observable/index.ts:101–515  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

99 * and you have to migrate to the "data/observable" `fromObject({})` or the `fromObjectRecursive({})` functions.
100 */
101export class Observable {
102 /**
103 * String value used when hooking to propertyChange event.
104 * @nsEvent {PropertyChangeData} propertyChange
105 */
106 public static propertyChangeEvent = 'propertyChange';
107
108 /**
109 * Alternative to `instanceof ViewBase`.
110 * @private
111 */
112 public _isViewBase: boolean;
113
114 private readonly _observers: ListEntryMap = {};
115
116 /**
117 * Gets the value of the specified property.
118 */
119 public get(name: string): any {
120 return this[name];
121 }
122
123 /**
124 * Updates the specified property with the provided value.
125 */
126 public set(name: string, value: any): void {
127 // TODO: Parameter validation
128 const oldValue = this[name];
129 if (this[name] === value) {
130 return;
131 }
132
133 const newValue = WrappedValue.unwrap(value);
134 this[name] = newValue;
135 this.notifyPropertyChange(name, newValue, oldValue);
136 }
137
138 /**
139 * Updates the specified property with the provided value and raises a property change event and a specific change event based on the property name.
140 */
141 public setProperty(name: string, value: any): void {
142 const oldValue = this[name];
143 if (this[name] === value) {
144 return;
145 }
146 this[name] = value;
147 this.notifyPropertyChange(name, value, oldValue);
148
149 const specificPropertyChangeEventName = name + 'Change';
150 if (this.hasListeners(specificPropertyChangeEventName)) {
151 const eventData = this._createPropertyChangeData(name, value, oldValue);
152 eventData.eventName = specificPropertyChangeEventName;
153 this.notify(eventData);
154 }
155 }
156
157 /**
158 * Adds a listener for the specified event name.

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected