fix: clean up Application types

This commit is contained in:
shirakaba
2022-11-23 15:57:47 +09:00
parent 890be6c888
commit 933af70d39
6 changed files with 47 additions and 31 deletions

View File

@@ -1,3 +1,4 @@
import type { ApplicationEventData } from '../application';
import * as Application from '../application'; import * as Application from '../application';
import { FontScaleCategory, getClosestValidFontScale } from './font-scale-common'; import { FontScaleCategory, getClosestValidFontScale } from './font-scale-common';
export * from './font-scale-common'; export * from './font-scale-common';
@@ -12,7 +13,7 @@ function fontScaleChanged(origFontScale: number) {
eventName: Application.fontScaleChangedEvent, eventName: Application.fontScaleChangedEvent,
object: Application, object: Application,
newValue: currentFontScale, newValue: currentFontScale,
}); } as ApplicationEventData);
} }
} }

View File

@@ -4,7 +4,6 @@ import '../globals';
// Types // Types
import { AndroidApplication, iOSApplication } from '.'; import { AndroidApplication, iOSApplication } from '.';
import { CssChangedEventData, DiscardedErrorEventData, LoadAppCSSEventData, UnhandledErrorEventData } from './application-interfaces'; import { CssChangedEventData, DiscardedErrorEventData, LoadAppCSSEventData, UnhandledErrorEventData } from './application-interfaces';
import type { EventData, Observable } from '../data/observable';
import { View } from '../ui/core/view'; import { View } from '../ui/core/view';
// Requires // Requires
@@ -50,10 +49,10 @@ export function setResources(res: any) {
export const android: AndroidApplication = undefined; export const android: AndroidApplication = undefined;
export const ios: iOSApplication = undefined; export const ios: iOSApplication = undefined;
export const on = global.NativeScriptGlobals.events.on.bind(global.NativeScriptGlobals.events) as Observable['on']; export const on = global.NativeScriptGlobals.events.on.bind(global.NativeScriptGlobals.events) as typeof import('.')['on'];
export const off = global.NativeScriptGlobals.events.off.bind(global.NativeScriptGlobals.events) as Observable['off']; export const off = global.NativeScriptGlobals.events.off.bind(global.NativeScriptGlobals.events) as typeof import('.')['off'];
export const notify = global.NativeScriptGlobals.events.notify.bind(global.NativeScriptGlobals.events) as Observable['notify']; export const notify = global.NativeScriptGlobals.events.notify.bind(global.NativeScriptGlobals.events) as typeof import('.')['notify'];
export const hasListeners = global.NativeScriptGlobals.events.hasListeners.bind(global.NativeScriptGlobals.events) as Observable['hasListeners']; export const hasListeners = global.NativeScriptGlobals.events.hasListeners.bind(global.NativeScriptGlobals.events) as typeof import('.')['hasListeners'];
let app: iOSApplication | AndroidApplication; let app: iOSApplication | AndroidApplication;
export function setApplication(instance: iOSApplication | AndroidApplication): void { export function setApplication(instance: iOSApplication | AndroidApplication): void {
@@ -63,7 +62,7 @@ export function setApplication(instance: iOSApplication | AndroidApplication): v
} }
export function livesync(rootView: View, context?: ModuleContext) { export function livesync(rootView: View, context?: ModuleContext) {
global.NativeScriptGlobals.events.notify(<EventData>{ eventName: 'livesync', object: app }); notify({ eventName: 'livesync', object: app });
const liveSyncCore = global.__onLiveSyncCore; const liveSyncCore = global.__onLiveSyncCore;
let reapplyAppStyles = false; let reapplyAppStyles = false;
@@ -85,7 +84,7 @@ export function livesync(rootView: View, context?: ModuleContext) {
export function setCssFileName(cssFileName: string) { export function setCssFileName(cssFileName: string) {
cssFile = cssFileName; cssFile = cssFileName;
global.NativeScriptGlobals.events.notify(<CssChangedEventData>{ notify(<CssChangedEventData>{
eventName: 'cssChanged', eventName: 'cssChanged',
object: app, object: app,
cssFile: cssFileName, cssFile: cssFileName,
@@ -98,7 +97,7 @@ export function getCssFileName(): string {
export function loadAppCss(): void { export function loadAppCss(): void {
try { try {
global.NativeScriptGlobals.events.notify(<LoadAppCSSEventData>{ notify(<LoadAppCSSEventData>{
eventName: 'loadAppCss', eventName: 'loadAppCss',
object: app, object: app,
cssFile: getCssFileName(), cssFile: getCssFileName(),
@@ -181,7 +180,7 @@ export function setSuspended(value: boolean): void {
} }
global.__onUncaughtError = function (error: NativeScriptError) { global.__onUncaughtError = function (error: NativeScriptError) {
global.NativeScriptGlobals.events.notify(<UnhandledErrorEventData>{ notify(<UnhandledErrorEventData>{
eventName: uncaughtErrorEvent, eventName: uncaughtErrorEvent,
object: app, object: app,
android: error, android: error,
@@ -191,7 +190,7 @@ global.__onUncaughtError = function (error: NativeScriptError) {
}; };
global.__onDiscardedError = function (error: NativeScriptError) { global.__onDiscardedError = function (error: NativeScriptError) {
global.NativeScriptGlobals.events.notify(<DiscardedErrorEventData>{ notify(<DiscardedErrorEventData>{
eventName: discardedErrorEvent, eventName: discardedErrorEvent,
object: app, object: app,
error: error, error: error,

View File

@@ -13,22 +13,45 @@ export interface NativeScriptError extends Error {
} }
export interface ApplicationEventData extends EventData { export interface ApplicationEventData extends EventData {
/**
* UIApplication or undefined, unless otherwise specified. Prefer explicit
* properties where possible.
*/
ios?: any; ios?: any;
/**
* androidx.appcompat.app.AppCompatActivity or undefined, unless otherwise
* specified. Prefer explicit properties where possible.
*/
android?: any; android?: any;
eventName: string; /**
* Careful with this messy type. A significant refactor is needed to make it
* strictly extend EventData['object'], which is an Observable. It's used in
* various ways:
* - By font-scale: the Application module, typeof import('.')
* - Within index.android.ts: AndroidApplication
* - Within index.ios.ts: iOSApplication
*/
object: any; object: any;
} }
export interface LaunchEventData extends ApplicationEventData { export interface LaunchEventData extends ApplicationEventData {
/**
* The value stored into didFinishLaunchingWithOptions notification's
* userInfo under 'UIApplicationLaunchOptionsLocalNotificationKey';
* otherwise, null.
*/
ios: unknown;
root?: View | null; root?: View | null;
savedInstanceState?: any /* android.os.Bundle */; savedInstanceState?: any /* android.os.Bundle */;
} }
export interface OrientationChangedEventData extends ApplicationEventData { export interface OrientationChangedEventData extends ApplicationEventData {
android: any /* globalAndroid.app.Application */;
newValue: 'portrait' | 'landscape' | 'unknown'; newValue: 'portrait' | 'landscape' | 'unknown';
} }
export interface SystemAppearanceChangedEventData extends ApplicationEventData { export interface SystemAppearanceChangedEventData extends ApplicationEventData {
android: any /* globalAndroid.app.Application */;
newValue: 'light' | 'dark'; newValue: 'light' | 'dark';
} }
@@ -42,15 +65,14 @@ export interface DiscardedErrorEventData extends ApplicationEventData {
error: NativeScriptError; error: NativeScriptError;
} }
export interface CssChangedEventData extends EventData { export interface CssChangedEventData extends ApplicationEventData {
cssFile?: string; cssFile?: string;
cssText?: string; cssText?: string;
} }
export interface AndroidActivityEventData { export interface AndroidActivityEventData extends ApplicationEventData {
activity: any /* androidx.appcompat.app.AppCompatActivity */; activity: any /* androidx.appcompat.app.AppCompatActivity */;
eventName: string; object: any /* AndroidApplication */;
object: any;
} }
export interface AndroidActivityBundleEventData extends AndroidActivityEventData { export interface AndroidActivityBundleEventData extends AndroidActivityEventData {
@@ -84,6 +106,6 @@ export interface RootViewControllerImpl {
contentController: any; contentController: any;
} }
export interface LoadAppCSSEventData extends EventData { export interface LoadAppCSSEventData extends ApplicationEventData {
cssFile: string; cssFile: string;
} }

View File

@@ -4,7 +4,7 @@ import { AndroidActivityBackPressedEventData, AndroidActivityBundleEventData, An
// TODO: explain why we need to this or remov it // TODO: explain why we need to this or remov it
// Use requires to ensure order of imports is maintained // Use requires to ensure order of imports is maintained
const appCommon = require('./application-common'); const appCommon = require('./application-common') as typeof import('./application-common');
// First reexport so that app module is initialized. // First reexport so that app module is initialized.
export * from './application-common'; export * from './application-common';

View File

@@ -257,7 +257,7 @@ export function _resetRootView(entry?: NavigationEntry | string);
/** /**
* Removes listener for the specified event name. * Removes listener for the specified event name.
*/ */
export function off(eventNames: string, callback?: (eventData: EventData) => void, thisArg?: any, options?: EventListenerOptions | boolean): void; export function off(eventNames: string, callback?: (eventData: ApplicationEventData) => void, thisArg?: any, options?: EventListenerOptions | boolean): void;
/** /**
* Shortcut alias to the removeEventListener method. * Shortcut alias to the removeEventListener method.
@@ -266,13 +266,13 @@ export function off(eventNames: string, callback?: (eventData: EventData) => voi
* @param thisArg - An optional parameter which will be used as `this` context for callback execution. * @param thisArg - An optional parameter which will be used as `this` context for callback execution.
* @param options An optional parameter. If passed as a boolean, configures the useCapture value. Otherwise, specifies options. * @param options An optional parameter. If passed as a boolean, configures the useCapture value. Otherwise, specifies options.
*/ */
export function off(eventNames: string, callback?: (eventData: EventData) => void, thisArg?: any, options?: EventListenerOptions | boolean): void; export function off(eventNames: string, callback?: (eventData: ApplicationEventData) => void, thisArg?: any, options?: EventListenerOptions | boolean): void;
/** /**
* Notifies all the registered listeners for the event provided in the data.eventName. * Notifies all the registered listeners for the event provided in the data.eventName.
* @param data The data associated with the event. * @param data The data associated with the event.
*/ */
export function notify(data: any): void; export function notify<T extends ApplicationEventData>(data: T, options?: CustomEventInit): void;
/** /**
* Checks whether a listener is registered for the specified event name. * Checks whether a listener is registered for the specified event name.
@@ -297,18 +297,13 @@ export function on(event: 'cssChanged', callback: (args: CssChangedEventData) =>
/** /**
* Event raised then livesync operation is performed. * Event raised then livesync operation is performed.
*/ */
export function on(event: 'livesync', callback: (args: EventData) => void, thisArg?: any, options?: AddEventListenerOptions | boolean): void; export function on(event: 'livesync', callback: (args: ApplicationEventData) => void, thisArg?: any, options?: AddEventListenerOptions | boolean): void;
/** /**
* This event is raised when application css is changed. * This event is raised when application css is changed.
*/ */
export function on(event: 'cssChanged', callback: (args: CssChangedEventData) => void, thisArg?: any, options?: AddEventListenerOptions | boolean): void; export function on(event: 'cssChanged', callback: (args: CssChangedEventData) => void, thisArg?: any, options?: AddEventListenerOptions | boolean): void;
/**
* Event raised then livesync operation is performed.
*/
export function on(event: 'livesync', callback: (args: EventData) => void, thisArg?: any, options?: AddEventListenerOptions | boolean): void;
/** /**
* This event is raised on application launchEvent. * This event is raised on application launchEvent.
*/ */
@@ -319,7 +314,7 @@ export function on(event: 'launch', callback: (args: LaunchEventData) => void, t
* Its intent is to be suitable for measuring app startup times. * Its intent is to be suitable for measuring app startup times.
* @experimental * @experimental
*/ */
export function on(event: 'displayed', callback: (args: EventData) => void, thisArg?: any, options?: AddEventListenerOptions | boolean): void; export function on(event: 'displayed', callback: (args: ApplicationEventData) => void, thisArg?: any, options?: AddEventListenerOptions | boolean): void;
/** /**
* This event is raised when the Application is suspended. * This event is raised when the Application is suspended.

View File

@@ -4,7 +4,7 @@ import { ApplicationEventData, CssChangedEventData, LaunchEventData, LoadAppCSSE
// TODO: explain why we need to this or remov it // TODO: explain why we need to this or remov it
// Use requires to ensure order of imports is maintained // Use requires to ensure order of imports is maintained
const { backgroundEvent, displayedEvent, exitEvent, foregroundEvent, getCssFileName, launchEvent, livesync, lowMemoryEvent, notify, on, orientationChanged, orientationChangedEvent, resumeEvent, setApplication, suspendEvent, systemAppearanceChanged, systemAppearanceChangedEvent } = require('./application-common'); const { backgroundEvent, displayedEvent, exitEvent, foregroundEvent, getCssFileName, launchEvent, livesync, lowMemoryEvent, notify, on, orientationChanged, orientationChangedEvent, resumeEvent, setApplication, suspendEvent, systemAppearanceChanged, systemAppearanceChangedEvent } = require('./application-common') as typeof import('./application-common');
// First reexport so that app module is initialized. // First reexport so that app module is initialized.
export * from './application-common'; export * from './application-common';
@@ -12,7 +12,6 @@ import { View } from '../ui/core/view';
import { NavigationEntry } from '../ui/frame/frame-interfaces'; import { NavigationEntry } from '../ui/frame/frame-interfaces';
// TODO: Remove this and get it from global to decouple builder for angular // TODO: Remove this and get it from global to decouple builder for angular
import { Builder } from '../ui/builder'; import { Builder } from '../ui/builder';
import { Observable } from '../data/observable';
import { CSSUtils } from '../css/system-classes'; import { CSSUtils } from '../css/system-classes';
import { IOSHelper } from '../ui/core/view/view-helper'; import { IOSHelper } from '../ui/core/view/view-helper';
import { Device } from '../platform'; import { Device } from '../platform';
@@ -238,7 +237,7 @@ export class iOSApplication implements iOSApplicationDefinition {
const args: LaunchEventData = { const args: LaunchEventData = {
eventName: launchEvent, eventName: launchEvent,
object: this, object: this,
ios: (notification && notification.userInfo && notification.userInfo.objectForKey('UIApplicationLaunchOptionsLocalNotificationKey')) || null, ios: notification?.userInfo?.objectForKey('UIApplicationLaunchOptionsLocalNotificationKey') || null,
}; };
notify(args); notify(args);