mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-18 22:01:42 +08:00
remove global events
should be done in a plugin
This commit is contained in:
@ -37,9 +37,6 @@ export class WrappedValue implements WrappedValueDefinition {
|
|||||||
const _wrappedValues = [new WrappedValue(null), new WrappedValue(null), new WrappedValue(null), new WrappedValue(null), new WrappedValue(null)];
|
const _wrappedValues = [new WrappedValue(null), new WrappedValue(null), new WrappedValue(null), new WrappedValue(null), new WrappedValue(null)];
|
||||||
|
|
||||||
|
|
||||||
const _globalEventHandlers = {};
|
|
||||||
|
|
||||||
|
|
||||||
export class Observable implements ObservableDefinition {
|
export class Observable implements ObservableDefinition {
|
||||||
public static propertyChangeEvent = 'propertyChange';
|
public static propertyChangeEvent = 'propertyChange';
|
||||||
public _isViewBase: boolean;
|
public _isViewBase: boolean;
|
||||||
@ -155,120 +152,15 @@ export class Observable implements ObservableDefinition {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static on(eventName: string, callback: any, thisArg?: any): void {
|
|
||||||
this.addEventListener(eventName, callback, thisArg);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static once(eventName: string, callback: any, thisArg?: any): void {
|
|
||||||
if (typeof eventName !== 'string') {
|
|
||||||
throw new TypeError('Event must be string.');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (typeof callback !== 'function') {
|
|
||||||
throw new TypeError('callback must be function.');
|
|
||||||
}
|
|
||||||
|
|
||||||
const eventClass = this.name;
|
|
||||||
if (!_globalEventHandlers[eventClass]) {
|
|
||||||
_globalEventHandlers[eventClass] = {};
|
|
||||||
}
|
|
||||||
if (!Array.isArray(_globalEventHandlers[eventClass][eventName])) {
|
|
||||||
_globalEventHandlers[eventClass][eventName] = [];
|
|
||||||
}
|
|
||||||
_globalEventHandlers[eventClass][eventName].push({callback, thisArg, once: true});
|
|
||||||
}
|
|
||||||
|
|
||||||
public static off(eventName: string, callback?: any, thisArg?: any): void {
|
|
||||||
this.removeEventListener(eventName, callback, thisArg);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static removeEventListener(eventName: string, callback?: any, thisArg?: any): void {
|
|
||||||
if (typeof eventName !== 'string') {
|
|
||||||
throw new TypeError('Event must be string.');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (callback && typeof callback !== 'function') {
|
|
||||||
throw new TypeError('callback must be function.');
|
|
||||||
}
|
|
||||||
|
|
||||||
const eventClass = this.name;
|
|
||||||
|
|
||||||
// Short Circuit if no handlers exist..
|
|
||||||
if (!_globalEventHandlers[eventClass] || !Array.isArray(_globalEventHandlers[eventClass][eventName])) { return; }
|
|
||||||
|
|
||||||
const events = _globalEventHandlers[eventClass][eventName];
|
|
||||||
if (thisArg) {
|
|
||||||
for (let i = 0; i < events.length; i++) {
|
|
||||||
if (events[i].callback === callback && events[i].thisArg === thisArg) {
|
|
||||||
events.splice(i, 1);
|
|
||||||
i--;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if (callback) {
|
|
||||||
for (let i = 0; i < events.length; i++) {
|
|
||||||
if (events[i].callback === callback) {
|
|
||||||
events.splice(i, 1);
|
|
||||||
i--;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// Clear all events of this type
|
|
||||||
delete _globalEventHandlers[eventClass][eventName];
|
|
||||||
}
|
|
||||||
|
|
||||||
if (events.length === 0) {
|
|
||||||
// Clear all events of this type
|
|
||||||
delete _globalEventHandlers[eventClass][eventName];
|
|
||||||
}
|
|
||||||
|
|
||||||
// Clear the primary class grouping if no events are left
|
|
||||||
const keys = Object.keys(_globalEventHandlers[eventClass]);
|
|
||||||
if (keys.length === 0) {
|
|
||||||
delete _globalEventHandlers[eventClass];
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public static addEventListener(eventName: string, callback: any, thisArg?: any): void {
|
|
||||||
if (typeof eventName !== 'string') {
|
|
||||||
throw new TypeError('Event must be string.');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (typeof callback !== 'function') {
|
|
||||||
throw new TypeError('callback must be function.');
|
|
||||||
}
|
|
||||||
|
|
||||||
const eventClass = this.name;
|
|
||||||
if (!_globalEventHandlers[eventClass]) {
|
|
||||||
_globalEventHandlers[eventClass] = {};
|
|
||||||
}
|
|
||||||
if (!Array.isArray(_globalEventHandlers[eventClass][eventName])) {
|
|
||||||
_globalEventHandlers[eventClass][eventName] = [];
|
|
||||||
}
|
|
||||||
_globalEventHandlers[eventClass][eventName].push({callback, thisArg});
|
|
||||||
}
|
|
||||||
|
|
||||||
public notify<T extends EventData>(data: T): void {
|
public notify<T extends EventData>(data: T): void {
|
||||||
const eventClass = this.constructor.name;
|
const eventClass = this.constructor.name;
|
||||||
if (_globalEventHandlers[eventClass]) {
|
|
||||||
const event = data.eventName + "First";
|
|
||||||
const events = _globalEventHandlers[eventClass][event];
|
|
||||||
if (events) {
|
|
||||||
Observable._handleEvent(events, data);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const observers = <Array<ListenerEntry>>this._observers[data.eventName];
|
const observers = <Array<ListenerEntry>>this._observers[data.eventName];
|
||||||
if (observers) {
|
if (observers) {
|
||||||
Observable._handleEvent(observers, data);
|
Observable._handleEvent(observers, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_globalEventHandlers[eventClass]) {
|
|
||||||
const events = _globalEventHandlers[eventClass][data.eventName];
|
|
||||||
if (events) {
|
|
||||||
Observable._handleEvent(events, data);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static _handleEvent<T extends EventData>(observers: Array<ListenerEntry>, data: T): void {
|
private static _handleEvent<T extends EventData>(observers: Array<ListenerEntry>, data: T): void {
|
||||||
|
Reference in New Issue
Block a user