UNPKG

6.53 kBTypeScriptView Raw
1/**
2 * Base event data.
3 */
4export interface EventData {
5 /**
6 * The name of the event.
7 */
8 eventName: string;
9 /**
10 * The Observable instance that has raised the event.
11 */
12 object: Observable;
13}
14
15export interface NotifyData extends Partial<EventData> {
16 eventName: string;
17 object?: Observable;
18}
19
20/**
21 * Data for the "propertyChange" event.
22 */
23export interface PropertyChangeData extends EventData {
24 /**
25 * The name of the property that has changed.
26 */
27 propertyName: string;
28 /**
29 * The new value of the property.
30 */
31 value: any;
32 /**
33 * The previous value of the property.
34 */
35 oldValue?: any;
36}
37
38/**
39 * Helper class that is used to fire property change even when real object is the same.
40 * By default property change will not be fired for a same object.
41 * By wrapping object into a WrappedValue instance `same object restriction` will be passed.
42 */
43export class WrappedValue {
44 /**
45 * Property which holds the real value.
46 */
47 wrapped: any;
48
49 /**
50 * Creates an instance of WrappedValue object.
51 * @param value - the real value which should be wrapped.
52 */
53 constructor(value: any);
54
55 /**
56 * Gets the real value of previously wrappedValue.
57 * @param value - Value that should be unwraped. If there is no wrappedValue property of the value object then value will be returned.
58 */
59 static unwrap(value: any): any;
60
61 /**
62 * Returns an instance of WrappedValue. The actual instance is get from a WrappedValues pool.
63 * @param value - Value that should be wrapped.
64 */
65 static wrap(value: any): WrappedValue;
66}
67
68/**
69 * Observable is used when you want to be notified when a change occurs. Use on/off methods to add/remove listener.
70 */
71export class Observable {
72 /**
73 * Please note that should you be using the `new Observable({})` constructor, it is **obsolete** since v3.0,
74 * and you have to migrate to the "data/observable" `fromObject({})` or the `fromObjectRecursive({})` functions.
75 */
76 constructor();
77
78 /**
79 * String value used when hooking to propertyChange event.
80 */
81 static propertyChangeEvent: string;
82
83 /**
84 * A basic method signature to hook an event listener (shortcut alias to the addEventListener method).
85 * @param eventNames - String corresponding to events (e.g. "propertyChange"). Optionally could be used more events separated by `,` (e.g. "propertyChange", "change").
86 * @param callback - Callback function which will be executed when event is raised.
87 * @param thisArg - An optional parameter which will be used as `this` context for callback execution.
88 */
89 on(eventNames: string, callback: (data: EventData) => void, thisArg?: any);
90
91 static on(eventName: string, callback: any, thisArg?: any): void;
92
93 /**
94 * Raised when a propertyChange occurs.
95 */
96 on(event: 'propertyChange', callback: (data: EventData) => void, thisArg?: any);
97
98 /**
99 * Adds one-time listener function for the event named `event`.
100 * @param event Name of the event to attach to.
101 * @param callback A function to be called when the specified event is raised.
102 * @param thisArg An optional parameter which when set will be used as "this" in callback method call.
103 */
104 once(event: string, callback: (data: EventData) => void, thisArg?: any);
105
106 static once(eventName: string, callback: any, thisArg?: any): void;
107
108 /**
109 * Shortcut alias to the removeEventListener method.
110 */
111 off(eventNames: string, callback?: any, thisArg?: any);
112
113 static off(eventName: string, callback?: any, thisArg?: any): void;
114
115 /**
116 * Adds a listener for the specified event name.
117 * @param eventNames Comma delimited names of the events to attach the listener to.
118 * @param callback A function to be called when some of the specified event(s) is raised.
119 * @param thisArg An optional parameter which when set will be used as "this" in callback method call.
120 */
121 addEventListener(eventNames: string, callback: (data: EventData) => void, thisArg?: any);
122
123 static addEventListener(eventName: string, callback: any, thisArg?: any): void;
124
125 /**
126 * Removes listener(s) for the specified event name.
127 * @param eventNames Comma delimited names of the events the specified listener is associated with.
128 * @param callback An optional parameter pointing to a specific listener. If not defined, all listeners for the event names will be removed.
129 * @param thisArg An optional parameter which when set will be used to refine search of the correct callback which will be removed as event listener.
130 */
131 removeEventListener(eventNames: string, callback?: any, thisArg?: any);
132
133 static removeEventListener(eventName: string, callback?: any, thisArg?: any): void;
134
135 /**
136 * Updates the specified property with the provided value.
137 */
138 set(name: string, value: any): void;
139
140 /**
141 * Updates the specified property with the provided value and raises a property change event and a specific change event based on the property name.
142 */
143 setProperty(name: string, value: any): void;
144
145 /**
146 * Gets the value of the specified property.
147 */
148 get(name: string): any;
149
150 /**
151 * Notifies all the registered listeners for the event provided in the data.eventName.
152 * @param data The data associated with the event.
153 */
154 notify<T extends NotifyData>(data: T): void;
155
156 /**
157 * Notifies all the registered listeners for the property change event.
158 */
159 notifyPropertyChange(propertyName: string, value: any, oldValue?: any): void;
160
161 /**
162 * Checks whether a listener is registered for the specified event name.
163 * @param eventName The name of the event to check for.
164 */
165 hasListeners(eventName: string): boolean;
166
167 public _emit(eventNames: string);
168
169 /**
170 * This method is intended to be overriden by inheritors to provide additional implementation.
171 */
172 _createPropertyChangeData(name: string, value: any, oldValue?: any): PropertyChangeData;
173
174 //@private
175 /**
176 * Filed to use instead of instanceof ViewBase.
177 * @private
178 */
179 public _isViewBase: boolean;
180 //@endprivate
181}
182
183/**
184 * Creates an Observable instance and sets its properties according to the supplied JavaScript object.
185 * param obj - A JavaScript object used to initialize nativescript Observable instance.
186 */
187export function fromObject(obj: any): Observable;
188
189/**
190 * Creates an Observable instance and sets its properties according to the supplied JavaScript object.
191 * This function will create new Observable for each nested object (expect arrays and functions) from supplied JavaScript object.
192 * param obj - A JavaScript object used to initialize nativescript Observable instance.
193 */
194export function fromObjectRecursive(obj: any): Observable;