-
Notifications
You must be signed in to change notification settings - Fork 98
/
interval.component.ts
45 lines (39 loc) 路 1.31 KB
/
interval.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { Component, OnDestroy } from '@angular/core';
import { UntilDestroy, untilDestroyed } from '@ngneat/until-destroy';
import { fromEvent } from 'rxjs';
import { pluck, debounceTime, finalize } from 'rxjs/operators';
import { IntervalService } from './interval.service';
@UntilDestroy({ checkProperties: true })
@Component({
selector: 'app-interval',
templateUrl: './interval.component.html'
})
export class IntervalComponent implements OnDestroy {
valueFromIntervalService = 0;
subscription = fromEvent<MouseEvent>(document, 'mousemove')
.pipe(
debounceTime(200),
pluck<MouseEvent, number>('clientX'),
finalize(() => console.log('IntervalComponent fromEvent stream has completed'))
)
.subscribe(clientX => {
console.log(`Mouse clientX position is ${clientX}`);
});
private intervalService = new IntervalService();
constructor() {
this.intervalService.interval$
.pipe(
untilDestroyed(this),
finalize(() =>
console.log('IntervalComponent intervalService.interval$ stream has completed')
)
)
.subscribe(value => {
console.log(`IntervalService emitted value inside component ${value}`);
this.valueFromIntervalService = value;
});
}
ngOnDestroy(): void {
this.intervalService.destroy();
}
}