fix: convert property bag to args

This commit is contained in:
shirakaba
2022-12-21 10:40:25 +09:00
parent 253d313776
commit 1f3a62c3fb
5 changed files with 44 additions and 194 deletions

View File

@@ -56,17 +56,17 @@ function accessibilityEventHelper(view: View, eventType: number) {
*/ */
if (SDK_VERSION >= 26) { if (SDK_VERSION >= 26) {
// Trigger all tap handlers on this view. // Trigger all tap handlers on this view.
new DOMEvent('tap').dispatchTo({ new DOMEvent('tap').dispatchTo(
target: view as View, view as View,
data: { {
android: view.android, android: view.android,
eventName: 'tap', eventName: 'tap',
ios: null, ios: null,
object: view, object: view,
type: GestureTypes.tap, type: GestureTypes.tap,
view, view,
} as GestureEventData, } as GestureEventData
}); );
} }
return; return;

View File

@@ -290,7 +290,9 @@ export class DOMEvent implements Event {
* event's cancelable attribute value is false or its preventDefault() * event's cancelable attribute value is false or its preventDefault()
* method was not invoked, and false otherwise. * method was not invoked, and false otherwise.
*/ */
dispatchTo({ target, data, getGlobalEventHandlersPreHandling, getGlobalEventHandlersPostHandling }: { target: Observable; data: EventData; getGlobalEventHandlersPreHandling?: () => MutationSensitiveArray<ListenerEntry>; getGlobalEventHandlersPostHandling?: () => MutationSensitiveArray<ListenerEntry> }): boolean { // Taking multiple params rather than a single property bag saves about 100
// nanoseconds per call.
dispatchTo(target: Observable, data: EventData, getGlobalEventHandlersPreHandling?: () => MutationSensitiveArray<ListenerEntry>, getGlobalEventHandlersPostHandling?: () => MutationSensitiveArray<ListenerEntry>): boolean {
if (this.eventPhase !== DOMEvent.NONE) { if (this.eventPhase !== DOMEvent.NONE) {
throw new Error('Tried to dispatch a dispatching event'); throw new Error('Tried to dispatch a dispatching event');
} }

View File

@@ -377,12 +377,12 @@ export class Observable implements EventTarget {
* @param options Options for the event, in line with DOM Standard. * @param options Options for the event, in line with DOM Standard.
*/ */
public notify<T extends EventData>(data: T, options?: CustomEventInit): void { public notify<T extends EventData>(data: T, options?: CustomEventInit): void {
new DOMEvent(data.eventName, options).dispatchTo({ new DOMEvent(data.eventName, options).dispatchTo(
target: this, this,
data, data,
getGlobalEventHandlersPreHandling: () => this._getGlobalEventHandlers(data, 'First'), () => this._getGlobalEventHandlers(data, 'First'),
getGlobalEventHandlersPostHandling: () => this._getGlobalEventHandlers(data, ''), () => this._getGlobalEventHandlers(data, '')
}); );
} }
dispatchEvent(event: DOMEvent): boolean { dispatchEvent(event: DOMEvent): boolean {
@@ -392,12 +392,12 @@ export class Observable implements EventTarget {
detail: event.detail, detail: event.detail,
}; };
return event.dispatchTo({ return event.dispatchTo(
target: this, this,
data, data,
getGlobalEventHandlersPreHandling: () => this._getGlobalEventHandlers(data, 'First'), () => this._getGlobalEventHandlers(data, 'First'),
getGlobalEventHandlersPostHandling: () => this._getGlobalEventHandlers(data, ''), () => this._getGlobalEventHandlers(data, '')
}); );
} }
private _getGlobalEventHandlers(data: EventData, eventType: 'First' | ''): MutationSensitiveArray<ListenerEntry> { private _getGlobalEventHandlers(data: EventData, eventType: 'First' | ''): MutationSensitiveArray<ListenerEntry> {

View File

@@ -63,10 +63,7 @@ function initializeTapAndDoubleTapGestureListener() {
public onLongPress(motionEvent: android.view.MotionEvent): void { public onLongPress(motionEvent: android.view.MotionEvent): void {
if (this._type & GestureTypes.longPress && this._observer?.callback) { if (this._type & GestureTypes.longPress && this._observer?.callback) {
new DOMEvent('longPress').dispatchTo({ new DOMEvent('longPress').dispatchTo(this._target, _getLongPressArgs(GestureTypes.longPress, this._target, GestureStateTypes.began, motionEvent));
target: this._target,
data: _getLongPressArgs(GestureTypes.longPress, this._target, GestureStateTypes.began, motionEvent),
});
} }
} }
@@ -74,10 +71,7 @@ function initializeTapAndDoubleTapGestureListener() {
if (this._target.getGestureObservers(GestureTypes.doubleTap).length) { if (this._target.getGestureObservers(GestureTypes.doubleTap).length) {
this._tapTimeoutId = timer.setTimeout(() => { this._tapTimeoutId = timer.setTimeout(() => {
if (this._type & GestureTypes.tap && this._observer?.callback) { if (this._type & GestureTypes.tap && this._observer?.callback) {
new DOMEvent('tap').dispatchTo({ new DOMEvent('tap').dispatchTo(this._target, _getTapArgs(GestureTypes.tap, this._target, motionEvent));
target: this._target,
data: _getTapArgs(GestureTypes.tap, this._target, motionEvent),
});
} }
timer.clearTimeout(this._tapTimeoutId); timer.clearTimeout(this._tapTimeoutId);
}, TapAndDoubleTapGestureListenerImpl.DoubleTapTimeout); }, TapAndDoubleTapGestureListenerImpl.DoubleTapTimeout);
@@ -85,10 +79,7 @@ function initializeTapAndDoubleTapGestureListener() {
} }
if (this._type & GestureTypes.tap && this._observer?.callback) { if (this._type & GestureTypes.tap && this._observer?.callback) {
new DOMEvent('tap').dispatchTo({ new DOMEvent('tap').dispatchTo(this._target, _getTapArgs(GestureTypes.tap, this._target, motionEvent));
target: this._target,
data: _getTapArgs(GestureTypes.tap, this._target, motionEvent),
});
} }
} }
@@ -97,10 +88,7 @@ function initializeTapAndDoubleTapGestureListener() {
timer.clearTimeout(this._tapTimeoutId); timer.clearTimeout(this._tapTimeoutId);
} }
if (this._type & GestureTypes.doubleTap && this._observer?.callback) { if (this._type & GestureTypes.doubleTap && this._observer?.callback) {
new DOMEvent('doubleTap').dispatchTo({ new DOMEvent('doubleTap').dispatchTo(this._target, _getTapArgs(GestureTypes.doubleTap, this._target, motionEvent));
target: this._target,
data: _getTapArgs(GestureTypes.doubleTap, this._target, motionEvent),
});
} }
} }
} }
@@ -137,10 +125,7 @@ function initializePinchGestureListener() {
this._scale = detector.getScaleFactor(); this._scale = detector.getScaleFactor();
if (this._observer?.callback) { if (this._observer?.callback) {
new DOMEvent('pinch').dispatchTo({ new DOMEvent('pinch').dispatchTo(this._target, new PinchGestureEventData(this._target, detector, this._scale, this._target, GestureStateTypes.began));
target: this._target,
data: new PinchGestureEventData(this._target, detector, this._scale, this._target, GestureStateTypes.began),
});
} }
return true; return true;
@@ -150,10 +135,7 @@ function initializePinchGestureListener() {
this._scale *= detector.getScaleFactor(); this._scale *= detector.getScaleFactor();
if (this._observer?.callback) { if (this._observer?.callback) {
new DOMEvent('pinch').dispatchTo({ new DOMEvent('pinch').dispatchTo(this._target, new PinchGestureEventData(this._target, detector, this._scale, this._target, GestureStateTypes.changed));
target: this._target,
data: new PinchGestureEventData(this._target, detector, this._scale, this._target, GestureStateTypes.changed),
});
} }
return true; return true;
@@ -163,10 +145,7 @@ function initializePinchGestureListener() {
this._scale *= detector.getScaleFactor(); this._scale *= detector.getScaleFactor();
if (this._observer?.callback) { if (this._observer?.callback) {
new DOMEvent('pinch').dispatchTo({ new DOMEvent('pinch').dispatchTo(this._target, new PinchGestureEventData(this._target, detector, this._scale, this._target, GestureStateTypes.ended));
target: this._target,
data: new PinchGestureEventData(this._target, detector, this._scale, this._target, GestureStateTypes.ended),
});
} }
} }
} }
@@ -212,19 +191,13 @@ function initializeSwipeGestureListener() {
if (Math.abs(deltaX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) { if (Math.abs(deltaX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (deltaX > 0) { if (deltaX > 0) {
if (this._observer?.callback) { if (this._observer?.callback) {
new DOMEvent('swipe').dispatchTo({ new DOMEvent('swipe').dispatchTo(this._target, _getSwipeArgs(SwipeDirection.right, this._target, initialEvent, currentEvent));
target: this._target,
data: _getSwipeArgs(SwipeDirection.right, this._target, initialEvent, currentEvent),
});
} }
result = true; result = true;
} else { } else {
if (this._observer?.callback) { if (this._observer?.callback) {
new DOMEvent('swipe').dispatchTo({ new DOMEvent('swipe').dispatchTo(this._target, _getSwipeArgs(SwipeDirection.left, this._target, initialEvent, currentEvent));
target: this._target,
data: _getSwipeArgs(SwipeDirection.left, this._target, initialEvent, currentEvent),
});
} }
result = true; result = true;
} }
@@ -233,18 +206,12 @@ function initializeSwipeGestureListener() {
if (Math.abs(deltaY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) { if (Math.abs(deltaY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
if (deltaY > 0) { if (deltaY > 0) {
if (this._observer?.callback) { if (this._observer?.callback) {
new DOMEvent('swipe').dispatchTo({ new DOMEvent('swipe').dispatchTo(this._target, _getSwipeArgs(SwipeDirection.down, this._target, initialEvent, currentEvent));
target: this._target,
data: _getSwipeArgs(SwipeDirection.down, this._target, initialEvent, currentEvent),
});
} }
result = true; result = true;
} else { } else {
if (this._observer?.callback) { if (this._observer?.callback) {
new DOMEvent('swipe').dispatchTo({ new DOMEvent('swipe').dispatchTo(this._target, _getSwipeArgs(SwipeDirection.up, this._target, initialEvent, currentEvent));
target: this._target,
data: _getSwipeArgs(SwipeDirection.up, this._target, initialEvent, currentEvent),
});
} }
result = true; result = true;
} }
@@ -372,10 +339,7 @@ export class GesturesObserver extends GesturesObserverBase {
this._eventData.prepare(this.target, motionEvent); this._eventData.prepare(this.target, motionEvent);
if (this.callback) { if (this.callback) {
new DOMEvent('touch').dispatchTo({ new DOMEvent('touch').dispatchTo(this.target, this._eventData);
target: this.target,
data: this._eventData,
});
} }
} }
@@ -504,10 +468,7 @@ class CustomPanGestureDetector {
private trackStop(currentEvent: android.view.MotionEvent, cacheEvent: boolean) { private trackStop(currentEvent: android.view.MotionEvent, cacheEvent: boolean) {
if (this.isTracking) { if (this.isTracking) {
if (this.observer?.callback) { if (this.observer?.callback) {
new DOMEvent('pan').dispatchTo({ new DOMEvent('pan').dispatchTo(this.target, _getPanArgs(this.deltaX, this.deltaY, this.target, GestureStateTypes.ended, null, currentEvent));
target: this.target,
data: _getPanArgs(this.deltaX, this.deltaY, this.target, GestureStateTypes.ended, null, currentEvent),
});
} }
this.deltaX = undefined; this.deltaX = undefined;
@@ -529,10 +490,7 @@ class CustomPanGestureDetector {
this.isTracking = true; this.isTracking = true;
if (this.observer?.callback) { if (this.observer?.callback) {
new DOMEvent('pan').dispatchTo({ new DOMEvent('pan').dispatchTo(this.target, _getPanArgs(0, 0, this.target, GestureStateTypes.began, null, currentEvent));
target: this.target,
data: _getPanArgs(0, 0, this.target, GestureStateTypes.began, null, currentEvent),
});
} }
} }
@@ -542,10 +500,7 @@ class CustomPanGestureDetector {
this.deltaY = current.y - this.initialY; this.deltaY = current.y - this.initialY;
if (this.observer?.callback) { if (this.observer?.callback) {
new DOMEvent('pan').dispatchTo({ new DOMEvent('pan').dispatchTo(this.target, _getPanArgs(this.deltaX, this.deltaY, this.target, GestureStateTypes.changed, null, currentEvent));
target: this.target,
data: _getPanArgs(this.deltaX, this.deltaY, this.target, GestureStateTypes.changed, null, currentEvent),
});
} }
} }
@@ -665,10 +620,7 @@ class CustomRotateGestureDetector {
}; };
if (this.observer?.callback) { if (this.observer?.callback) {
new DOMEvent('rotation').dispatchTo({ new DOMEvent('rotation').dispatchTo(this.target, args);
target: this.target,
data: args,
});
} }
} }

View File

@@ -134,141 +134,37 @@ export class GesturesObserver extends GesturesObserverBase {
// release, we may make them bubbling. // release, we may make them bubbling.
if (type & GestureTypes.tap) { if (type & GestureTypes.tap) {
nativeView.addGestureRecognizer( nativeView.addGestureRecognizer(this._createRecognizer(GestureTypes.tap, (args) => args.view && new DOMEvent('tap').dispatchTo(args.view as View, _getTapData(args))));
this._createRecognizer(
GestureTypes.tap,
(args) =>
args.view &&
new DOMEvent('tap').dispatchTo({
target: args.view as View,
data: _getTapData(args),
})
)
);
} }
if (type & GestureTypes.doubleTap) { if (type & GestureTypes.doubleTap) {
nativeView.addGestureRecognizer( nativeView.addGestureRecognizer(this._createRecognizer(GestureTypes.doubleTap, (args) => args.view && new DOMEvent('doubleTap').dispatchTo(args.view as View, _getTapData(args))));
this._createRecognizer(
GestureTypes.doubleTap,
(args) =>
args.view &&
new DOMEvent('doubleTap').dispatchTo({
target: args.view as View,
data: _getTapData(args),
})
)
);
} }
if (type & GestureTypes.pinch) { if (type & GestureTypes.pinch) {
nativeView.addGestureRecognizer( nativeView.addGestureRecognizer(this._createRecognizer(GestureTypes.pinch, (args) => args.view && new DOMEvent('pinch').dispatchTo(args.view as View, _getPinchData(args))));
this._createRecognizer(
GestureTypes.pinch,
(args) =>
args.view &&
new DOMEvent('pinch').dispatchTo({
target: args.view as View,
data: _getPinchData(args),
})
)
);
} }
if (type & GestureTypes.pan) { if (type & GestureTypes.pan) {
nativeView.addGestureRecognizer( nativeView.addGestureRecognizer(this._createRecognizer(GestureTypes.pan, (args) => args.view && new DOMEvent('pan').dispatchTo(args.view as View, _getPanData(args, target.nativeViewProtected))));
this._createRecognizer(
GestureTypes.pan,
(args) =>
args.view &&
new DOMEvent('pan').dispatchTo({
target: args.view as View,
data: _getPanData(args, target.nativeViewProtected),
})
)
);
} }
if (type & GestureTypes.swipe) { if (type & GestureTypes.swipe) {
nativeView.addGestureRecognizer( nativeView.addGestureRecognizer(this._createRecognizer(GestureTypes.swipe, (args) => args.view && new DOMEvent('swipe').dispatchTo(args.view as View, _getSwipeData(args)), UISwipeGestureRecognizerDirection.Down));
this._createRecognizer(
GestureTypes.swipe,
(args) =>
args.view &&
new DOMEvent('swipe').dispatchTo({
target: args.view as View,
data: _getSwipeData(args),
}),
UISwipeGestureRecognizerDirection.Down
)
);
nativeView.addGestureRecognizer( nativeView.addGestureRecognizer(this._createRecognizer(GestureTypes.swipe, (args) => args.view && new DOMEvent('swipe').dispatchTo(args.view as View, _getSwipeData(args)), UISwipeGestureRecognizerDirection.Left));
this._createRecognizer(
GestureTypes.swipe,
(args) =>
args.view &&
new DOMEvent('swipe').dispatchTo({
target: args.view as View,
data: _getSwipeData(args),
}),
UISwipeGestureRecognizerDirection.Left
)
);
nativeView.addGestureRecognizer( nativeView.addGestureRecognizer(this._createRecognizer(GestureTypes.swipe, (args) => args.view && new DOMEvent('swipe').dispatchTo(args.view as View, _getSwipeData(args)), UISwipeGestureRecognizerDirection.Right));
this._createRecognizer(
GestureTypes.swipe,
(args) =>
args.view &&
new DOMEvent('swipe').dispatchTo({
target: args.view as View,
data: _getSwipeData(args),
}),
UISwipeGestureRecognizerDirection.Right
)
);
nativeView.addGestureRecognizer( nativeView.addGestureRecognizer(this._createRecognizer(GestureTypes.swipe, (args) => args.view && new DOMEvent('swipe').dispatchTo(args.view as View, _getSwipeData(args)), UISwipeGestureRecognizerDirection.Up));
this._createRecognizer(
GestureTypes.swipe,
(args) =>
args.view &&
new DOMEvent('swipe').dispatchTo({
target: args.view as View,
data: _getSwipeData(args),
}),
UISwipeGestureRecognizerDirection.Up
)
);
} }
if (type & GestureTypes.rotation) { if (type & GestureTypes.rotation) {
nativeView.addGestureRecognizer( nativeView.addGestureRecognizer(this._createRecognizer(GestureTypes.rotation, (args) => args.view && new DOMEvent('rotation').dispatchTo(args.view as View, _getRotationData(args))));
this._createRecognizer(
GestureTypes.rotation,
(args) =>
args.view &&
new DOMEvent('rotation').dispatchTo({
target: args.view as View,
data: _getRotationData(args),
})
)
);
} }
if (type & GestureTypes.longPress) { if (type & GestureTypes.longPress) {
nativeView.addGestureRecognizer( nativeView.addGestureRecognizer(this._createRecognizer(GestureTypes.longPress, (args) => args.view && new DOMEvent('longPress').dispatchTo(args.view as View, _getLongPressData(args))));
this._createRecognizer(
GestureTypes.longPress,
(args) =>
args.view &&
new DOMEvent('longPress').dispatchTo({
target: args.view as View,
data: _getLongPressData(args),
})
)
);
} }
if (type & GestureTypes.touch) { if (type & GestureTypes.touch) {