Skip to content

Commit 2473004

Browse files
why520crazywalkerkay
authored andcommitted
feat(planet): update type of BootstrapAppModule for ts strict mode #OSP-248
1 parent 8a9b352 commit 2473004

17 files changed

+90
-84
lines changed

packages/planet/src/application/planet-application-loader.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ describe('PlanetApplicationLoader', () => {
405405

406406
const app1Host = document.querySelector(app1.selector);
407407
planetApplicationLoader.reroute({ url: '/app2/dashboard' });
408-
expect(app1Host.getAttribute('style')).toContain('display:none;');
408+
expect(app1Host!.getAttribute('style')).toContain('display:none;');
409409
expect(appStatusChangeFaker.spy).toHaveBeenCalledTimes(7);
410410

411411
tick();
@@ -732,7 +732,7 @@ describe('PlanetApplicationLoader', () => {
732732
name: 'app100',
733733
switchMode: SwitchModes.coexist,
734734
routerPathPrefix: '',
735-
hostParent: undefined
735+
hostParent: ''
736736
});
737737
expect(result).toEqual(true);
738738
});

packages/planet/src/application/planet-application-loader.ts

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export interface AppStatusChangeEvent {
3838
export class PlanetApplicationLoader {
3939
private firstLoad = true;
4040

41-
private startRouteChangeEvent: PlanetRouterEvent;
41+
private startRouteChangeEvent!: PlanetRouterEvent;
4242

4343
private options: PlanetOptions;
4444

@@ -188,7 +188,9 @@ export class PlanetApplicationLoader {
188188
appStatus === ApplicationStatus.loadError
189189
) {
190190
debug(
191-
`app(${app.name}) status is ${ApplicationStatus[appStatus]}, start load assets`
191+
`app(${app.name}) status is ${
192+
ApplicationStatus[appStatus as ApplicationStatus]
193+
}, start load assets`
192194
);
193195
hasAppsNeedLoadingAssets = true;
194196
return this.ngZone.runOutsideAngular(() => {
@@ -215,7 +217,7 @@ export class PlanetApplicationLoader {
215217
);
216218
this.showApp(app);
217219
const appRef = getPlanetApplicationRef(app.name);
218-
appRef.navigateByUrl(event.url);
220+
appRef?.navigateByUrl(event.url);
219221
this.setAppStatus(app, ApplicationStatus.active);
220222
this.setLoadingDone();
221223
return of(app);
@@ -235,16 +237,18 @@ export class PlanetApplicationLoader {
235237
debug(`[routeChange] app(${app.name}) is active, do nothings`);
236238
const appRef = getPlanetApplicationRef(app.name);
237239
// Backwards compatibility sub app use old version which has not getCurrentRouterStateUrl
238-
const currentUrl = appRef.getCurrentRouterStateUrl
240+
const currentUrl = appRef?.getCurrentRouterStateUrl
239241
? appRef.getCurrentRouterStateUrl()
240242
: '';
241243
if (currentUrl !== event.url) {
242-
appRef.navigateByUrl(event.url);
244+
appRef?.navigateByUrl(event.url);
243245
}
244246
return of(app);
245247
} else {
246248
debug(
247-
`[routeChange] app(${app.name}) status is ${ApplicationStatus[appStatus]}`
249+
`[routeChange] app(${app.name}) status is ${
250+
ApplicationStatus[appStatus as ApplicationStatus]
251+
}`
248252
);
249253
return this.getAppStatusChange$(app).pipe(
250254
take(1),
@@ -298,7 +302,7 @@ export class PlanetApplicationLoader {
298302
.subscribe();
299303
}
300304

301-
private startLoadAppAssets(app: PlanetApplication) {
305+
private startLoadAppAssets(app: PlanetApplication): Observable<PlanetApplication> {
302306
if (this.inProgressAppAssetsLoads.get(app.name)) {
303307
return this.inProgressAppAssetsLoads.get(app.name);
304308
} else {
@@ -325,15 +329,15 @@ export class PlanetApplicationLoader {
325329

326330
private hideApp(planetApp: PlanetApplication) {
327331
const appRef = getPlanetApplicationRef(planetApp.name);
328-
const appRootElement = document.querySelector(appRef.selector || planetApp.selector);
332+
const appRootElement = document.querySelector(appRef?.selector || (planetApp.selector as string));
329333
if (appRootElement) {
330334
appRootElement.setAttribute('style', 'display:none;');
331335
}
332336
}
333337

334338
private showApp(planetApp: PlanetApplication) {
335339
const appRef = getPlanetApplicationRef(planetApp.name);
336-
const appRootElement = document.querySelector(appRef.selector || planetApp.selector);
340+
const appRootElement = document.querySelector(appRef?.selector || (planetApp.selector as string));
337341
if (appRootElement) {
338342
appRootElement.setAttribute('style', '');
339343
}
@@ -345,9 +349,9 @@ export class PlanetApplicationLoader {
345349
appRef.destroy();
346350
}
347351
const container = getHTMLElement(planetApp.hostParent);
348-
const appRootElement = container.querySelector((appRef && appRef.selector) || planetApp.selector);
352+
const appRootElement = container?.querySelector((appRef && appRef.selector) || (planetApp.selector as string));
349353
if (appRootElement) {
350-
container.removeChild(appRootElement);
354+
container?.removeChild(appRootElement);
351355
}
352356
}
353357

@@ -362,12 +366,12 @@ export class PlanetApplicationLoader {
362366
const container = getHTMLElement(app.hostParent);
363367
let appRootElement: HTMLElement;
364368
if (container) {
365-
appRootElement = container.querySelector(appRef.selector || app.selector);
369+
appRootElement = container.querySelector(appRef.selector || app.selector!)!;
366370
if (!appRootElement) {
367371
if (appRef.template) {
368-
appRootElement = createElementByTemplate(appRef.template);
372+
appRootElement = createElementByTemplate(appRef.template)!;
369373
} else {
370-
appRootElement = document.createElement(app.selector);
374+
appRootElement = document.createElement(app.selector!);
371375
}
372376
appRootElement.setAttribute('style', 'display:none;');
373377
if (app.hostClass) {
@@ -381,7 +385,7 @@ export class PlanetApplicationLoader {
381385
}
382386
let result = appRef.bootstrap(this.portalApp);
383387
// Backwards compatibility promise for bootstrap
384-
if (result['then']) {
388+
if ((result as any)['then']) {
385389
result = from(result) as Observable<PlanetApplicationRef>;
386390
}
387391
return result.pipe(
@@ -452,7 +456,7 @@ export class PlanetApplicationLoader {
452456
private preloadApps(activeApps?: PlanetApplication[]) {
453457
setTimeout(() => {
454458
const toPreloadApps = this.planetApplicationService.getAppsToPreload(
455-
activeApps ? activeApps.map(item => item.name) : null
459+
activeApps ? activeApps.map(item => item.name) : undefined
456460
);
457461
debug(`start preload apps: ${this.getAppNames(toPreloadApps)}`);
458462
const loadApps$ = toPreloadApps.map(preloadApp => {
@@ -490,7 +494,7 @@ export class PlanetApplicationLoader {
490494
this.routeChange$.next(event);
491495
}
492496

493-
private preloadInternal(app: PlanetApplication, immediate?: boolean): Observable<PlanetApplicationRef> {
497+
private preloadInternal(app: PlanetApplication, immediate?: boolean): Observable<PlanetApplicationRef | null> {
494498
const status = this.appsStatus.get(app);
495499
if (!status || status === ApplicationStatus.loadError) {
496500
debug(`preload app(${app.name}), status is empty, start to load assets`);

packages/planet/src/application/planet-application-ref.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,21 @@ export interface BootstrapOptions {
1313
bootstrap: BootstrapAppModule;
1414
}
1515

16-
export type BootstrapAppModule = (portalApp?: PlanetPortalApplication) => Promise<NgModuleRef<any>>;
16+
export type BootstrapAppModule = (portalApp: PlanetPortalApplication) => Promise<NgModuleRef<any> | undefined | null>;
1717

1818
export type PlantComponentFactory = <TData, TComp>(
1919
componentName: string,
2020
config: PlantComponentConfig<TData>
2121
) => PlanetComponentRef<TComp>;
2222

2323
export class PlanetApplicationRef {
24-
public appModuleRef: NgModuleRef<any>;
25-
public template: string;
26-
private innerSelector: string;
24+
public appModuleRef?: NgModuleRef<any>;
25+
public template?: string;
26+
private innerSelector?: string;
2727
private name: string;
28-
private portalApp: PlanetPortalApplication;
29-
private appModuleBootstrap: (app: PlanetPortalApplication) => Promise<NgModuleRef<any>>;
30-
private componentFactory: PlantComponentFactory;
28+
private portalApp!: PlanetPortalApplication;
29+
private appModuleBootstrap?: BootstrapAppModule;
30+
private componentFactory?: PlantComponentFactory;
3131

3232
public get selector() {
3333
return this.innerSelector;
@@ -37,19 +37,19 @@ export class PlanetApplicationRef {
3737
return !!this.appModuleRef;
3838
}
3939

40-
public get ngZone(): NgZone {
41-
return this.appModuleRef.injector.get(NgZone);
40+
public get ngZone(): NgZone | undefined {
41+
return this.appModuleRef?.injector.get(NgZone);
4242
}
4343

4444
public get sandbox(): Sandbox {
4545
return getSandboxInstance();
4646
}
4747

48-
constructor(name: string, options: BootstrapOptions) {
48+
constructor(name: string, options?: BootstrapOptions) {
4949
this.name = name;
5050
if (options) {
5151
this.template = options.template;
52-
this.innerSelector = this.template ? getTagNameByTemplate(this.template) : null;
52+
this.innerSelector = this.template ? getTagNameByTemplate(this.template) : '';
5353
this.appModuleBootstrap = options.bootstrap;
5454
}
5555
// This is a hack, since NgZone doesn't allow you to configure the property that identifies your zone.
@@ -62,15 +62,15 @@ export class PlanetApplicationRef {
6262

6363
// 子应用路由变化后同步修改 portal 的 Route
6464
private syncPortalRouteWhenNavigationEnd() {
65-
const router = this.appModuleRef.injector.get(Router);
65+
const router = this.appModuleRef?.injector.get(Router);
6666
if (router) {
6767
router.events.subscribe(event => {
6868
if (event instanceof NavigationEnd) {
69-
this.ngZone.onStable
69+
this.ngZone?.onStable
7070
.asObservable()
7171
.pipe(take(1))
7272
.subscribe(() => {
73-
this.portalApp.router.navigateByUrl(event.url);
73+
this.portalApp.router!.navigateByUrl(event.url);
7474
});
7575
}
7676
});
@@ -84,7 +84,7 @@ export class PlanetApplicationRef {
8484
this.portalApp = app;
8585
return from(
8686
this.appModuleBootstrap(app).then(appModuleRef => {
87-
this.appModuleRef = appModuleRef;
87+
this.appModuleRef = appModuleRef!;
8888
this.appModuleRef.instance.appName = this.name;
8989
this.syncPortalRouteWhenNavigationEnd();
9090
return this;
@@ -93,17 +93,17 @@ export class PlanetApplicationRef {
9393
}
9494

9595
getRouter() {
96-
return this.appModuleRef.injector.get(Router);
96+
return this.appModuleRef?.injector.get(Router);
9797
}
9898

9999
getCurrentRouterStateUrl() {
100-
return this.getRouter().routerState.snapshot.url;
100+
return this.getRouter()?.routerState.snapshot.url;
101101
}
102102

103103
navigateByUrl(url: string): void {
104104
const router = this.getRouter();
105-
this.ngZone.run(() => {
106-
router.navigateByUrl(url);
105+
this.ngZone?.run(() => {
106+
router?.navigateByUrl(url);
107107
});
108108
}
109109

@@ -125,7 +125,7 @@ export class PlanetApplicationRef {
125125
this.sandbox.destroy();
126126
}
127127
this.appModuleRef.destroy();
128-
delete this.appModuleRef;
128+
this.appModuleRef = undefined;
129129
}
130130
}
131131
}

packages/planet/src/application/planet-application.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export class PlanetApplicationService {
5858
}
5959
}
6060

61-
getAppsByMatchedUrl<TExtra>(url: string): PlanetApplication<TExtra>[] {
61+
getAppsByMatchedUrl<TExtra = unknown>(url: string): PlanetApplication<TExtra>[] {
6262
return this.getApps().filter(app => {
6363
if (app.routerPathPrefix instanceof RegExp) {
6464
return app.routerPathPrefix.test(url);

packages/planet/src/application/portal-application.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,26 @@ import { NgZone, Injector, ApplicationRef } from '@angular/core';
33
import { GlobalEventDispatcher } from '../global-event-dispatcher';
44

55
export class PlanetPortalApplication<TData = any> {
6-
applicationRef: ApplicationRef;
7-
injector: Injector;
8-
router: Router;
9-
ngZone: NgZone;
10-
globalEventDispatcher: GlobalEventDispatcher;
11-
data: TData;
6+
applicationRef?: ApplicationRef;
7+
injector?: Injector;
8+
router?: Router;
9+
ngZone?: NgZone;
10+
globalEventDispatcher?: GlobalEventDispatcher;
11+
data?: TData;
1212

1313
navigateByUrl(url: string | UrlTree, extras?: NavigationExtras): Promise<boolean> {
14-
return this.ngZone.run(() => {
15-
return this.router.navigateByUrl(url, extras);
14+
return this.ngZone!.run(() => {
15+
return this.router!.navigateByUrl(url, extras);
1616
});
1717
}
1818

1919
run<T>(fn: (...args: any[]) => T): T {
20-
return this.ngZone.run<T>(() => {
20+
return this.ngZone!.run<T>(() => {
2121
return fn();
2222
});
2323
}
2424

2525
tick() {
26-
this.applicationRef.tick();
26+
this.applicationRef!.tick();
2727
}
2828
}

packages/planet/src/debug.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export interface Debug {
3232
/**
3333
* Debug factory for debug module
3434
*/
35-
let _debugFactory: Debug;
35+
let _debugFactory: Debug | undefined;
3636
let _debuggerMap: Record<string, Debugger> = {};
3737

3838
export function createDebug(namespace: string): Debugger {
@@ -57,7 +57,7 @@ export function setDebugFactory(debug: Debug) {
5757
}
5858

5959
export function clearDebugFactory() {
60-
setDebugFactory(undefined);
60+
_debugFactory = undefined;
6161
_debuggerMap = {};
6262
}
6363

packages/planet/src/global-planet.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ declare const window: any;
99
export interface GlobalPlanet {
1010
apps: { [key: string]: PlanetApplicationRef };
1111
portalApplication?: PlanetPortalApplication;
12-
applicationLoader?: PlanetApplicationLoader;
13-
applicationService?: PlanetApplicationService;
12+
applicationLoader: PlanetApplicationLoader;
13+
applicationService: PlanetApplicationService;
1414
}
1515

1616
export const globalPlanet: GlobalPlanet = (window.planet = window.planet || {
@@ -31,7 +31,7 @@ export function defineApplication(name: string, options: BootstrapAppModule | Bo
3131
globalPlanet.apps[name] = appRef;
3232
}
3333

34-
export function getPlanetApplicationRef(appName: string): PlanetApplicationRef {
34+
export function getPlanetApplicationRef(appName: string): PlanetApplicationRef | null {
3535
if (globalPlanet && globalPlanet.apps && globalPlanet.apps[appName]) {
3636
return globalPlanet.apps[appName];
3737
} else {
@@ -40,11 +40,13 @@ export function getPlanetApplicationRef(appName: string): PlanetApplicationRef {
4040
}
4141

4242
export function setPortalApplicationData<T>(data: T) {
43-
globalPlanet.portalApplication.data = data;
43+
if (globalPlanet.portalApplication) {
44+
globalPlanet.portalApplication.data = data;
45+
}
4446
}
4547

4648
export function getPortalApplicationData<TData>(): TData {
47-
return globalPlanet.portalApplication.data as TData;
49+
return globalPlanet.portalApplication?.data as TData;
4850
}
4951

5052
export function setApplicationLoader(loader: PlanetApplicationLoader) {

packages/planet/src/helpers.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ describe('helpers', () => {
153153

154154
it('should get tagName when template is empty', () => {
155155
const tagName = getTagNameByTemplate(``);
156-
expect(tagName).toBe(null);
156+
expect(tagName).toBe('');
157157
});
158158
});
159159

0 commit comments

Comments
 (0)