Archive commit

This commit is contained in:
Erik McClure
2019-08-28 21:15:02 -07:00
commit 735845746e
1435 changed files with 140724 additions and 0 deletions
@@ -0,0 +1,44 @@
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;
}
};
}
}