mirror of
https://github.com/bitwarden/browser.git
synced 2024-12-19 15:57:42 +01:00
9c1e2ebd67
* Use typescript-strict-plugin to iteratively turn on strict * Add strict testing to pipeline Can be executed locally through either `npm run test:types` for full type checking including spec files, or `npx tsc-strict` for only tsconfig.json included files. * turn on strict for scripts directory * Use plugin for all tsconfigs in monorepo vscode is capable of executing tsc with plugins, but uses the most relevant tsconfig to do so. If the plugin is not a part of that config, it is skipped and developers get no feedback of strict compile time issues. These updates remedy that at the cost of slightly more complex removal of the plugin when the time comes. * remove plugin from configs that extend one that already has it * Update workspace settings to honor strict plugin * Apply strict-plugin to native message test runner * Update vscode workspace to use root tsc version * `./node_modules/.bin/update-strict-comments` 🤖 This is a one-time operation. All future files should adhere to strict type checking. * Add fixme to `ts-strict-ignore` comments * `update-strict-comments` 🤖 repeated for new merge files
91 lines
2.7 KiB
TypeScript
91 lines
2.7 KiB
TypeScript
// FIXME: Update this file to be type safe and remove this and next line
|
|
// @ts-strict-ignore
|
|
import { firstValueFrom, Observable, Subject, Subscription, throwError, timeout } from "rxjs";
|
|
|
|
/** Test class to enable async awaiting of observable emissions */
|
|
export class ObservableTracker<T> {
|
|
private subscription: Subscription;
|
|
private emissionReceived = new Subject<T>();
|
|
emissions: T[] = [];
|
|
constructor(observable: Observable<T>) {
|
|
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
|
|
* @returns The next emission from the observable
|
|
* @throws If the timeout is exceeded
|
|
*/
|
|
async expectEmission(msTimeout = 50): Promise<T> {
|
|
return await firstValueFrom(
|
|
this.emissionReceived.pipe(
|
|
timeout({
|
|
first: msTimeout,
|
|
with: () => throwError(() => new Error("Timeout exceeded waiting for another emission.")),
|
|
}),
|
|
),
|
|
);
|
|
}
|
|
|
|
/** Awaits until the total number of emissions observed by this tracker equals or exceeds {@link count}
|
|
* @param count The number of emissions to wait for
|
|
*/
|
|
async pauseUntilReceived(count: number, msTimeout = 50): Promise<T[]> {
|
|
while (this.emissions.length < count) {
|
|
await this.expectEmission(msTimeout);
|
|
}
|
|
return this.emissions;
|
|
}
|
|
|
|
private trackEmissions(observable: Observable<T>): T[] {
|
|
const emissions: T[] = [];
|
|
this.emissionReceived.subscribe((value) => {
|
|
emissions.push(value);
|
|
});
|
|
this.subscription = observable.subscribe((value) => {
|
|
if (value == null) {
|
|
this.emissionReceived.next(null);
|
|
return;
|
|
}
|
|
|
|
switch (typeof value) {
|
|
case "string":
|
|
case "number":
|
|
case "boolean":
|
|
this.emissionReceived.next(value);
|
|
break;
|
|
case "symbol":
|
|
// Cheating types to make symbols work at all
|
|
this.emissionReceived.next(value as T);
|
|
break;
|
|
default: {
|
|
this.emissionReceived.next(clone(value));
|
|
}
|
|
}
|
|
});
|
|
|
|
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);
|
|
}
|