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';
// Known Limitation
@@ -6,75 +5,71 @@ import { Observable } from '../data/observable';
// to make assignable our `AbortSignal` into that.
// https://github.com/Microsoft/TSJS-lib-generator/pull/623
type Events = {
abort: any // Event & Type<"abort">
}
abort: any; // Event & Type<"abort">
};
type EventAttributes = {
onabort: any // Event & Type<"abort">
}
onabort: any; // Event & Type<"abort">
};
/**
* The signal class.
* @see https://dom.spec.whatwg.org/#abortsignal
*/
export default class AbortSignal extends Observable {
/**
* AbortSignal cannot be constructed directly.
*/
public constructor() {
super()
}
/**
* AbortSignal cannot be constructed directly.
*/
public constructor() {
super();
}
/**
* Returns `true` if this `AbortSignal`'s `AbortController` has signaled to abort, and `false` otherwise.
*/
public get aborted(): boolean {
const aborted = abortedFlags.get(this)
if (typeof aborted !== "boolean") {
throw new TypeError(
`Expected 'this' to be an 'AbortSignal' object, but got ${
this === null ? "null" : typeof this
}`,
)
}
return aborted
}
/**
* Returns `true` if this `AbortSignal`'s `AbortController` has signaled to abort, and `false` otherwise.
*/
public get aborted(): boolean {
const aborted = abortedFlags.get(this);
if (typeof aborted !== 'boolean') {
throw new TypeError(`Expected 'this' to be an 'AbortSignal' object, but got ${this === null ? 'null' : typeof this}`);
}
return aborted;
}
}
/**
* Create an AbortSignal object.
*/
export function createAbortSignal(): AbortSignal {
const signal = new AbortSignal();
abortedFlags.set(signal, false)
return signal
const signal = new AbortSignal();
abortedFlags.set(signal, false);
return signal;
}
/**
* Abort a given signal.
*/
export function abortSignal(signal: AbortSignal): void {
if (abortedFlags.get(signal) !== false) {
return
}
if (abortedFlags.get(signal) !== false) {
return;
}
abortedFlags.set(signal, true)
signal.notify({ eventName: "abort", type: "abort" })
abortedFlags.set(signal, true);
signal.notify({ eventName: 'abort', type: 'abort', object: this });
}
/**
* Aborted flag for each instances.
*/
const abortedFlags = new WeakMap<AbortSignal, boolean>()
const abortedFlags = new WeakMap<AbortSignal, boolean>();
// Properties should be enumerable.
Object.defineProperties(AbortSignal.prototype, {
aborted: { enumerable: true },
})
aborted: { enumerable: true },
});
// `toString()` should return `"[object AbortSignal]"`
if (typeof Symbol === "function" && typeof Symbol.toStringTag === "symbol") {
Object.defineProperty(AbortSignal.prototype, Symbol.toStringTag, {
configurable: true,
value: "AbortSignal",
})
}
if (typeof Symbol === 'function' && typeof Symbol.toStringTag === 'symbol') {
Object.defineProperty(AbortSignal.prototype, Symbol.toStringTag, {
configurable: true,
value: 'AbortSignal',
});
}

View File

@@ -19,7 +19,7 @@ export const accessibilityPerformEscapeEvent = 'accessibilityPerformEscape';
* @param {boolean} receivedFocus
* @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) {
return;
}

View File

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

View File

@@ -9,7 +9,7 @@ export * from './font-scale';
/**
* 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

View File

@@ -3,23 +3,39 @@ import { DOMEvent } from '../dom-events/dom-event';
import { Observable as ObservableDefinition, WrappedValue as WrappedValueDefinition } from '.';
/**
* Base event data.
*/
export interface EventData {
/**
* The name of the event.
*/
eventName: string;
object: Partial<Observable>;
/**
* The Observable instance that has raised the event.
*/
object: Observable;
}
export interface EventDataValue extends EventData {
value?: boolean;
}
export interface NotifyData extends Partial<EventData> {
eventName: string;
object?: Partial<Observable>;
}
/**
* Data for the "propertyChange" event.
*/
export interface PropertyChangeData extends EventData {
/**
* The name of the property that has changed.
*/
propertyName: string;
/**
* The new value of the property.
*/
value: any;
/**
* The previous value of the property.
*/
oldValue?: any;
}
@@ -288,18 +304,12 @@ export class Observable implements ObservableDefinition {
* @param data The data associated with the event.
* @param options Options for the event, in line with DOM Standard.
*/
public notify<T extends NotifyData>(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;
public notify<T extends EventData>(data: T, options?: CustomEventInit): void {
new DOMEvent(data.eventName, options).dispatchTo({
target: this,
data: eventData,
getGlobalEventHandlersPreHandling: () => this._getGlobalEventHandlers(eventData, 'First'),
getGlobalEventHandlersPostHandling: () => this._getGlobalEventHandlers(eventData, ''),
data,
getGlobalEventHandlersPreHandling: () => this._getGlobalEventHandlers(data, 'First'),
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.
* @param view The view form which to start the search.
*/
export function getParentWithViewController(view: Partial<View>): View;
export function updateAutoAdjustScrollInsets(controller: any /* UIViewController */, owner: Partial<View>): void;
export function updateConstraints(controller: any /* UIViewController */, owner: Partial<View>): void;
export function layoutView(controller: any /* UIViewController */, owner: Partial<View>): void;
export function getParentWithViewController(view: View): View;
export function updateAutoAdjustScrollInsets(controller: any /* UIViewController */, owner: View): void;
export function updateConstraints(controller: any /* UIViewController */, owner: View): void;
export function layoutView(controller: any /* UIViewController */, owner: View): void;
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 shrinkToSafeArea(view: Partial<View>, frame: any /* CGRect */): any; /* CGRect */
export function expandBeyondSafeArea(view: Partial<View>, frame: any /* CGRect */): any; /* CGRect */
export function shrinkToSafeArea(view: View, frame: any /* CGRect */): any; /* CGRect */
export function expandBeyondSafeArea(view: View, frame: any /* CGRect */): any; /* CGRect */
export class UILayoutViewController {
public static initWithOwner(owner: WeakRef<Partial<View>>): UILayoutViewController;
public static initWithOwner(owner: WeakRef<View>): UILayoutViewController;
}
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 {
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';
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 {
tagetRef: WeakRef<Object>;

View File

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

View File

@@ -140,7 +140,7 @@ export interface GestureEventData extends EventData {
/**
* 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/).
*/
@@ -287,7 +287,7 @@ export class GesturesObserver {
* @param callback - A function that will be executed when a gesture is received.
* @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.