integration layer; merge work on indicators from forks with existing ingestion pipeline
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { defer, finalize, Observable, Subscription } from 'rxjs';
|
||||
|
||||
// Shared by availability and KPI data; figure requests retain their existing lifecycle.
|
||||
@Injectable({ providedIn: 'root' })
|
||||
export class IndicadoresRequests {
|
||||
private active = 0;
|
||||
private readonly queue: (() => void)[] = [];
|
||||
run<T>(request: () => Observable<T>): Observable<T> {
|
||||
return new Observable<T>(subscriber => {
|
||||
let started = false;
|
||||
let subscription: Subscription | undefined;
|
||||
const start = () => {
|
||||
started = true;
|
||||
this.active++;
|
||||
subscription = defer(request).pipe(finalize(() => {
|
||||
this.active--;
|
||||
this.drain();
|
||||
})).subscribe(subscriber);
|
||||
};
|
||||
if (this.active < 4) start();
|
||||
else this.queue.push(start);
|
||||
return () => {
|
||||
if (!started) {
|
||||
const index = this.queue.indexOf(start);
|
||||
if (index >= 0) this.queue.splice(index, 1);
|
||||
}
|
||||
subscription?.unsubscribe();
|
||||
};
|
||||
});
|
||||
}
|
||||
private drain() {
|
||||
while (this.active < 4 && this.queue.length) this.queue.shift()!();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user