-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathremote-module.directive.ts
44 lines (39 loc) · 1.63 KB
/
remote-module.directive.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { AfterContentInit, Directive, Input } from '@angular/core';
import {
RemoteModuleResult,
RemoteModuleResultTypes,
RemoteModuleService,
} from './remote-module.service';
import { LoadRemoteModuleOptions } from '@angular-architects/module-federation';
export type RemoteModuleDirectiveOptions = LoadRemoteModuleOptions & {
id: string;
};
@Directive({
selector: '[remoteModule]',
standalone: true,
})
export class RemoteModuleDirective implements AfterContentInit {
public constructor(private readonly _remoteModuleService: RemoteModuleService) {}
@Input({ required: true })
public remoteModuleoptions!: RemoteModuleDirectiveOptions;
@Input()
public loadRemoteModuleCallback?: (webpackModule: any) => void | Promise<void>;
public async ngAfterContentInit(): Promise<void> {
// RemoteModuleDirectiveOptions type has the same shape as LoadRemoteModuleOptionsExtended
// so I can just flow it into the remoteModuleService.loadAsync call
const result: RemoteModuleResult = await this._remoteModuleService.loadAsync(this.remoteModuleoptions);
switch (result.type) {
case RemoteModuleResultTypes.Loaded:
// if this.loadRemoteModuleCallback is not defined then use a callback function that does nothing
const callback = this.loadRemoteModuleCallback ?? function (_: any): void {};
await callback(result.webpackModule);
break;
case RemoteModuleResultTypes.Failed:
break;
default:
// see https://www.typescriptlang.org/docs/handbook/2/narrowing.html#exhaustiveness-checking
const _exhaustiveCheck: never = result;
return _exhaustiveCheck;
}
}
}