Skip to content
Snippets Groups Projects
native-event.service.ts 935 B
Newer Older
  • Learn to ignore specific revisions
  • import { Inject, Injectable } from '@angular/core';
    import {
      from, fromEvent, Observable, Subject
    } from 'rxjs';
    import { DOCUMENT } from '@angular/common';
    import { mergeMap } from 'rxjs/operators';
    
    @Injectable({
      providedIn: 'root'
    })
    export class NativeEventService {
      private _focus = new Subject<boolean>();
    
      private _mouseUp = new Subject<Event>();
    
    
      constructor(@Inject(DOCUMENT) private document: Document) {
        from(['blur', 'focus'])
          .pipe(
            mergeMap(event => fromEvent(window, event))
          )
          .subscribe(
            () => this._focus.next(document.hasFocus())// Do something with the event here
          );
    
    
        fromEvent(window, 'mouseup')
          .subscribe((mouseEvent: Event) => {
            this._mouseUp.next(mouseEvent);
          });
    
      }
    
      get focus(): Observable<boolean> {
        return this._focus.asObservable();
      }
    
    
      get mouseUp(): Observable<Event> {
        return this._mouseUp.asObservable();
      }