ucap-lg-web/src/app/app.component.ts

45 lines
1.2 KiB
TypeScript
Raw Normal View History

2020-04-08 07:46:41 +00:00
import { Component, OnDestroy, OnInit } from '@angular/core';
import { Store } from '@ngrx/store';
import { AppActions } from '@app/store/actions';
import { fromEvent, interval, Subscription } from 'rxjs';
import { debounce } from 'rxjs/operators';
2020-03-27 08:43:47 +00:00
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
2020-04-08 07:46:41 +00:00
export class AppComponent implements OnInit, OnDestroy {
private resizeWindowSubscription: Subscription;
constructor(private store: Store<any>) {
this.resizeWindowSubscription = fromEvent(window, 'resize')
.pipe(debounce(() => interval(100)))
.subscribe((event: any) => {
this.dispatchWindowSize({
width: event.target.innerWidth,
height: event.target.innerHeight
});
});
}
ngOnInit(): void {
this.dispatchWindowSize({
width: window.innerWidth,
height: window.innerHeight
});
}
ngOnDestroy(): void {
if (!!this.resizeWindowSubscription) {
this.resizeWindowSubscription.unsubscribe();
}
}
2020-03-27 08:43:47 +00:00
2020-04-08 07:46:41 +00:00
private dispatchWindowSize(size: { width: number; height: number }) {
this.store.dispatch(AppActions.windowResized(size));
}
2020-03-27 08:43:47 +00:00
}