Skip to content

Commit f625f5b

Browse files
committed
fix: clean up Application types
fix: global types
1 parent df79ab5 commit f625f5b

File tree

7 files changed

+48
-42
lines changed

7 files changed

+48
-42
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 { EventData } 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);
54-
export const off = global.NativeScriptGlobals.events.off.bind(global.NativeScriptGlobals.events);
55-
export const notify = global.NativeScriptGlobals.events.notify.bind(global.NativeScriptGlobals.events);
56-
export const hasListeners = global.NativeScriptGlobals.events.hasListeners.bind(global.NativeScriptGlobals.events);
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,21 +257,21 @@ 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): void;
260+
export function off(eventNames: string, callback?: (eventData: ApplicationEventData) => void, thisArg?: any): void;
261261

262262
/**
263263
* Shortcut alias to the removeEventListener method.
264264
* @param eventNames - String corresponding to events (e.g. "onLaunch").
265265
* @param callback - Callback function which will be removed.
266266
* @param thisArg - An optional parameter which will be used as `this` context for callback execution.
267267
*/
268-
export function off(eventNames: string, callback?: (eventData: EventData) => void, thisArg?: any): void;
268+
export function off(eventNames: string, callback?: (eventData: ApplicationEventData) => void, thisArg?: any): void;
269269

270270
/**
271271
* Notifies all the registered listeners for the event provided in the data.eventName.
272272
* @param data The data associated with the event.
273273
*/
274-
export function notify(data: any): void;
274+
export function notify<T extends ApplicationEventData>(data: T): void;
275275

276276
/**
277277
* Checks whether a listener is registered for the specified event name.
@@ -295,18 +295,13 @@ export function on(event: 'cssChanged', callback: (args: CssChangedEventData) =>
295295
/**
296296
* Event raised then livesync operation is performed.
297297
*/
298-
export function on(event: 'livesync', callback: (args: EventData) => void, thisArg?: any): void;
298+
export function on(event: 'livesync', callback: (args: ApplicationEventData) => void, thisArg?: any): void;
299299

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

305-
/**
306-
* Event raised then livesync operation is performed.
307-
*/
308-
export function on(event: 'livesync', callback: (args: EventData) => void, thisArg?: any): void;
309-
310305
/**
311306
* This event is raised on application launchEvent.
312307
*/
@@ -317,7 +312,7 @@ export function on(event: 'launch', callback: (args: LaunchEventData) => void, t
317312
* Its intent is to be suitable for measuring app startup times.
318313
* @experimental
319314
*/
320-
export function on(event: 'displayed', callback: (args: EventData) => void, thisArg?: any): void;
315+
export function on(event: 'displayed', callback: (args: ApplicationEventData) => void, thisArg?: any): void;
321316

322317
/**
323318
* 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);

packages/core/global-types.d.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,7 @@ declare namespace NodeJS {
2525
* Global framework event handling
2626
*/
2727
events: {
28-
on(eventNames: string, callback: (data: any) => void, thisArg?: any);
29-
on(event: 'propertyChange', callback: (data: any) => void, thisArg?: any);
30-
off(eventNames: string, callback?: any, thisArg?: any);
31-
addEventListener(eventNames: string, callback: (data: any) => void, thisArg?: any);
32-
removeEventListener(eventNames: string, callback?: any, thisArg?: any);
33-
set(name: string, value: any): void;
34-
setProperty(name: string, value: any): void;
35-
get(name: string): any;
36-
notify<T>(data: any): void;
37-
notifyPropertyChange(propertyName: string, value: any, oldValue?: any): void;
38-
hasListeners(eventName: string): boolean;
28+
[Key in keyof import('data/observable').Observable]: import('data/observable').Observable[Key];
3929
};
4030
launched: boolean;
4131
// used by various classes to setup callbacks to wire up global app event handling when the app instance is ready

0 commit comments

Comments
 (0)