mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-15 19:26:42 +08:00

* chore: move tns-core-modules to nativescript-core * chore: preparing compat generate script * chore: add missing definitions * chore: no need for http-request to be private * chore: packages chore * test: generate tests for tns-core-modules * chore: add anroid module for consistency * chore: add .npmignore * chore: added privateModulesWhitelist * chore(webpack): added bundle-entry-points * chore: scripts * chore: tests changed to use @ns/core * test: add scoped-packages test project * test: fix types * test: update test project * chore: build scripts * chore: update build script * chore: npm scripts cleanup * chore: make the compat pgk work with old wp config * test: generate diff friendly tests * chore: create barrel exports * chore: move files after rebase * chore: typedoc config * chore: compat mode * chore: review of barrels * chore: remove tns-core-modules import after rebase * chore: dev workflow setup * chore: update developer-workflow * docs: experiment with API extractor * chore: api-extractor and barrel exports * chore: api-extractor configs * chore: generate d.ts rollup with api-extractor * refactor: move methods inside Frame * chore: fic tests to use Frame static methods * refactor: create Builder class * refactor: use Builder class in tests * refactor: include Style in ui barrel * chore: separate compat build script * chore: fix tslint errors * chore: update NATIVESCRIPT_CORE_ARGS * chore: fix compat pack * chore: fix ui-test-app build with linked modules * chore: Application, ApplicationSettings, Connectivity and Http * chore: export Trace, Profiling and Utils * refactor: Static create methods for ImageSource * chore: fix deprecated usages of ImageSource * chore: move Span and FormattedString to ui * chore: add events-args and ImageSource to index files * chore: check for CLI >= 6.2 when building for IOS * chore: update travis build * chore: copy Pod file to compat package * chore: update error msg ui-tests-app * refactor: Apply suggestions from code review Co-Authored-By: Martin Yankov <m.i.yankov@gmail.com> * chore: typings and refs * chore: add missing d.ts files for public API * chore: adress code review FB * chore: update api-report * chore: dev-workflow for other apps * chore: api update * chore: update api-report
262 lines
7.8 KiB
TypeScript
262 lines
7.8 KiB
TypeScript
import { Observable as ObservableDefinition, WrappedValue as WrappedValueDefinition, PropertyChangeData } from ".";
|
|
|
|
// TODO: Remove this. It is the same export as in d.ts to fix failing build when modules are linked
|
|
export interface EventData {
|
|
eventName: string;
|
|
object: ObservableDefinition;
|
|
}
|
|
|
|
interface ListenerEntry {
|
|
callback: (data: EventData) => void;
|
|
thisArg: any;
|
|
once?: true;
|
|
}
|
|
|
|
let _wrappedIndex = 0;
|
|
|
|
export class WrappedValue implements WrappedValueDefinition {
|
|
constructor(public wrapped: any) {
|
|
}
|
|
|
|
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;
|
|
|
|
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;
|
|
|
|
private _observers = {};
|
|
|
|
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;
|
|
}
|
|
|
|
const newValue = WrappedValue.unwrap(value);
|
|
this[name] = newValue;
|
|
this.notifyPropertyChange(name, newValue, oldValue);
|
|
}
|
|
|
|
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 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.");
|
|
}
|
|
|
|
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
|
|
});
|
|
}
|
|
}
|
|
|
|
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.");
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
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 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 _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 });
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
class ObservableFromObject extends Observable {
|
|
public _map = {};
|
|
|
|
public get(name: string): any {
|
|
return this._map[name];
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
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
|
|
});
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
defineNewProperty(observable, prop);
|
|
observable.set(prop, value);
|
|
});
|
|
}
|
|
|
|
export function fromObject(source: any): Observable {
|
|
let observable = new ObservableFromObject();
|
|
addPropertiesFromObject(observable, source, false);
|
|
|
|
return observable;
|
|
}
|
|
|
|
export function fromObjectRecursive(source: any): Observable {
|
|
let observable = new ObservableFromObject();
|
|
addPropertiesFromObject(observable, source, true);
|
|
|
|
return observable;
|
|
}
|