Files
pixel.horse/src/ts/components/services/intervalUpdateService.ts
T
2019-08-30 01:52:40 -07:00

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;
}
};
}
}