Skip to content

Commit de169c2

Browse files
committed
fix: clean up Application types
1 parent 8da843d commit de169c2

File tree

6 files changed

+47
-31
lines changed

6 files changed

+47
-31
lines changed

packages/core/accessibility/font-scale.android.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { ApplicationEventData } from '../application';
12
import * as Application from '../application';
23
import { FontScaleCategory, getClosestValidFontScale } from './font-scale-common';
34
export * from './font-scale-common';
@@ -12,7 +13,7 @@ function fontScaleChanged(origFontScale: number) {
1213
eventName: Application.fontScaleChangedEvent,
1314
object: Application,
1415
newValue: currentFontScale,
15-
});
16+
} as ApplicationEventData);
1617
}
1718
}
1819

packages/core/application/application-common.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import '../globals';
44
// Types
55
import { AndroidApplication, iOSApplication } from '.';
66
import { CssChangedEventData, DiscardedErrorEventData, LoadAppCSSEventData, UnhandledErrorEventData } from './application-interfaces';
7-
import type { EventData, Observable } from '../data/observable';
87
import { View } from '../ui/core/view';
98

109
// Requires
@@ -50,10 +49,10 @@ export function setResources(res: any) {
5049
export const android: AndroidApplication = undefined;
5150
export const ios: iOSApplication = undefined;
5251

53-
export const on = global.NativeScriptGlobals.events.on.bind(global.NativeScriptGlobals.events) as Observable['on'];
54-
export const off = global.NativeScriptGlobals.events.off.bind(global.NativeScriptGlobals.events) as Observable['off'];
55-
export const notify = global.NativeScriptGlobals.events.notify.bind(global.NativeScriptGlobals.events) as Observable['notify'];
56-
export const hasListeners = global.NativeScriptGlobals.events.hasListeners.bind(global.NativeScriptGlobals.events) as Observable['hasListeners'];
52+
export const on = global.NativeScriptGlobals.events.on.bind(global.NativeScriptGlobals.events) as typeof import('.')['on'];
53+
export const off = global.NativeScriptGlobals.events.off.bind(global.NativeScriptGlobals.events) as typeof import('.')['off'];
54+
export const notify = global.NativeScriptGlobals.events.notify.bind(global.NativeScriptGlobals.events) as typeof import('.')['notify'];
55+
export const hasListeners = global.NativeScriptGlobals.events.hasListeners.bind(global.NativeScriptGlobals.events) as typeof import('.')['hasListeners'];
5756

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

6564
export function livesync(rootView: View, context?: ModuleContext) {
66-
global.NativeScriptGlobals.events.notify(<EventData>{ eventName: 'livesync', object: app });
65+
notify({ eventName: 'livesync', object: app });
6766
const liveSyncCore = global.__onLiveSyncCore;
6867
let reapplyAppStyles = false;
6968

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

8685
export function setCssFileName(cssFileName: string) {
8786
cssFile = cssFileName;
88-
global.NativeScriptGlobals.events.notify(<CssChangedEventData>{
87+
notify(<CssChangedEventData>{
8988
eventName: 'cssChanged',
9089
object: app,
9190
cssFile: cssFileName,
@@ -98,7 +97,7 @@ export function getCssFileName(): string {
9897

9998
export function loadAppCss(): void {
10099
try {
101-
global.NativeScriptGlobals.events.notify(<LoadAppCSSEventData>{
100+
notify(<LoadAppCSSEventData>{
102101
eventName: 'loadAppCss',
103102
object: app,
104103
cssFile: getCssFileName(),
@@ -181,7 +180,7 @@ export function setSuspended(value: boolean): void {
181180
}
182181

183182
global.__onUncaughtError = function (error: NativeScriptError) {
184-
global.NativeScriptGlobals.events.notify(<UnhandledErrorEventData>{
183+
notify(<UnhandledErrorEventData>{
185184
eventName: uncaughtErrorEvent,
186185
object: app,
187186
android: error,
@@ -191,7 +190,7 @@ global.__onUncaughtError = function (error: NativeScriptError) {
191190
};
192191

193192
global.__onDiscardedError = function (error: NativeScriptError) {
194-
global.NativeScriptGlobals.events.notify(<DiscardedErrorEventData>{
193+
notify(<DiscardedErrorEventData>{
195194
eventName: discardedErrorEvent,
196195
object: app,
197196
error: error,

packages/core/application/application-interfaces.ts

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,45 @@ export interface NativeScriptError extends Error {
1313
}
1414

1515
export interface ApplicationEventData extends EventData {
16+
/**
17+
* UIApplication or undefined, unless otherwise specified. Prefer explicit
18+
* properties where possible.
19+
*/
1620
ios?: any;
21+
/**
22+
* androidx.appcompat.app.AppCompatActivity or undefined, unless otherwise
23+
* specified. Prefer explicit properties where possible.
24+
*/
1725
android?: any;
18-
eventName: string;
26+
/**
27+
* Careful with this messy type. A significant refactor is needed to make it
28+
* strictly extend EventData['object'], which is an Observable. It's used in
29+
* various ways:
30+
* - By font-scale: the Application module, typeof import('.')
31+
* - Within index.android.ts: AndroidApplication
32+
* - Within index.ios.ts: iOSApplication
33+
*/
1934
object: any;
2035
}
2136

2237
export interface LaunchEventData extends ApplicationEventData {
38+
/**
39+
* The value stored into didFinishLaunchingWithOptions notification's
40+
* userInfo under 'UIApplicationLaunchOptionsLocalNotificationKey';
41+
* otherwise, null.
42+
*/
43+
ios: unknown;
2344
root?: View | null;
2445
savedInstanceState?: any /* android.os.Bundle */;
2546
}
2647

2748
export interface OrientationChangedEventData extends ApplicationEventData {
49+
android: any /* globalAndroid.app.Application */;
2850
newValue: 'portrait' | 'landscape' | 'unknown';
2951
}
3052

3153
export interface SystemAppearanceChangedEventData extends ApplicationEventData {
54+
android: any /* globalAndroid.app.Application */;
3255
newValue: 'light' | 'dark';
3356
}
3457

@@ -42,15 +65,14 @@ export interface DiscardedErrorEventData extends ApplicationEventData {
4265
error: NativeScriptError;
4366
}
4467

45-
export interface CssChangedEventData extends EventData {
68+
export interface CssChangedEventData extends ApplicationEventData {
4669
cssFile?: string;
4770
cssText?: string;
4871
}
4972

50-
export interface AndroidActivityEventData {
73+
export interface AndroidActivityEventData extends ApplicationEventData {
5174
activity: any /* androidx.appcompat.app.AppCompatActivity */;
52-
eventName: string;
53-
object: any;
75+
object: any /* AndroidApplication */;
5476
}
5577

5678
export interface AndroidActivityBundleEventData extends AndroidActivityEventData {
@@ -84,6 +106,6 @@ export interface RootViewControllerImpl {
84106
contentController: any;
85107
}
86108

87-
export interface LoadAppCSSEventData extends EventData {
109+
export interface LoadAppCSSEventData extends ApplicationEventData {
88110
cssFile: string;
89111
}

packages/core/application/index.android.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { AndroidActivityBackPressedEventData, AndroidActivityBundleEventData, An
44

55
// TODO: explain why we need to this or remov it
66
// Use requires to ensure order of imports is maintained
7-
const appCommon = require('./application-common');
7+
const appCommon = require('./application-common') as typeof import('./application-common');
88

99
// First reexport so that app module is initialized.
1010
export * from './application-common';

packages/core/application/index.d.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ export function _resetRootView(entry?: NavigationEntry | string);
257257
/**
258258
* Removes listener for the specified event name.
259259
*/
260-
export function off(eventNames: string, callback?: (eventData: EventData) => void, thisArg?: any, options?: EventListenerOptions | boolean): void;
260+
export function off(eventNames: string, callback?: (eventData: ApplicationEventData) => void, thisArg?: any, options?: EventListenerOptions | boolean): void;
261261

262262
/**
263263
* Shortcut alias to the removeEventListener method.
@@ -266,13 +266,13 @@ export function off(eventNames: string, callback?: (eventData: EventData) => voi
266266
* @param thisArg - An optional parameter which will be used as `this` context for callback execution.
267267
* @param options An optional parameter. If passed as a boolean, configures the useCapture value. Otherwise, specifies options.
268268
*/
269-
export function off(eventNames: string, callback?: (eventData: EventData) => void, thisArg?: any, options?: EventListenerOptions | boolean): void;
269+
export function off(eventNames: string, callback?: (eventData: ApplicationEventData) => void, thisArg?: any, options?: EventListenerOptions | boolean): void;
270270

271271
/**
272272
* Notifies all the registered listeners for the event provided in the data.eventName.
273273
* @param data The data associated with the event.
274274
*/
275-
export function notify(data: any): void;
275+
export function notify<T extends ApplicationEventData>(data: T, options?: CustomEventInit): void;
276276

277277
/**
278278
* Checks whether a listener is registered for the specified event name.
@@ -297,18 +297,13 @@ export function on(event: 'cssChanged', callback: (args: CssChangedEventData) =>
297297
/**
298298
* Event raised then livesync operation is performed.
299299
*/
300-
export function on(event: 'livesync', callback: (args: EventData) => void, thisArg?: any, options?: AddEventListenerOptions | boolean): void;
300+
export function on(event: 'livesync', callback: (args: ApplicationEventData) => void, thisArg?: any, options?: AddEventListenerOptions | boolean): void;
301301

302302
/**
303303
* This event is raised when application css is changed.
304304
*/
305305
export function on(event: 'cssChanged', callback: (args: CssChangedEventData) => void, thisArg?: any, options?: AddEventListenerOptions | boolean): void;
306306

307-
/**
308-
* Event raised then livesync operation is performed.
309-
*/
310-
export function on(event: 'livesync', callback: (args: EventData) => void, thisArg?: any, options?: AddEventListenerOptions | boolean): void;
311-
312307
/**
313308
* This event is raised on application launchEvent.
314309
*/
@@ -319,7 +314,7 @@ export function on(event: 'launch', callback: (args: LaunchEventData) => void, t
319314
* Its intent is to be suitable for measuring app startup times.
320315
* @experimental
321316
*/
322-
export function on(event: 'displayed', callback: (args: EventData) => void, thisArg?: any, options?: AddEventListenerOptions | boolean): void;
317+
export function on(event: 'displayed', callback: (args: ApplicationEventData) => void, thisArg?: any, options?: AddEventListenerOptions | boolean): void;
323318

324319
/**
325320
* This event is raised when the Application is suspended.

packages/core/application/index.ios.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@ import { ApplicationEventData, CssChangedEventData, LaunchEventData, LoadAppCSSE
44

55
// TODO: explain why we need to this or remov it
66
// Use requires to ensure order of imports is maintained
7-
const { backgroundEvent, displayedEvent, exitEvent, foregroundEvent, getCssFileName, launchEvent, livesync, lowMemoryEvent, notify, on, orientationChanged, orientationChangedEvent, resumeEvent, setApplication, suspendEvent, systemAppearanceChanged, systemAppearanceChangedEvent } = require('./application-common');
7+
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');
88
// First reexport so that app module is initialized.
99
export * from './application-common';
1010

1111
import { View } from '../ui/core/view';
1212
import { NavigationEntry } from '../ui/frame/frame-interfaces';
1313
// TODO: Remove this and get it from global to decouple builder for angular
1414
import { Builder } from '../ui/builder';
15-
import { Observable } from '../data/observable';
1615
import { CSSUtils } from '../css/system-classes';
1716
import { IOSHelper } from '../ui/core/view/view-helper';
1817
import { Device } from '../platform';
@@ -238,7 +237,7 @@ export class iOSApplication implements iOSApplicationDefinition {
238237
const args: LaunchEventData = {
239238
eventName: launchEvent,
240239
object: this,
241-
ios: (notification && notification.userInfo && notification.userInfo.objectForKey('UIApplicationLaunchOptionsLocalNotificationKey')) || null,
240+
ios: notification?.userInfo?.objectForKey('UIApplicationLaunchOptionsLocalNotificationKey') || null,
242241
};
243242

244243
notify(args);

0 commit comments

Comments
 (0)