mirror of
https://github.com/Terncode/pixel.horse.git
synced 2026-09-24 21:55:52 +02:00
45 lines
1.0 KiB
TypeScript
45 lines
1.0 KiB
TypeScript
import { Injectable, NgZone } from '@angular/core';
|
|
import { removeItem } from '../../common/utils';
|
|
|
|
@Injectable({
|
|
providedIn: 'root',
|
|
})
|
|
export class IntervalUpdateService {
|
|
private interval: any;
|
|
private actions: (() => void)[] = [];
|
|
constructor(private zone: NgZone) {
|
|
}
|
|
subscribe(action: () => void) {
|
|
this.actions.push(action);
|
|
|
|
if (!this.interval) {
|
|
this.zone.runOutsideAngular(() => {
|
|
this.interval = setInterval(() => {
|
|
this.actions.forEach(a => a());
|
|
}, 1000 * 10);
|
|
});
|
|
}
|
|
|
|
return () => {
|
|
removeItem(this.actions, action);
|
|
|
|
if (this.actions.length === 0) {
|
|
clearInterval(this.interval);
|
|
this.interval = undefined;
|
|
}
|
|
};
|
|
}
|
|
toggle(action: () => void) {
|
|
let unsubscribe: (() => void) | undefined;
|
|
|
|
return (on: boolean) => {
|
|
if (on && !unsubscribe) {
|
|
unsubscribe = this.subscribe(action);
|
|
} else if (!on && unsubscribe) {
|
|
unsubscribe();
|
|
unsubscribe = undefined;
|
|
}
|
|
};
|
|
}
|
|
}
|