ngx-admin/docs/app/@theme/services/iframe-communicator.service.ts
Sergey Andrievskiy 2129689f98 feat: docs app
2020-08-28 19:29:11 +03:00

25 lines
711 B
TypeScript

import { Inject, Injectable } from '@angular/core';
import { Observable, fromEvent as observableFromEvent } from 'rxjs';
import { filter, map } from 'rxjs/operators';
import { NB_WINDOW } from '@nebular/theme';
@Injectable()
export class NgxIframeCommunicatorService {
constructor(@Inject(NB_WINDOW) private window) {
}
public send(payload: any, target: Window = this.window.parent) {
if (target !== this.window) {
target.postMessage(payload, '*');
}
}
public receive(id: string): Observable<any> {
return observableFromEvent(this.window, 'message')
.pipe(
filter((msg: any) => msg.data && msg.data.id === id),
map((msg: any) => msg.data),
);
}
}