src/utils/document.service.ts
Properties |
|
Methods |
handleClick | ||||||
handleClick(callback: EventHandler)
|
||||||
Defined in src/utils/document.service.ts:27
|
||||||
Parameters :
Returns :
void
|
handleEvent | |||||||||
handleEvent(eventType: string, callback: EventHandler)
|
|||||||||
Defined in src/utils/document.service.ts:15
|
|||||||||
Parameters :
Returns :
void
|
ngOnDestroy |
ngOnDestroy()
|
Defined in src/utils/document.service.ts:31
|
Returns :
void
|
Protected documentRef |
documentRef:
|
Default value : document
|
Defined in src/utils/document.service.ts:11
|
Protected globalEvents |
globalEvents:
|
Default value : new Map<string, Observable<Event>>()
|
Defined in src/utils/document.service.ts:9
|
Protected subscriptions |
subscriptions:
|
Default value : new Subscription()
|
Defined in src/utils/document.service.ts:13
|
import { Injectable, OnDestroy } from "@angular/core";
import { Observable, Subscription } from "rxjs";
import { EventHandler, getEventObservable } from "./event.service";
@Injectable({
providedIn: "root"
})
export class DocumentService implements OnDestroy {
protected globalEvents = new Map<string, Observable<Event>>();
protected documentRef = document;
protected subscriptions = new Subscription();
handleEvent(eventType: string, callback: EventHandler) {
if (!this.globalEvents.has(eventType)) {
if (this.documentRef) {
this.globalEvents.set(eventType, getEventObservable(this.documentRef as any, eventType));
} else {
this.globalEvents.set(eventType, new Observable());
}
}
const observable = this.globalEvents.get(eventType);
this.subscriptions.add(observable.subscribe(callback));
}
handleClick(callback: EventHandler) {
this.handleEvent("click", callback);
}
ngOnDestroy() {
this.subscriptions.unsubscribe();
this.globalEvents = null;
}
}