Skip to content

Commit

Permalink
feat(screen-orientation): Viewport add new service
Browse files Browse the repository at this point in the history
  • Loading branch information
waterplea committed Oct 14, 2024
1 parent dd04a00 commit 6fc1a0b
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions libs/screen-orientation/src/viewport.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import {inject, Injectable} from '@angular/core';
import {WINDOW} from '@ng-web-apis/common';
import {EMPTY, filter, fromEvent, map, merge, Observable, shareReplay} from 'rxjs';

@Injectable({
providedIn: 'root',
})
export class ViewportService extends Observable<VisualViewport> {
private readonly visualViewport = inject(WINDOW).visualViewport;

private readonly stream$ = this.visualViewport
? merge(
fromEvent(this.visualViewport, 'resize'),
fromEvent(this.visualViewport, 'scroll'),
fromEvent(this.visualViewport, 'scrollend'),
).pipe(
map(() => this.visualViewport),
filter(Boolean),
shareReplay({bufferSize: 1, refCount: true}),
)
: EMPTY;

constructor() {
super((subscriber) => this.stream$.subscribe(subscriber));
}
}

0 comments on commit 6fc1a0b

Please sign in to comment.