UNPKG

7.42 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 * A basic method signature to hook an event listener (shortcut alias to the addEventListener method).
95 * @param eventNames - String corresponding to events (e.g. "propertyChange"). Optionally could be used more events separated by `,` (e.g. "propertyChange", "change").
96 * @param callback - Callback function which will be executed when event is raised.
97 * @param thisArg - An optional parameter which will be used as `this` context for callback execution.
98 */
99 on(eventNames: string, callback: (data: EventData) => void, thisArg?: any): void;
100 /**
101 * Adds one-time listener function for the event named `event`.
102 * @param event Name of the event to attach to.
103 * @param callback A function to be called when the specified event is raised.
104 * @param thisArg An optional parameter which when set will be used as "this" in callback method call.
105 */
106 once(event: string, callback: (data: EventData) => void, thisArg?: any): void;
107 /**
108 * Shortcut alias to the removeEventListener method.
109 */
110 off(eventNames: string, callback?: (data: EventData) => void, thisArg?: any): void;
111 /**
112 * Adds a listener for the specified event name.
113 * @param eventNames Comma delimited names of the events to attach the listener to.
114 * @param callback A function to be called when some of the specified event(s) is raised.
115 * @param thisArg An optional parameter which when set will be used as "this" in callback method call.
116 */
117 addEventListener(eventNames: string, callback: (data: EventData) => void, thisArg?: any): void;
118 /**
119 * Removes listener(s) for the specified event name.
120 * @param eventNames Comma delimited names of the events the specified listener is associated with.
121 * @param callback An optional parameter pointing to a specific listener. If not defined, all listeners for the event names will be removed.
122 * @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.
123 */
124 removeEventListener(eventNames: string, callback?: (data: EventData) => void, thisArg?: any): void;
125 static on(eventName: string, callback: (data: EventData) => void, thisArg?: any): void;
126 static once(eventName: string, callback: (data: EventData) => void, thisArg?: any): void;
127 static off(eventName: string, callback?: (data: EventData) => void, thisArg?: any): void;
128 static removeEventListener(eventName: string, callback?: (data: EventData) => void, thisArg?: any): void;
129 static addEventListener(eventName: string, callback: (data: EventData) => void, thisArg?: any): void;
130 private _globalNotify;
131 /**
132 * Notify this Observable instance with some data. This causes all event
133 * handlers on the Observable instance to be called, as well as any 'global'
134 * event handlers set on the instance's class.
135 *
136 * @param data an object that satisfies the EventData interface, though with
137 * an optional 'object' property. If left undefined, the 'object' property
138 * will implicitly be set as this Observable instance.
139 */
140 notify<T extends Optional<EventData, 'object'>>(data: T): void;
141 private static _handleEvent;
142 /**
143 * Notifies all the registered listeners for the property change event.
144 */
145 notifyPropertyChange(name: string, value: any, oldValue?: any): void;
146 /**
147 * Checks whether a listener is registered for the specified event name.
148 * @param eventName The name of the event to check for.
149 */
150 hasListeners(eventName: string): boolean;
151 /**
152 * This method is intended to be overriden by inheritors to provide additional implementation.
153 */
154 _createPropertyChangeData(propertyName: string, value: any, oldValue?: any): PropertyChangeData;
155 _emit(eventNames: string): void;
156 private _getEventList;
157 private static _indexOfListener;
158}
159/**
160 * Creates an Observable instance and sets its properties according to the supplied JavaScript object.
161 * param obj - A JavaScript object used to initialize nativescript Observable instance.
162 */
163export declare function fromObject(source: any): Observable;
164/**
165 * Creates an Observable instance and sets its properties according to the supplied JavaScript object.
166 * This function will create new Observable for each nested object (expect arrays and functions) from supplied JavaScript object.
167 * param obj - A JavaScript object used to initialize nativescript Observable instance.
168 */
169export declare function fromObjectRecursive(source: any): Observable;