mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
chore: default tabs
This commit is contained in:
244
nativescript-core/data/observable/index.d.ts
vendored
244
nativescript-core/data/observable/index.d.ts
vendored
@@ -2,32 +2,32 @@
|
||||
* Base event data.
|
||||
*/
|
||||
export interface EventData {
|
||||
/**
|
||||
* The name of the event.
|
||||
*/
|
||||
eventName: string;
|
||||
/**
|
||||
* The Observable instance that has raised the event.
|
||||
*/
|
||||
object: Observable;
|
||||
/**
|
||||
* The name of the event.
|
||||
*/
|
||||
eventName: string;
|
||||
/**
|
||||
* The Observable instance that has raised the event.
|
||||
*/
|
||||
object: Observable;
|
||||
}
|
||||
|
||||
/**
|
||||
* Data for the "propertyChange" event.
|
||||
*/
|
||||
export interface PropertyChangeData extends EventData {
|
||||
/**
|
||||
* The name of the property that has changed.
|
||||
*/
|
||||
propertyName: string;
|
||||
/**
|
||||
* The new value of the property.
|
||||
*/
|
||||
value: any;
|
||||
/**
|
||||
* The previous value of the property.
|
||||
*/
|
||||
oldValue?: any;
|
||||
/**
|
||||
* The name of the property that has changed.
|
||||
*/
|
||||
propertyName: string;
|
||||
/**
|
||||
* The new value of the property.
|
||||
*/
|
||||
value: any;
|
||||
/**
|
||||
* The previous value of the property.
|
||||
*/
|
||||
oldValue?: any;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -36,133 +36,133 @@ export interface PropertyChangeData extends EventData {
|
||||
* By wrapping object into a WrappedValue instance `same object restriction` will be passed.
|
||||
*/
|
||||
export class WrappedValue {
|
||||
/**
|
||||
* Property which holds the real value.
|
||||
*/
|
||||
wrapped: any;
|
||||
/**
|
||||
* Property which holds the real value.
|
||||
*/
|
||||
wrapped: any;
|
||||
|
||||
/**
|
||||
* Creates an instance of WrappedValue object.
|
||||
* @param value - the real value which should be wrapped.
|
||||
*/
|
||||
constructor(value: any);
|
||||
/**
|
||||
* Creates an instance of WrappedValue object.
|
||||
* @param value - the real value which should be wrapped.
|
||||
*/
|
||||
constructor(value: any);
|
||||
|
||||
/**
|
||||
* Gets the real value of previously wrappedValue.
|
||||
* @param value - Value that should be unwraped. If there is no wrappedValue property of the value object then value will be returned.
|
||||
*/
|
||||
static unwrap(value: any): any;
|
||||
/**
|
||||
* Gets the real value of previously wrappedValue.
|
||||
* @param value - Value that should be unwraped. If there is no wrappedValue property of the value object then value will be returned.
|
||||
*/
|
||||
static unwrap(value: any): any;
|
||||
|
||||
/**
|
||||
* Returns an instance of WrappedValue. The actual instance is get from a WrappedValues pool.
|
||||
* @param value - Value that should be wrapped.
|
||||
*/
|
||||
static wrap(value: any): WrappedValue;
|
||||
/**
|
||||
* Returns an instance of WrappedValue. The actual instance is get from a WrappedValues pool.
|
||||
* @param value - Value that should be wrapped.
|
||||
*/
|
||||
static wrap(value: any): WrappedValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Observable is used when you want to be notified when a change occurs. Use on/off methods to add/remove listener.
|
||||
*/
|
||||
export class Observable {
|
||||
/**
|
||||
* Please note that should you be using the `new Observable({})` constructor, it is **obsolete** since v3.0,
|
||||
* and you have to migrate to the "data/observable" `fromObject({})` or the `fromObjectRecursive({})` functions.
|
||||
*/
|
||||
constructor();
|
||||
/**
|
||||
* Please note that should you be using the `new Observable({})` constructor, it is **obsolete** since v3.0,
|
||||
* and you have to migrate to the "data/observable" `fromObject({})` or the `fromObjectRecursive({})` functions.
|
||||
*/
|
||||
constructor();
|
||||
|
||||
/**
|
||||
* String value used when hooking to propertyChange event.
|
||||
*/
|
||||
public static propertyChangeEvent: string;
|
||||
/**
|
||||
* String value used when hooking to propertyChange event.
|
||||
*/
|
||||
public static propertyChangeEvent: string;
|
||||
|
||||
/**
|
||||
* A basic method signature to hook an event listener (shortcut alias to the addEventListener method).
|
||||
* @param eventNames - String corresponding to events (e.g. "propertyChange"). Optionally could be used more events separated by `,` (e.g. "propertyChange", "change").
|
||||
* @param callback - Callback function which will be executed when event is raised.
|
||||
* @param thisArg - An optional parameter which will be used as `this` context for callback execution.
|
||||
*/
|
||||
on(eventNames: string, callback: (data: EventData) => void, thisArg?: any);
|
||||
/**
|
||||
* A basic method signature to hook an event listener (shortcut alias to the addEventListener method).
|
||||
* @param eventNames - String corresponding to events (e.g. "propertyChange"). Optionally could be used more events separated by `,` (e.g. "propertyChange", "change").
|
||||
* @param callback - Callback function which will be executed when event is raised.
|
||||
* @param thisArg - An optional parameter which will be used as `this` context for callback execution.
|
||||
*/
|
||||
on(eventNames: string, callback: (data: EventData) => void, thisArg?: any);
|
||||
|
||||
/**
|
||||
* Raised when a propertyChange occurs.
|
||||
*/
|
||||
on(event: 'propertyChange', callback: (data: EventData) => void, thisArg?: any);
|
||||
/**
|
||||
* Raised when a propertyChange occurs.
|
||||
*/
|
||||
on(event: 'propertyChange', callback: (data: EventData) => void, thisArg?: any);
|
||||
|
||||
/**
|
||||
* Adds one-time listener function for the event named `event`.
|
||||
* @param event Name of the event to attach to.
|
||||
* @param callback A function to be called when the specified event is raised.
|
||||
* @param thisArg An optional parameter which when set will be used as "this" in callback method call.
|
||||
*/
|
||||
once(event: string, callback: (data: EventData) => void, thisArg?: any);
|
||||
/**
|
||||
* Adds one-time listener function for the event named `event`.
|
||||
* @param event Name of the event to attach to.
|
||||
* @param callback A function to be called when the specified event is raised.
|
||||
* @param thisArg An optional parameter which when set will be used as "this" in callback method call.
|
||||
*/
|
||||
once(event: string, callback: (data: EventData) => void, thisArg?: any);
|
||||
|
||||
/**
|
||||
* Shortcut alias to the removeEventListener method.
|
||||
*/
|
||||
off(eventNames: string, callback?: any, thisArg?: any);
|
||||
/**
|
||||
* Shortcut alias to the removeEventListener method.
|
||||
*/
|
||||
off(eventNames: string, callback?: any, thisArg?: any);
|
||||
|
||||
/**
|
||||
* Adds a listener for the specified event name.
|
||||
* @param eventNames Comma delimited names of the events to attach the listener to.
|
||||
* @param callback A function to be called when some of the specified event(s) is raised.
|
||||
* @param thisArg An optional parameter which when set will be used as "this" in callback method call.
|
||||
*/
|
||||
addEventListener(eventNames: string, callback: (data: EventData) => void, thisArg?: any);
|
||||
/**
|
||||
* Adds a listener for the specified event name.
|
||||
* @param eventNames Comma delimited names of the events to attach the listener to.
|
||||
* @param callback A function to be called when some of the specified event(s) is raised.
|
||||
* @param thisArg An optional parameter which when set will be used as "this" in callback method call.
|
||||
*/
|
||||
addEventListener(eventNames: string, callback: (data: EventData) => void, thisArg?: any);
|
||||
|
||||
/**
|
||||
* Removes listener(s) for the specified event name.
|
||||
* @param eventNames Comma delimited names of the events the specified listener is associated with.
|
||||
* @param callback An optional parameter pointing to a specific listener. If not defined, all listeners for the event names will be removed.
|
||||
* @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.
|
||||
*/
|
||||
removeEventListener(eventNames: string, callback?: any, thisArg?: any);
|
||||
/**
|
||||
* Removes listener(s) for the specified event name.
|
||||
* @param eventNames Comma delimited names of the events the specified listener is associated with.
|
||||
* @param callback An optional parameter pointing to a specific listener. If not defined, all listeners for the event names will be removed.
|
||||
* @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.
|
||||
*/
|
||||
removeEventListener(eventNames: string, callback?: any, thisArg?: any);
|
||||
|
||||
/**
|
||||
* Updates the specified property with the provided value.
|
||||
*/
|
||||
set(name: string, value: any): void;
|
||||
/**
|
||||
* Updates the specified property with the provided value.
|
||||
*/
|
||||
set(name: string, value: any): void;
|
||||
|
||||
/**
|
||||
* Updates the specified property with the provided value and raises a property change event and a specific change event based on the property name.
|
||||
*/
|
||||
setProperty(name: string, value: any): void;
|
||||
/**
|
||||
* Updates the specified property with the provided value and raises a property change event and a specific change event based on the property name.
|
||||
*/
|
||||
setProperty(name: string, value: any): void;
|
||||
|
||||
/**
|
||||
* Gets the value of the specified property.
|
||||
*/
|
||||
get(name: string): any;
|
||||
/**
|
||||
* Gets the value of the specified property.
|
||||
*/
|
||||
get(name: string): any;
|
||||
|
||||
/**
|
||||
* Notifies all the registered listeners for the event provided in the data.eventName.
|
||||
* @param data The data associated with the event.
|
||||
*/
|
||||
notify<T extends EventData>(data: T): void;
|
||||
/**
|
||||
* Notifies all the registered listeners for the event provided in the data.eventName.
|
||||
* @param data The data associated with the event.
|
||||
*/
|
||||
notify<T extends EventData>(data: T): void;
|
||||
|
||||
/**
|
||||
* Notifies all the registered listeners for the property change event.
|
||||
*/
|
||||
notifyPropertyChange(propertyName: string, value: any, oldValue?: any): void;
|
||||
/**
|
||||
* Notifies all the registered listeners for the property change event.
|
||||
*/
|
||||
notifyPropertyChange(propertyName: string, value: any, oldValue?: any): void;
|
||||
|
||||
/**
|
||||
* Checks whether a listener is registered for the specified event name.
|
||||
* @param eventName The name of the event to check for.
|
||||
*/
|
||||
hasListeners(eventName: string): boolean;
|
||||
/**
|
||||
* Checks whether a listener is registered for the specified event name.
|
||||
* @param eventName The name of the event to check for.
|
||||
*/
|
||||
hasListeners(eventName: string): boolean;
|
||||
|
||||
public _emit(eventNames: string);
|
||||
public _emit(eventNames: string);
|
||||
|
||||
/**
|
||||
* This method is intended to be overriden by inheritors to provide additional implementation.
|
||||
*/
|
||||
_createPropertyChangeData(name: string, value: any, oldValue?: any): PropertyChangeData;
|
||||
/**
|
||||
* This method is intended to be overriden by inheritors to provide additional implementation.
|
||||
*/
|
||||
_createPropertyChangeData(name: string, value: any, oldValue?: any): PropertyChangeData;
|
||||
|
||||
//@private
|
||||
/**
|
||||
* Filed to use instead of instanceof ViewBase.
|
||||
* @private
|
||||
*/
|
||||
public _isViewBase: boolean;
|
||||
//@endprivate
|
||||
//@private
|
||||
/**
|
||||
* Filed to use instead of instanceof ViewBase.
|
||||
* @private
|
||||
*/
|
||||
public _isViewBase: boolean;
|
||||
//@endprivate
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,275 +1,275 @@
|
||||
import { Observable as ObservableDefinition, WrappedValue as WrappedValueDefinition } from '.';
|
||||
|
||||
export interface EventData {
|
||||
eventName: string;
|
||||
object: Observable;
|
||||
eventName: string;
|
||||
object: Observable;
|
||||
}
|
||||
|
||||
export interface PropertyChangeData extends EventData {
|
||||
propertyName: string;
|
||||
value: any;
|
||||
oldValue?: any;
|
||||
propertyName: string;
|
||||
value: any;
|
||||
oldValue?: any;
|
||||
}
|
||||
|
||||
interface ListenerEntry {
|
||||
callback: (data: EventData) => void;
|
||||
thisArg: any;
|
||||
once?: true;
|
||||
callback: (data: EventData) => void;
|
||||
thisArg: any;
|
||||
once?: true;
|
||||
}
|
||||
|
||||
let _wrappedIndex = 0;
|
||||
|
||||
export class WrappedValue implements WrappedValueDefinition {
|
||||
constructor(public wrapped: any) {}
|
||||
constructor(public wrapped: any) {}
|
||||
|
||||
public static unwrap(value: any) {
|
||||
return value instanceof WrappedValue ? value.wrapped : value;
|
||||
}
|
||||
public static unwrap(value: any) {
|
||||
return value instanceof WrappedValue ? value.wrapped : value;
|
||||
}
|
||||
|
||||
public static wrap(value: any) {
|
||||
const w = _wrappedValues[_wrappedIndex++ % 5];
|
||||
w.wrapped = value;
|
||||
public static wrap(value: any) {
|
||||
const w = _wrappedValues[_wrappedIndex++ % 5];
|
||||
w.wrapped = value;
|
||||
|
||||
return w;
|
||||
}
|
||||
return w;
|
||||
}
|
||||
}
|
||||
|
||||
let _wrappedValues = [new WrappedValue(null), new WrappedValue(null), new WrappedValue(null), new WrappedValue(null), new WrappedValue(null)];
|
||||
|
||||
export class Observable implements ObservableDefinition {
|
||||
public static propertyChangeEvent = 'propertyChange';
|
||||
public _isViewBase: boolean;
|
||||
public static propertyChangeEvent = 'propertyChange';
|
||||
public _isViewBase: boolean;
|
||||
|
||||
private _observers = {};
|
||||
private _observers = {};
|
||||
|
||||
public get(name: string): any {
|
||||
return this[name];
|
||||
}
|
||||
public get(name: string): any {
|
||||
return this[name];
|
||||
}
|
||||
|
||||
public set(name: string, value: any) {
|
||||
// TODO: Parameter validation
|
||||
const oldValue = this[name];
|
||||
if (this[name] === value) {
|
||||
return;
|
||||
}
|
||||
public set(name: string, value: any) {
|
||||
// TODO: Parameter validation
|
||||
const oldValue = this[name];
|
||||
if (this[name] === value) {
|
||||
return;
|
||||
}
|
||||
|
||||
const newValue = WrappedValue.unwrap(value);
|
||||
this[name] = newValue;
|
||||
this.notifyPropertyChange(name, newValue, oldValue);
|
||||
}
|
||||
const newValue = WrappedValue.unwrap(value);
|
||||
this[name] = newValue;
|
||||
this.notifyPropertyChange(name, newValue, oldValue);
|
||||
}
|
||||
|
||||
public setProperty(name: string, value: any) {
|
||||
const oldValue = this[name];
|
||||
if (this[name] === value) {
|
||||
return;
|
||||
}
|
||||
this[name] = value;
|
||||
this.notifyPropertyChange(name, value, oldValue);
|
||||
public setProperty(name: string, value: any) {
|
||||
const oldValue = this[name];
|
||||
if (this[name] === value) {
|
||||
return;
|
||||
}
|
||||
this[name] = value;
|
||||
this.notifyPropertyChange(name, value, oldValue);
|
||||
|
||||
const specificPropertyChangeEventName = name + 'Change';
|
||||
if (this.hasListeners(specificPropertyChangeEventName)) {
|
||||
const eventData = this._createPropertyChangeData(name, value, oldValue);
|
||||
eventData.eventName = specificPropertyChangeEventName;
|
||||
this.notify(eventData);
|
||||
}
|
||||
}
|
||||
const specificPropertyChangeEventName = name + 'Change';
|
||||
if (this.hasListeners(specificPropertyChangeEventName)) {
|
||||
const eventData = this._createPropertyChangeData(name, value, oldValue);
|
||||
eventData.eventName = specificPropertyChangeEventName;
|
||||
this.notify(eventData);
|
||||
}
|
||||
}
|
||||
|
||||
public on(eventNames: string, callback: (data: EventData) => void, thisArg?: any) {
|
||||
this.addEventListener(eventNames, callback, thisArg);
|
||||
}
|
||||
public on(eventNames: string, callback: (data: EventData) => void, thisArg?: any) {
|
||||
this.addEventListener(eventNames, callback, thisArg);
|
||||
}
|
||||
|
||||
public once(event: string, callback: (data: EventData) => void, thisArg?: any) {
|
||||
const list = this._getEventList(event, true);
|
||||
list.push({ callback, thisArg, once: true });
|
||||
}
|
||||
public once(event: string, callback: (data: EventData) => void, thisArg?: any) {
|
||||
const list = this._getEventList(event, true);
|
||||
list.push({ callback, thisArg, once: true });
|
||||
}
|
||||
|
||||
public off(eventNames: string, callback?: any, thisArg?: any) {
|
||||
this.removeEventListener(eventNames, callback, thisArg);
|
||||
}
|
||||
public off(eventNames: string, callback?: any, thisArg?: any) {
|
||||
this.removeEventListener(eventNames, callback, thisArg);
|
||||
}
|
||||
|
||||
public addEventListener(eventNames: string, callback: (data: EventData) => void, thisArg?: Object) {
|
||||
if (typeof eventNames !== 'string') {
|
||||
throw new TypeError('Events name(s) must be string.');
|
||||
}
|
||||
public addEventListener(eventNames: string, callback: (data: EventData) => void, thisArg?: Object) {
|
||||
if (typeof eventNames !== 'string') {
|
||||
throw new TypeError('Events name(s) must be string.');
|
||||
}
|
||||
|
||||
if (typeof callback !== 'function') {
|
||||
throw new TypeError('callback must be function.');
|
||||
}
|
||||
if (typeof callback !== 'function') {
|
||||
throw new TypeError('callback must be function.');
|
||||
}
|
||||
|
||||
const events = eventNames.split(',');
|
||||
for (let i = 0, l = events.length; i < l; i++) {
|
||||
const event = events[i].trim();
|
||||
const list = this._getEventList(event, true);
|
||||
// TODO: Performance optimization - if we do not have the thisArg specified, do not wrap the callback in additional object (ObserveEntry)
|
||||
list.push({
|
||||
callback: callback,
|
||||
thisArg: thisArg,
|
||||
});
|
||||
}
|
||||
}
|
||||
const events = eventNames.split(',');
|
||||
for (let i = 0, l = events.length; i < l; i++) {
|
||||
const event = events[i].trim();
|
||||
const list = this._getEventList(event, true);
|
||||
// TODO: Performance optimization - if we do not have the thisArg specified, do not wrap the callback in additional object (ObserveEntry)
|
||||
list.push({
|
||||
callback: callback,
|
||||
thisArg: thisArg,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public removeEventListener(eventNames: string, callback?: any, thisArg?: Object) {
|
||||
if (typeof eventNames !== 'string') {
|
||||
throw new TypeError('Events name(s) must be string.');
|
||||
}
|
||||
public removeEventListener(eventNames: string, callback?: any, thisArg?: Object) {
|
||||
if (typeof eventNames !== 'string') {
|
||||
throw new TypeError('Events name(s) must be string.');
|
||||
}
|
||||
|
||||
if (callback && typeof callback !== 'function') {
|
||||
throw new TypeError('callback must be function.');
|
||||
}
|
||||
if (callback && typeof callback !== 'function') {
|
||||
throw new TypeError('callback must be function.');
|
||||
}
|
||||
|
||||
const events = eventNames.split(',');
|
||||
for (let i = 0, l = events.length; i < l; i++) {
|
||||
const event = events[i].trim();
|
||||
if (callback) {
|
||||
const list = this._getEventList(event, false);
|
||||
if (list) {
|
||||
const index = this._indexOfListener(list, callback, thisArg);
|
||||
if (index >= 0) {
|
||||
list.splice(index, 1);
|
||||
}
|
||||
if (list.length === 0) {
|
||||
delete this._observers[event];
|
||||
}
|
||||
}
|
||||
} else {
|
||||
this._observers[event] = undefined;
|
||||
delete this._observers[event];
|
||||
}
|
||||
}
|
||||
}
|
||||
const events = eventNames.split(',');
|
||||
for (let i = 0, l = events.length; i < l; i++) {
|
||||
const event = events[i].trim();
|
||||
if (callback) {
|
||||
const list = this._getEventList(event, false);
|
||||
if (list) {
|
||||
const index = this._indexOfListener(list, callback, thisArg);
|
||||
if (index >= 0) {
|
||||
list.splice(index, 1);
|
||||
}
|
||||
if (list.length === 0) {
|
||||
delete this._observers[event];
|
||||
}
|
||||
}
|
||||
} else {
|
||||
this._observers[event] = undefined;
|
||||
delete this._observers[event];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public notify<T extends EventData>(data: T) {
|
||||
const observers = <Array<ListenerEntry>>this._observers[data.eventName];
|
||||
if (!observers) {
|
||||
return;
|
||||
}
|
||||
public notify<T extends EventData>(data: T) {
|
||||
const observers = <Array<ListenerEntry>>this._observers[data.eventName];
|
||||
if (!observers) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (let i = observers.length - 1; i >= 0; i--) {
|
||||
let entry = observers[i];
|
||||
if (entry.once) {
|
||||
observers.splice(i, 1);
|
||||
}
|
||||
if (entry.thisArg) {
|
||||
entry.callback.apply(entry.thisArg, [data]);
|
||||
} else {
|
||||
entry.callback(data);
|
||||
}
|
||||
}
|
||||
}
|
||||
for (let i = observers.length - 1; i >= 0; i--) {
|
||||
let entry = observers[i];
|
||||
if (entry.once) {
|
||||
observers.splice(i, 1);
|
||||
}
|
||||
if (entry.thisArg) {
|
||||
entry.callback.apply(entry.thisArg, [data]);
|
||||
} else {
|
||||
entry.callback(data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public notifyPropertyChange(name: string, value: any, oldValue?: any) {
|
||||
this.notify(this._createPropertyChangeData(name, value, oldValue));
|
||||
}
|
||||
public notifyPropertyChange(name: string, value: any, oldValue?: any) {
|
||||
this.notify(this._createPropertyChangeData(name, value, oldValue));
|
||||
}
|
||||
|
||||
public hasListeners(eventName: string) {
|
||||
return eventName in this._observers;
|
||||
}
|
||||
public hasListeners(eventName: string) {
|
||||
return eventName in this._observers;
|
||||
}
|
||||
|
||||
public _createPropertyChangeData(propertyName: string, value: any, oldValue?: any): PropertyChangeData {
|
||||
return {
|
||||
eventName: Observable.propertyChangeEvent,
|
||||
object: this,
|
||||
propertyName,
|
||||
value,
|
||||
oldValue,
|
||||
};
|
||||
}
|
||||
public _createPropertyChangeData(propertyName: string, value: any, oldValue?: any): PropertyChangeData {
|
||||
return {
|
||||
eventName: Observable.propertyChangeEvent,
|
||||
object: this,
|
||||
propertyName,
|
||||
value,
|
||||
oldValue,
|
||||
};
|
||||
}
|
||||
|
||||
public _emit(eventNames: string) {
|
||||
const events = eventNames.split(',');
|
||||
public _emit(eventNames: string) {
|
||||
const events = eventNames.split(',');
|
||||
|
||||
for (let i = 0, l = events.length; i < l; i++) {
|
||||
const event = events[i].trim();
|
||||
this.notify({ eventName: event, object: this });
|
||||
}
|
||||
}
|
||||
for (let i = 0, l = events.length; i < l; i++) {
|
||||
const event = events[i].trim();
|
||||
this.notify({ eventName: event, object: this });
|
||||
}
|
||||
}
|
||||
|
||||
private _getEventList(eventName: string, createIfNeeded?: boolean): Array<ListenerEntry> {
|
||||
if (!eventName) {
|
||||
throw new TypeError('EventName must be valid string.');
|
||||
}
|
||||
private _getEventList(eventName: string, createIfNeeded?: boolean): Array<ListenerEntry> {
|
||||
if (!eventName) {
|
||||
throw new TypeError('EventName must be valid string.');
|
||||
}
|
||||
|
||||
let list = <Array<ListenerEntry>>this._observers[eventName];
|
||||
if (!list && createIfNeeded) {
|
||||
list = [];
|
||||
this._observers[eventName] = list;
|
||||
}
|
||||
let list = <Array<ListenerEntry>>this._observers[eventName];
|
||||
if (!list && createIfNeeded) {
|
||||
list = [];
|
||||
this._observers[eventName] = list;
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
private _indexOfListener(list: Array<ListenerEntry>, callback: (data: EventData) => void, thisArg?: any): number {
|
||||
for (let i = 0; i < list.length; i++) {
|
||||
const entry = list[i];
|
||||
if (thisArg) {
|
||||
if (entry.callback === callback && entry.thisArg === thisArg) {
|
||||
return i;
|
||||
}
|
||||
} else {
|
||||
if (entry.callback === callback) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
}
|
||||
private _indexOfListener(list: Array<ListenerEntry>, callback: (data: EventData) => void, thisArg?: any): number {
|
||||
for (let i = 0; i < list.length; i++) {
|
||||
const entry = list[i];
|
||||
if (thisArg) {
|
||||
if (entry.callback === callback && entry.thisArg === thisArg) {
|
||||
return i;
|
||||
}
|
||||
} else {
|
||||
if (entry.callback === callback) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
class ObservableFromObject extends Observable {
|
||||
public _map = {};
|
||||
public _map = {};
|
||||
|
||||
public get(name: string): any {
|
||||
return this._map[name];
|
||||
}
|
||||
public get(name: string): any {
|
||||
return this._map[name];
|
||||
}
|
||||
|
||||
public set(name: string, value: any) {
|
||||
const currentValue = this._map[name];
|
||||
if (currentValue === value) {
|
||||
return;
|
||||
}
|
||||
public set(name: string, value: any) {
|
||||
const currentValue = this._map[name];
|
||||
if (currentValue === value) {
|
||||
return;
|
||||
}
|
||||
|
||||
const newValue = WrappedValue.unwrap(value);
|
||||
this._map[name] = newValue;
|
||||
this.notifyPropertyChange(name, newValue, currentValue);
|
||||
}
|
||||
const newValue = WrappedValue.unwrap(value);
|
||||
this._map[name] = newValue;
|
||||
this.notifyPropertyChange(name, newValue, currentValue);
|
||||
}
|
||||
}
|
||||
|
||||
function defineNewProperty(target: ObservableFromObject, propertyName: string): void {
|
||||
Object.defineProperty(target, propertyName, {
|
||||
get: function () {
|
||||
return target._map[propertyName];
|
||||
},
|
||||
set: function (value) {
|
||||
target.set(propertyName, value);
|
||||
},
|
||||
enumerable: true,
|
||||
configurable: true,
|
||||
});
|
||||
Object.defineProperty(target, propertyName, {
|
||||
get: function () {
|
||||
return target._map[propertyName];
|
||||
},
|
||||
set: function (value) {
|
||||
target.set(propertyName, value);
|
||||
},
|
||||
enumerable: true,
|
||||
configurable: true,
|
||||
});
|
||||
}
|
||||
|
||||
function addPropertiesFromObject(observable: ObservableFromObject, source: any, recursive: boolean = false) {
|
||||
Object.keys(source).forEach((prop) => {
|
||||
let value = source[prop];
|
||||
if (recursive && !Array.isArray(value) && value && typeof value === 'object' && !(value instanceof Observable)) {
|
||||
value = fromObjectRecursive(value);
|
||||
}
|
||||
Object.keys(source).forEach((prop) => {
|
||||
let value = source[prop];
|
||||
if (recursive && !Array.isArray(value) && value && typeof value === 'object' && !(value instanceof Observable)) {
|
||||
value = fromObjectRecursive(value);
|
||||
}
|
||||
|
||||
defineNewProperty(observable, prop);
|
||||
observable.set(prop, value);
|
||||
});
|
||||
defineNewProperty(observable, prop);
|
||||
observable.set(prop, value);
|
||||
});
|
||||
}
|
||||
|
||||
export function fromObject(source: any): Observable {
|
||||
let observable = new ObservableFromObject();
|
||||
addPropertiesFromObject(observable, source, false);
|
||||
let observable = new ObservableFromObject();
|
||||
addPropertiesFromObject(observable, source, false);
|
||||
|
||||
return observable;
|
||||
return observable;
|
||||
}
|
||||
|
||||
export function fromObjectRecursive(source: any): Observable {
|
||||
let observable = new ObservableFromObject();
|
||||
addPropertiesFromObject(observable, source, true);
|
||||
let observable = new ObservableFromObject();
|
||||
addPropertiesFromObject(observable, source, true);
|
||||
|
||||
return observable;
|
||||
return observable;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user