-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a75de04
commit 1d880e4
Showing
3 changed files
with
84 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,48 @@ | ||
import { Injectable, NgZone } from '@angular/core'; | ||
import { Subject } from 'rxjs'; | ||
import { Injectable, NgZone, inject } from '@angular/core'; | ||
import { Subject, Subscription } from 'rxjs'; | ||
import { KeysEvent } from './keysEvent'; | ||
|
||
@Injectable({ providedIn: 'root' }) | ||
export class MagnifyService { | ||
keyEvents = new KeysEvent(); | ||
private keyEvents = new KeysEvent(); | ||
currentMousePosition: MouseEvent; | ||
currentMouse$ = new Subject<MouseEvent>(); | ||
z$ = new Subject<boolean>(); | ||
private ngZone = inject(NgZone); | ||
private zSubscription: Subscription; | ||
private observersIds = new Set<string>(); | ||
private isConnected = false; | ||
|
||
constructor(private ngZone: NgZone) { | ||
constructor() {} | ||
|
||
private onMouseMove = (ev: MouseEvent) => { | ||
this.currentMousePosition = ev; | ||
this.currentMouse$.next(ev); | ||
}; | ||
|
||
add(id: string) { | ||
this.observersIds.add(id); | ||
if (!this.isConnected) this.connect(); | ||
} | ||
|
||
remove(id: string) { | ||
this.observersIds.delete(id); | ||
if (this.observersIds.size === 0) this.disconnect(); | ||
} | ||
|
||
private disconnect() { | ||
document.removeEventListener('mousemove', this.onMouseMove); | ||
this.zSubscription?.unsubscribe(); | ||
this.isConnected = false; | ||
} | ||
|
||
private connect() { | ||
this.ngZone.runOutsideAngular(() => { | ||
document.addEventListener('mousemove', (ev) => this.onMouseMove(ev)); | ||
this.keyEvents.event('z').subscribe(([res, ev]) => { | ||
this.isConnected = true; | ||
document.addEventListener('mousemove', ev => this.onMouseMove(ev)); | ||
this.zSubscription = this.keyEvents.event('z').subscribe(([res, ev]) => { | ||
this.z$.next(res); | ||
}); | ||
}); | ||
} | ||
|
||
onMouseMove = (ev: MouseEvent) => { | ||
this.currentMousePosition = ev; | ||
this.currentMouse$.next(ev); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters