In my angular application I listen to resize event and update the breakpoint name in breakpointChanged subject:
breakpointChanged = new Subject();
resize$ = fromEvent(window, "resize").pipe(
startWith(() => []),
map(() => [window.innerHeight, window.innerWidth]),
tap(([height, width]) => {
const name = this.breakpoint(width);
this.breakpointChanged.next(name);
})
);
Base on the breakpoint name I decided when the drawer should be open or not:
drawerOpened$ = this.breakpointChanged.pipe(
distinctUntilChanged(),
map((breakpointName) => {
return breakpointName !== "xs"; // if it's not xs breakpoint then it's true mean it's open.
})
);
I also want to use distinctUntilChanged because if the breakpoint not changed I don't want to activate the stream.
And in ngOnInit I subscribe to the resize stream. And I do it in runOutsideAngular, so I wont trigger change detection every time the resize happens.
this.zone.runOutsideAngular(() => {
this.resize$.subscribe();
});
The problem is the view doesn't update.
Only when I wrap the this.breakpointChanged.next(name); with this.zone.run(() => {}) it's works but if I do that I'll trigger change detection - always when the resize happens. which is bad thing.
Any idea how to solve this problem and make it nice and clean?
In the same context, how async pipe doesn't make change detection? the value is changing and async should subscribe and trigger when it's the value changes.