Skip to content

Commit

Permalink
fix: clean up Application types
Browse files Browse the repository at this point in the history
  • Loading branch information
shirakaba committed Nov 23, 2022
1 parent 8057ac2 commit be58333
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 31 deletions.
3 changes: 2 additions & 1 deletion packages/core/accessibility/font-scale.android.ts
@@ -1,3 +1,4 @@
import type { ApplicationEventData } from '../application';
import * as Application from '../application';
import { FontScaleCategory, getClosestValidFontScale } from './font-scale-common';
export * from './font-scale-common';
Expand All @@ -12,7 +13,7 @@ function fontScaleChanged(origFontScale: number) {
eventName: Application.fontScaleChangedEvent,
object: Application,
newValue: currentFontScale,
});
} as ApplicationEventData);
}
}

Expand Down
19 changes: 9 additions & 10 deletions packages/core/application/application-common.ts
Expand Up @@ -4,7 +4,6 @@ import '../globals';
// Types
import { AndroidApplication, iOSApplication } from '.';
import { CssChangedEventData, DiscardedErrorEventData, LoadAppCSSEventData, UnhandledErrorEventData } from './application-interfaces';
import type { EventData, Observable } from '../data/observable';
import { View } from '../ui/core/view';

// Requires
Expand Down Expand Up @@ -50,10 +49,10 @@ export function setResources(res: any) {
export const android: AndroidApplication = undefined;
export const ios: iOSApplication = undefined;

export const on = global.NativeScriptGlobals.events.on.bind(global.NativeScriptGlobals.events) as Observable['on'];
export const off = global.NativeScriptGlobals.events.off.bind(global.NativeScriptGlobals.events) as Observable['off'];
export const notify = global.NativeScriptGlobals.events.notify.bind(global.NativeScriptGlobals.events) as Observable['notify'];
export const hasListeners = global.NativeScriptGlobals.events.hasListeners.bind(global.NativeScriptGlobals.events) as Observable['hasListeners'];
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 typeof import('.')['off'];
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 typeof import('.')['hasListeners'];

let app: iOSApplication | AndroidApplication;
export function setApplication(instance: iOSApplication | AndroidApplication): void {
Expand All @@ -63,7 +62,7 @@ export function setApplication(instance: iOSApplication | AndroidApplication): v
}

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;
let reapplyAppStyles = false;

Expand All @@ -85,7 +84,7 @@ export function livesync(rootView: View, context?: ModuleContext) {

export function setCssFileName(cssFileName: string) {
cssFile = cssFileName;
global.NativeScriptGlobals.events.notify(<CssChangedEventData>{
notify(<CssChangedEventData>{
eventName: 'cssChanged',
object: app,
cssFile: cssFileName,
Expand All @@ -98,7 +97,7 @@ export function getCssFileName(): string {

export function loadAppCss(): void {
try {
global.NativeScriptGlobals.events.notify(<LoadAppCSSEventData>{
notify(<LoadAppCSSEventData>{
eventName: 'loadAppCss',
object: app,
cssFile: getCssFileName(),
Expand Down Expand Up @@ -181,7 +180,7 @@ export function setSuspended(value: boolean): void {
}

global.__onUncaughtError = function (error: NativeScriptError) {
global.NativeScriptGlobals.events.notify(<UnhandledErrorEventData>{
notify(<UnhandledErrorEventData>{
eventName: uncaughtErrorEvent,
object: app,
android: error,
Expand All @@ -191,7 +190,7 @@ global.__onUncaughtError = function (error: NativeScriptError) {
};

global.__onDiscardedError = function (error: NativeScriptError) {
global.NativeScriptGlobals.events.notify(<DiscardedErrorEventData>{
notify(<DiscardedErrorEventData>{
eventName: discardedErrorEvent,
object: app,
error: error,
Expand Down
34 changes: 28 additions & 6 deletions packages/core/application/application-interfaces.ts
Expand Up @@ -13,22 +13,45 @@ export interface NativeScriptError extends Error {
}

export interface ApplicationEventData extends EventData {
/**
* UIApplication or undefined, unless otherwise specified. Prefer explicit
* properties where possible.
*/
ios?: any;
/**
* androidx.appcompat.app.AppCompatActivity or undefined, unless otherwise
* specified. Prefer explicit properties where possible.
*/
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;
}

export interface LaunchEventData extends ApplicationEventData {
/**
* The value stored into didFinishLaunchingWithOptions notification's
* userInfo under 'UIApplicationLaunchOptionsLocalNotificationKey';
* otherwise, null.
*/
ios: unknown;
root?: View | null;
savedInstanceState?: any /* android.os.Bundle */;
}

export interface OrientationChangedEventData extends ApplicationEventData {
android: any /* globalAndroid.app.Application */;
newValue: 'portrait' | 'landscape' | 'unknown';
}

export interface SystemAppearanceChangedEventData extends ApplicationEventData {
android: any /* globalAndroid.app.Application */;
newValue: 'light' | 'dark';
}

Expand All @@ -42,15 +65,14 @@ export interface DiscardedErrorEventData extends ApplicationEventData {
error: NativeScriptError;
}

export interface CssChangedEventData extends EventData {
export interface CssChangedEventData extends ApplicationEventData {
cssFile?: string;
cssText?: string;
}

export interface AndroidActivityEventData {
export interface AndroidActivityEventData extends ApplicationEventData {
activity: any /* androidx.appcompat.app.AppCompatActivity */;
eventName: string;
object: any;
object: any /* AndroidApplication */;
}

export interface AndroidActivityBundleEventData extends AndroidActivityEventData {
Expand Down Expand Up @@ -84,6 +106,6 @@ export interface RootViewControllerImpl {
contentController: any;
}

export interface LoadAppCSSEventData extends EventData {
export interface LoadAppCSSEventData extends ApplicationEventData {
cssFile: string;
}
2 changes: 1 addition & 1 deletion packages/core/application/index.android.ts
Expand Up @@ -4,7 +4,7 @@ import { AndroidActivityBackPressedEventData, AndroidActivityBundleEventData, An

// TODO: explain why we need to this or remov it
// 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.
export * from './application-common';
Expand Down
15 changes: 5 additions & 10 deletions packages/core/application/index.d.ts
Expand Up @@ -257,7 +257,7 @@ export function _resetRootView(entry?: NavigationEntry | string);
/**
* 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.
Expand All @@ -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 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.
* @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.
Expand All @@ -297,18 +297,13 @@ export function on(event: 'cssChanged', callback: (args: CssChangedEventData) =>
/**
* 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.
*/
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.
*/
Expand All @@ -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.
* @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.
Expand Down
5 changes: 2 additions & 3 deletions packages/core/application/index.ios.ts
Expand Up @@ -4,15 +4,14 @@ import { ApplicationEventData, CssChangedEventData, LaunchEventData, LoadAppCSSE

// TODO: explain why we need to this or remov it
// 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.
export * from './application-common';

import { View } from '../ui/core/view';
import { NavigationEntry } from '../ui/frame/frame-interfaces';
// TODO: Remove this and get it from global to decouple builder for angular
import { Builder } from '../ui/builder';
import { Observable } from '../data/observable';
import { CSSUtils } from '../css/system-classes';
import { IOSHelper } from '../ui/core/view/view-helper';
import { Device } from '../platform';
Expand Down Expand Up @@ -238,7 +237,7 @@ export class iOSApplication implements iOSApplicationDefinition {
const args: LaunchEventData = {
eventName: launchEvent,
object: this,
ios: (notification && notification.userInfo && notification.userInfo.objectForKey('UIApplicationLaunchOptionsLocalNotificationKey')) || null,
ios: notification?.userInfo?.objectForKey('UIApplicationLaunchOptionsLocalNotificationKey') || null,
};

notify(args);
Expand Down

0 comments on commit be58333

Please sign in to comment.