2024-06-10 18:55:12 +02:00
|
|
|
import { firstValueFrom, Observable, Subject, Subscription, throwError, timeout } from "rxjs";
|
2024-03-27 18:03:09 +01:00
|
|
|
|
|
|
|
/** Test class to enable async awaiting of observable emissions */
|
|
|
|
export class ObservableTracker<T> {
|
|
|
|
private subscription: Subscription;
|
2024-06-05 00:42:04 +02:00
|
|
|
private emissionReceived = new Subject<T>();
|
2024-03-27 18:03:09 +01:00
|
|
|
emissions: T[] = [];
|
2024-06-05 00:42:04 +02:00
|
|
|
constructor(observable: Observable<T>) {
|
2024-03-27 18:03:09 +01:00
|
|
|
this.emissions = this.trackEmissions(observable);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Unsubscribes from the observable */
|
|
|
|
unsubscribe() {
|
|
|
|
this.subscription.unsubscribe();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Awaits the next emission from the observable, or throws if the timeout is exceeded
|
|
|
|
* @param msTimeout The maximum time to wait for another emission before throwing
|
2024-04-26 21:08:39 +02:00
|
|
|
* @returns The next emission from the observable
|
|
|
|
* @throws If the timeout is exceeded
|
2024-03-27 18:03:09 +01:00
|
|
|
*/
|
2024-04-26 21:08:39 +02:00
|
|
|
async expectEmission(msTimeout = 50): Promise<T> {
|
|
|
|
return await firstValueFrom(
|
2024-06-05 00:42:04 +02:00
|
|
|
this.emissionReceived.pipe(
|
2024-03-27 18:03:09 +01:00
|
|
|
timeout({
|
|
|
|
first: msTimeout,
|
|
|
|
with: () => throwError(() => new Error("Timeout exceeded waiting for another emission.")),
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-04-27 17:15:27 +02:00
|
|
|
/** Awaits until the total number of emissions observed by this tracker equals or exceeds {@link count}
|
2024-03-27 18:03:09 +01:00
|
|
|
* @param count The number of emissions to wait for
|
|
|
|
*/
|
|
|
|
async pauseUntilReceived(count: number, msTimeout = 50): Promise<T[]> {
|
2024-06-05 00:42:04 +02:00
|
|
|
while (this.emissions.length < count) {
|
2024-03-27 18:03:09 +01:00
|
|
|
await this.expectEmission(msTimeout);
|
|
|
|
}
|
|
|
|
return this.emissions;
|
|
|
|
}
|
|
|
|
|
2024-06-05 00:42:04 +02:00
|
|
|
private trackEmissions(observable: Observable<T>): T[] {
|
2024-03-27 18:03:09 +01:00
|
|
|
const emissions: T[] = [];
|
2024-06-10 18:55:12 +02:00
|
|
|
this.emissionReceived.subscribe((value) => {
|
|
|
|
emissions.push(value);
|
|
|
|
});
|
2024-03-27 18:03:09 +01:00
|
|
|
this.subscription = observable.subscribe((value) => {
|
2024-06-05 00:42:04 +02:00
|
|
|
if (value == null) {
|
|
|
|
this.emissionReceived.next(null);
|
|
|
|
return;
|
2024-03-27 18:03:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (typeof value) {
|
|
|
|
case "string":
|
|
|
|
case "number":
|
|
|
|
case "boolean":
|
2024-06-05 00:42:04 +02:00
|
|
|
this.emissionReceived.next(value);
|
2024-03-27 18:03:09 +01:00
|
|
|
break;
|
|
|
|
case "symbol":
|
|
|
|
// Cheating types to make symbols work at all
|
2024-06-05 00:42:04 +02:00
|
|
|
this.emissionReceived.next(value as T);
|
2024-03-27 18:03:09 +01:00
|
|
|
break;
|
|
|
|
default: {
|
2024-06-05 00:42:04 +02:00
|
|
|
this.emissionReceived.next(clone(value));
|
2024-03-27 18:03:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2024-06-10 18:55:12 +02:00
|
|
|
|
2024-03-27 18:03:09 +01:00
|
|
|
return emissions;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function clone(value: any): any {
|
|
|
|
if (global.structuredClone != undefined) {
|
|
|
|
return structuredClone(value);
|
|
|
|
} else {
|
|
|
|
return JSON.parse(JSON.stringify(value));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** A test helper that builds an @see{@link ObservableTracker}, which can be used to assert things about the
|
|
|
|
* emissions of the given observable
|
|
|
|
* @param observable The observable to track
|
|
|
|
*/
|
|
|
|
export function subscribeTo<T>(observable: Observable<T>) {
|
|
|
|
return new ObservableTracker(observable);
|
|
|
|
}
|