fix: simplify EventData typings, drop NotifyData

This commit is contained in:
shirakaba
2022-11-23 14:35:36 +09:00
parent 3dad494136
commit b572da1e86
9 changed files with 85 additions and 79 deletions

View File

@@ -1,4 +1,3 @@
import { Observable } from '../data/observable'; import { Observable } from '../data/observable';
// Known Limitation // Known Limitation
@@ -6,11 +5,11 @@ import { Observable } from '../data/observable';
// to make assignable our `AbortSignal` into that. // to make assignable our `AbortSignal` into that.
// https://github.com/Microsoft/TSJS-lib-generator/pull/623 // https://github.com/Microsoft/TSJS-lib-generator/pull/623
type Events = { type Events = {
abort: any // Event & Type<"abort"> abort: any; // Event & Type<"abort">
} };
type EventAttributes = { type EventAttributes = {
onabort: any // Event & Type<"abort"> onabort: any; // Event & Type<"abort">
} };
/** /**
* The signal class. * The signal class.
@@ -21,22 +20,18 @@ export default class AbortSignal extends Observable {
* AbortSignal cannot be constructed directly. * AbortSignal cannot be constructed directly.
*/ */
public constructor() { public constructor() {
super() super();
} }
/** /**
* Returns `true` if this `AbortSignal`'s `AbortController` has signaled to abort, and `false` otherwise. * Returns `true` if this `AbortSignal`'s `AbortController` has signaled to abort, and `false` otherwise.
*/ */
public get aborted(): boolean { public get aborted(): boolean {
const aborted = abortedFlags.get(this) const aborted = abortedFlags.get(this);
if (typeof aborted !== "boolean") { if (typeof aborted !== 'boolean') {
throw new TypeError( throw new TypeError(`Expected 'this' to be an 'AbortSignal' object, but got ${this === null ? 'null' : typeof this}`);
`Expected 'this' to be an 'AbortSignal' object, but got ${
this === null ? "null" : typeof this
}`,
)
} }
return aborted return aborted;
} }
} }
@@ -45,8 +40,8 @@ export default class AbortSignal extends Observable {
*/ */
export function createAbortSignal(): AbortSignal { export function createAbortSignal(): AbortSignal {
const signal = new AbortSignal(); const signal = new AbortSignal();
abortedFlags.set(signal, false) abortedFlags.set(signal, false);
return signal return signal;
} }
/** /**
@@ -54,27 +49,27 @@ export function createAbortSignal(): AbortSignal {
*/ */
export function abortSignal(signal: AbortSignal): void { export function abortSignal(signal: AbortSignal): void {
if (abortedFlags.get(signal) !== false) { if (abortedFlags.get(signal) !== false) {
return return;
} }
abortedFlags.set(signal, true) abortedFlags.set(signal, true);
signal.notify({ eventName: "abort", type: "abort" }) signal.notify({ eventName: 'abort', type: 'abort', object: this });
} }
/** /**
* Aborted flag for each instances. * Aborted flag for each instances.
*/ */
const abortedFlags = new WeakMap<AbortSignal, boolean>() const abortedFlags = new WeakMap<AbortSignal, boolean>();
// Properties should be enumerable. // Properties should be enumerable.
Object.defineProperties(AbortSignal.prototype, { Object.defineProperties(AbortSignal.prototype, {
aborted: { enumerable: true }, aborted: { enumerable: true },
}) });
// `toString()` should return `"[object AbortSignal]"` // `toString()` should return `"[object AbortSignal]"`
if (typeof Symbol === "function" && typeof Symbol.toStringTag === "symbol") { if (typeof Symbol === 'function' && typeof Symbol.toStringTag === 'symbol') {
Object.defineProperty(AbortSignal.prototype, Symbol.toStringTag, { Object.defineProperty(AbortSignal.prototype, Symbol.toStringTag, {
configurable: true, configurable: true,
value: "AbortSignal", value: 'AbortSignal',
}) });
} }

View File

@@ -19,7 +19,7 @@ export const accessibilityPerformEscapeEvent = 'accessibilityPerformEscape';
* @param {boolean} receivedFocus * @param {boolean} receivedFocus
* @param {boolean} lostFocus * @param {boolean} lostFocus
*/ */
export function notifyAccessibilityFocusState(view: Partial<View>, receivedFocus: boolean, lostFocus: boolean): void { export function notifyAccessibilityFocusState(view: View, receivedFocus: boolean, lostFocus: boolean): void {
if (!receivedFocus && !lostFocus) { if (!receivedFocus && !lostFocus) {
return; return;
} }

View File

@@ -14,8 +14,8 @@ export * from './font-scale';
let clickableRolesMap = new Set<string>(); let clickableRolesMap = new Set<string>();
let lastFocusedView: WeakRef<Partial<View>>; let lastFocusedView: WeakRef<View>;
function accessibilityEventHelper(view: Partial<View>, eventType: number) { function accessibilityEventHelper(view: View, eventType: number) {
const eventName = accessibilityEventTypeMap.get(eventType); const eventName = accessibilityEventTypeMap.get(eventType);
if (!isAccessibilityServiceEnabled()) { if (!isAccessibilityServiceEnabled()) {
if (Trace.isEnabled()) { if (Trace.isEnabled()) {
@@ -105,7 +105,7 @@ function accessibilityEventHelper(view: Partial<View>, eventType: number) {
let TNSAccessibilityDelegate: android.view.View.androidviewViewAccessibilityDelegate; let TNSAccessibilityDelegate: android.view.View.androidviewViewAccessibilityDelegate;
const androidViewToTNSView = new WeakMap<android.view.View, WeakRef<Partial<View>>>(); const androidViewToTNSView = new WeakMap<android.view.View, WeakRef<View>>();
let accessibilityEventMap: Map<AndroidAccessibilityEvent, number>; let accessibilityEventMap: Map<AndroidAccessibilityEvent, number>;
let accessibilityEventTypeMap: Map<number, string>; let accessibilityEventTypeMap: Map<number, string>;
@@ -440,11 +440,11 @@ export function isAccessibilityServiceEnabled(): boolean {
return accessibilityServiceEnabled; return accessibilityServiceEnabled;
} }
export function setupAccessibleView(view: Partial<View>): void { export function setupAccessibleView(view: View): void {
updateAccessibilityProperties(view); updateAccessibilityProperties(view);
} }
export function updateAccessibilityProperties(view: Partial<View>): void { export function updateAccessibilityProperties(view: View): void {
if (!view.nativeViewProtected) { if (!view.nativeViewProtected) {
return; return;
} }
@@ -540,7 +540,7 @@ export function updateContentDescription(view: View, forceUpdate?: boolean): str
return applyContentDescription(view, forceUpdate); return applyContentDescription(view, forceUpdate);
} }
function setAccessibilityDelegate(view: Partial<View>): void { function setAccessibilityDelegate(view: View): void {
if (!view.nativeViewProtected) { if (!view.nativeViewProtected) {
return; return;
} }
@@ -566,7 +566,7 @@ function setAccessibilityDelegate(view: Partial<View>): void {
androidView.setAccessibilityDelegate(TNSAccessibilityDelegate); androidView.setAccessibilityDelegate(TNSAccessibilityDelegate);
} }
function applyContentDescription(view: Partial<View>, forceUpdate?: boolean) { function applyContentDescription(view: View, forceUpdate?: boolean) {
let androidView = view.nativeViewProtected as android.view.View; let androidView = view.nativeViewProtected as android.view.View;
if (!androidView || (androidView instanceof android.widget.TextView && !view._androidContentDescriptionUpdated)) { if (!androidView || (androidView instanceof android.widget.TextView && !view._androidContentDescriptionUpdated)) {
return null; return null;

View File

@@ -9,7 +9,7 @@ export * from './font-scale';
/** /**
* Initialize accessibility for View. This should be called on loaded-event. * Initialize accessibility for View. This should be called on loaded-event.
*/ */
export function setupAccessibleView(view: Partial<View>): void; export function setupAccessibleView(view: View): void;
/** /**
* Update accessibility properties on nativeView * Update accessibility properties on nativeView

View File

@@ -3,23 +3,39 @@ import { DOMEvent } from '../dom-events/dom-event';
import { Observable as ObservableDefinition, WrappedValue as WrappedValueDefinition } from '.'; import { Observable as ObservableDefinition, WrappedValue as WrappedValueDefinition } from '.';
/**
* Base event data.
*/
export interface EventData { export interface EventData {
/**
* The name of the event.
*/
eventName: string; eventName: string;
object: Partial<Observable>; /**
* The Observable instance that has raised the event.
*/
object: Observable;
} }
export interface EventDataValue extends EventData { export interface EventDataValue extends EventData {
value?: boolean; value?: boolean;
} }
export interface NotifyData extends Partial<EventData> { /**
eventName: string; * Data for the "propertyChange" event.
object?: Partial<Observable>; */
}
export interface PropertyChangeData extends EventData { export interface PropertyChangeData extends EventData {
/**
* The name of the property that has changed.
*/
propertyName: string; propertyName: string;
/**
* The new value of the property.
*/
value: any; value: any;
/**
* The previous value of the property.
*/
oldValue?: any; oldValue?: any;
} }
@@ -288,18 +304,12 @@ export class Observable implements ObservableDefinition {
* @param data The data associated with the event. * @param data The data associated with the event.
* @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 NotifyData>(data: T, options?: CustomEventInit): void { public notify<T extends EventData>(data: T, options?: CustomEventInit): void {
data.object = data.object || this;
// Now that we've filled in the `object` field (that was optional in
// NotifyData), `data` can be treated as EventData.
const eventData = data as EventData;
new DOMEvent(data.eventName, options).dispatchTo({ new DOMEvent(data.eventName, options).dispatchTo({
target: this, target: this,
data: eventData, data,
getGlobalEventHandlersPreHandling: () => this._getGlobalEventHandlers(eventData, 'First'), getGlobalEventHandlersPreHandling: () => this._getGlobalEventHandlers(data, 'First'),
getGlobalEventHandlersPostHandling: () => this._getGlobalEventHandlers(eventData, ''), getGlobalEventHandlersPostHandling: () => this._getGlobalEventHandlers(data, ''),
}); });
} }

View File

@@ -46,21 +46,21 @@ export namespace IOSHelper {
* Returns a view with viewController or undefined if no such found along the view's parent chain. * Returns a view with viewController or undefined if no such found along the view's parent chain.
* @param view The view form which to start the search. * @param view The view form which to start the search.
*/ */
export function getParentWithViewController(view: Partial<View>): View; export function getParentWithViewController(view: View): View;
export function updateAutoAdjustScrollInsets(controller: any /* UIViewController */, owner: Partial<View>): void; export function updateAutoAdjustScrollInsets(controller: any /* UIViewController */, owner: View): void;
export function updateConstraints(controller: any /* UIViewController */, owner: Partial<View>): void; export function updateConstraints(controller: any /* UIViewController */, owner: View): void;
export function layoutView(controller: any /* UIViewController */, owner: Partial<View>): void; export function layoutView(controller: any /* UIViewController */, owner: View): void;
export function getPositionFromFrame(frame: any /* CGRect */): { left; top; right; bottom }; export function getPositionFromFrame(frame: any /* CGRect */): { left; top; right; bottom };
export function getFrameFromPosition(position: { left; top; right; bottom }, insets?: { left; top; right; bottom }): any; /* CGRect */ export function getFrameFromPosition(position: { left; top; right; bottom }, insets?: { left; top; right; bottom }): any; /* CGRect */
export function shrinkToSafeArea(view: Partial<View>, frame: any /* CGRect */): any; /* CGRect */ export function shrinkToSafeArea(view: View, frame: any /* CGRect */): any; /* CGRect */
export function expandBeyondSafeArea(view: Partial<View>, frame: any /* CGRect */): any; /* CGRect */ export function expandBeyondSafeArea(view: View, frame: any /* CGRect */): any; /* CGRect */
export class UILayoutViewController { export class UILayoutViewController {
public static initWithOwner(owner: WeakRef<Partial<View>>): UILayoutViewController; public static initWithOwner(owner: WeakRef<View>): UILayoutViewController;
} }
export class UIAdaptivePresentationControllerDelegateImp { export class UIAdaptivePresentationControllerDelegateImp {
public static initWithOwnerAndCallback(owner: WeakRef<Partial<View>>, whenClosedCallback: Function): UIAdaptivePresentationControllerDelegateImp; public static initWithOwnerAndCallback(owner: WeakRef<View>, whenClosedCallback: Function): UIAdaptivePresentationControllerDelegateImp;
} }
export class UIPopoverPresentationControllerDelegateImp { export class UIPopoverPresentationControllerDelegateImp {
public static initWithOwnerAndCallback(owner: WeakRef<Partial<View>>, whenClosedCallback: Function): UIPopoverPresentationControllerDelegateImp; public static initWithOwnerAndCallback(owner: WeakRef<View>, whenClosedCallback: Function): UIPopoverPresentationControllerDelegateImp;
} }
} }

View File

@@ -1,7 +1,7 @@
import { Observable, EventData } from '../../../data/observable'; import { Observable, EventData } from '../../../data/observable';
const handlersForEventName = new Map<string, (eventData: EventData) => void>(); const handlersForEventName = new Map<string, (eventData: EventData) => void>();
const sourcesMap = new WeakMap<Partial<Observable>, Map<string, Array<TargetHandlerPair>>>(); const sourcesMap = new WeakMap<Observable, Map<string, Array<TargetHandlerPair>>>();
class TargetHandlerPair { class TargetHandlerPair {
tagetRef: WeakRef<Object>; tagetRef: WeakRef<Object>;

View File

@@ -533,6 +533,7 @@ export abstract class EditableTextBase extends EditableTextBaseCommon {
this.notify({ this.notify({
eventName: EditableTextBase.blurEvent, eventName: EditableTextBase.blurEvent,
object: this,
}); });
dismissSoftInput(this); dismissSoftInput(this);
} }

View File

@@ -140,7 +140,7 @@ export interface GestureEventData extends EventData {
/** /**
* Gets the view which originates the gesture. * Gets the view which originates the gesture.
*/ */
view: Partial<View>; view: View;
/** /**
* Gets the underlying native iOS specific [UIGestureRecognizer](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIGestureRecognizer_Class/). * Gets the underlying native iOS specific [UIGestureRecognizer](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIGestureRecognizer_Class/).
*/ */
@@ -287,7 +287,7 @@ export class GesturesObserver {
* @param callback - A function that will be executed when a gesture is received. * @param callback - A function that will be executed when a gesture is received.
* @param context - default this argument for the callbacks. * @param context - default this argument for the callbacks.
*/ */
constructor(target: Partial<View>, callback: (args: GestureEventData) => void, context: any); constructor(target: View, callback: (args: GestureEventData) => void, context: any);
/** /**
* Registers a gesture observer to a view and gesture. * Registers a gesture observer to a view and gesture.