feat: implement Event and EventTarget

This commit is contained in:
shirakaba
2022-11-29 19:23:05 +09:00
parent 933af70d39
commit 0fe149c9e1
5 changed files with 59 additions and 36 deletions

View File

@@ -292,7 +292,11 @@ export abstract class ViewCommon extends ViewBase implements ViewDefinition {
return this._gestureObservers[type] || [];
}
public addEventListener(arg: string | GestureTypes, callback: (data: EventData) => void, thisArg?: any, options?: AddEventListenerOptions | boolean): void {
public addEventListener(arg: string | GestureTypes, callback: EventListenerOrEventListenerObject | ((data: EventData) => void), thisArg?: any, options?: AddEventListenerOptions | boolean): void {
if (typeof callback !== 'function') {
throw new TypeError('Callback must be function.');
}
// To avoid a full refactor of the Gestures system when migrating to DOM
// Events, we mirror the this._gestureObservers record, creating
// corresponding DOM Event listeners for each gesture.
@@ -308,7 +312,7 @@ export abstract class ViewCommon extends ViewBase implements ViewDefinition {
// the same time).
if (typeof arg === 'number') {
this._observe(arg, callback, thisArg, options);
this._observe(arg, callback as (data: EventData) => void, thisArg, options);
super.addEventListener(gestureToString(arg), callback, thisArg, options);
return;
}
@@ -320,15 +324,19 @@ export abstract class ViewCommon extends ViewBase implements ViewDefinition {
for (const event of events) {
const gesture = gestureFromString(event);
if (gesture && !this._isEvent(arg)) {
this._observe(gesture, callback, thisArg, options);
this._observe(gesture, callback as (data: EventData) => void, thisArg, options);
}
super.addEventListener(event, callback, thisArg, options);
}
}
public removeEventListener(arg: string | GestureTypes, callback?: (data: EventData) => void, thisArg?: any, options?: EventListenerOptions | boolean): void {
public removeEventListener(arg: string | GestureTypes, callback?: EventListenerOrEventListenerObject | ((data: EventData) => void), thisArg?: any, options?: EventListenerOptions | boolean): void {
if (callback && typeof callback !== 'function') {
throw new TypeError('Callback, if provided, must be function.');
}
if (typeof arg === 'number') {
this._disconnectGestureObservers(arg, callback, thisArg, options);
this._disconnectGestureObservers(arg, callback as (data: EventData) => void, thisArg, options);
super.removeEventListener(gestureToString(arg), callback, thisArg, options);
return;
}
@@ -338,7 +346,7 @@ export abstract class ViewCommon extends ViewBase implements ViewDefinition {
for (const event of events) {
const gesture = gestureFromString(event);
if (gesture && !this._isEvent(arg)) {
this._disconnectGestureObservers(gesture, callback, thisArg, options);
this._disconnectGestureObservers(gesture, callback as (data: EventData) => void, thisArg, options);
}
super.removeEventListener(event, callback, thisArg, options);
}

View File

@@ -16,7 +16,7 @@ export abstract class ScrollViewBase extends ContentView implements ScrollViewDe
public scrollBarIndicatorVisible: boolean;
public isScrollEnabled: boolean;
public addEventListener(arg: string, callback: (data: EventData) => void, thisArg?: any, options?: AddEventListenerOptions | boolean): void {
public addEventListener(arg: string, callback: EventListenerOrEventListenerObject | ((data: EventData) => void), thisArg?: any, options?: AddEventListenerOptions | boolean): void {
super.addEventListener(arg, callback, thisArg, options);
if (arg === ScrollViewBase.scrollEvent) {
@@ -25,7 +25,7 @@ export abstract class ScrollViewBase extends ContentView implements ScrollViewDe
}
}
public removeEventListener(arg: string, callback?: (data: EventData) => void, thisArg?: any, options?: EventListenerOptions | boolean): void {
public removeEventListener(arg: string, callback?: EventListenerOrEventListenerObject | ((data: EventData) => void), thisArg?: any, options?: EventListenerOptions | boolean): void {
super.removeEventListener(arg, callback, thisArg, options);
if (arg === ScrollViewBase.scrollEvent) {

View File

@@ -81,12 +81,12 @@ export class Span extends ViewBase implements SpanDefinition {
return this._tappable;
}
addEventListener(arg: string, callback: (data: EventData) => void, thisArg?: any, options?: AddEventListenerOptions | boolean): void {
addEventListener(arg: string, callback: EventListenerOrEventListenerObject | ((data: EventData) => void), thisArg?: any, options?: AddEventListenerOptions | boolean): void {
super.addEventListener(arg, callback, thisArg, options);
this._setTappable(this.hasListeners(Span.linkTapEvent));
}
removeEventListener(arg: string, callback?: (data: EventData) => void, thisArg?: any, options?: EventListenerOptions | boolean): void {
removeEventListener(arg: string, callback?: EventListenerOrEventListenerObject | ((data: EventData) => void), thisArg?: any, options?: EventListenerOptions | boolean): void {
super.removeEventListener(arg, callback, thisArg, options);
this._setTappable(this.hasListeners(Span.linkTapEvent));
}