Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
2022-05-31-part-2-angular-shell-application
- Loading branch information
Showing
17 changed files
with
219 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Empty file.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
|
||
import { FrameComponent } from './frame.component'; | ||
|
||
describe('FrameComponent', () => { | ||
let component: FrameComponent; | ||
let fixture: ComponentFixture<FrameComponent>; | ||
|
||
beforeEach(async () => { | ||
await TestBed.configureTestingModule({ | ||
declarations: [FrameComponent], | ||
}).compileComponents(); | ||
}); | ||
|
||
beforeEach(() => { | ||
fixture = TestBed.createComponent(FrameComponent); | ||
component = fixture.componentInstance; | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it('should create', () => { | ||
expect(component).toBeTruthy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { | ||
AfterViewInit, | ||
Component, | ||
ElementRef, | ||
HostListener, | ||
ViewChild, | ||
} from '@angular/core'; | ||
import { DomSanitizer } from '@angular/platform-browser'; | ||
|
||
import { environment } from '../../environments/environment'; | ||
import { MessageService } from './services/message.service'; | ||
|
||
@Component({ | ||
template: `<iframe | ||
#childWindow | ||
[src]="iframeUrl" | ||
width="400px" | ||
height="400px" | ||
></iframe>`, | ||
providers: [MessageService], | ||
}) | ||
export class FrameComponent implements AfterViewInit { | ||
@ViewChild('childWindow') | ||
public readonly iframe!: ElementRef<HTMLIFrameElement>; | ||
|
||
public readonly iframeUrl = this.sanitizer.bypassSecurityTrustResourceUrl( | ||
environment.iframeUrl | ||
); | ||
|
||
constructor( | ||
private messageService: MessageService, | ||
private sanitizer: DomSanitizer | ||
) {} | ||
|
||
public ngAfterViewInit(): void { | ||
this.messageService.target = this.iframe; | ||
} | ||
|
||
@HostListener('window:message', ['$event']) | ||
private message(event: MessageEvent) { | ||
this.messageService.requestActivity(event); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { NgModule } from '@angular/core'; | ||
import { CommonModule } from '@angular/common'; | ||
import { Routes, RouterModule } from '@angular/router'; | ||
import { HttpClientModule } from '@angular/common/http'; | ||
|
||
import { FrameComponent } from './frame.component'; | ||
import { ActivityService } from './services/activity.service'; | ||
|
||
const routes: Routes = [{ path: '', component: FrameComponent }]; | ||
|
||
@NgModule({ | ||
declarations: [FrameComponent], | ||
imports: [CommonModule, RouterModule.forChild(routes), HttpClientModule], | ||
providers: [ActivityService] | ||
}) | ||
export class FrameModule {} |
22 changes: 22 additions & 0 deletions
22
apps/angular-shell/src/app/frame/services/activity.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { HttpClient, HttpParams } from '@angular/common/http'; | ||
import { Injectable } from '@angular/core'; | ||
import { Observable } from 'rxjs'; | ||
|
||
import { ActivityItem } from '@demo--nx-iframe-microfrontends/models'; | ||
|
||
@Injectable() | ||
export class ActivityService { | ||
constructor(private http: HttpClient) { | ||
this.getActivity().subscribe(console.log) | ||
} | ||
|
||
public getActivity(): Observable<ActivityItem> { | ||
const params = new HttpParams().set('participants', 1); | ||
return this.http.get<ActivityItem>( | ||
'http://www.boredapi.com/api/activity', | ||
{ | ||
params, | ||
} | ||
); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
apps/angular-shell/src/app/frame/services/message.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { Injectable, ElementRef, OnDestroy } from '@angular/core'; | ||
import { debounceTime, Subject, Subscription, switchMap } from 'rxjs'; | ||
|
||
import { ActivityService } from './activity.service'; | ||
|
||
@Injectable() | ||
export class MessageService implements OnDestroy { | ||
private incomingMessage$$ = new Subject<MessageEvent>(); | ||
private targetWindow: ElementRef<HTMLIFrameElement> | null = null; | ||
private subscription: Subscription | null = null; | ||
|
||
constructor(private activityService: ActivityService) { | ||
this.subscribeToMessages(); | ||
} | ||
|
||
public ngOnDestroy(): void { | ||
this.subscription?.unsubscribe(); | ||
} | ||
|
||
public set target(targetWindow: ElementRef<HTMLIFrameElement>) { | ||
this.targetWindow = targetWindow; | ||
} | ||
|
||
public requestActivity(event: MessageEvent): void { | ||
this.incomingMessage$$.next(event); | ||
} | ||
|
||
private subscribeToMessages(): void { | ||
this.subscription = this.incomingMessage$$ | ||
.pipe( | ||
debounceTime(100), | ||
switchMap(() => this.activityService.getActivity()) | ||
) | ||
.subscribe((v) => { | ||
this.targetWindow?.nativeElement.contentWindow?.postMessage(v, '*'); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export const environment = { | ||
production: true, | ||
iframeUrl: 'http://localhost:4200', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,4 @@ | ||
// This file can be replaced during build by using the `fileReplacements` array. | ||
// `ng build` replaces `environment.ts` with `environment.prod.ts`. | ||
// The list of file replacements can be found in `angular.json`. | ||
|
||
export const environment = { | ||
production: false, | ||
iframeUrl: 'http://localhost:4200', | ||
}; | ||
|
||
/* | ||
* For easier debugging in development mode, you can import the following file | ||
* to ignore zone related error stack frames such as `zone.run`, `zoneDelegate.invokeTask`. | ||
* | ||
* This import should be commented out in production mode because it will have a negative impact | ||
* on performance if an error is thrown. | ||
*/ | ||
// import 'zone.js/plugins/zone-error'; // Included with Angular CLI. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.