File

src/utils/document.service.ts

Index

Properties
Methods

Methods

handleClick
handleClick(callback: EventHandler)
Parameters :
Name Type Optional
callback EventHandler No
Returns : void
handleEvent
handleEvent(eventType: string, callback: EventHandler)
Parameters :
Name Type Optional
eventType string No
callback EventHandler No
Returns : void
ngOnDestroy
ngOnDestroy()
Returns : void

Properties

Protected documentRef
documentRef:
Default value : document
Protected globalEvents
globalEvents:
Default value : new Map<string, Observable<Event>>()
Protected subscriptions
subscriptions:
Default value : new Subscription()
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;
	}
}

result-matching ""

    No results matching ""