UNPKG

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