mirror of
https://github.com/bitwarden/browser
synced 2025-12-06 00:13:28 +00:00
[PM-17408] Create new method on sdk service to allow explicit addition of a new client instance (#13309)
* feat: allow the user client to be overriden by an external provider * feat: add ability to unset client * feat: add `setClient` to interface (and add some docs) * fix: re-add undefined * fix: strict typing issues
This commit is contained in:
@@ -1,13 +1,33 @@
|
||||
// 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";
|
||||
import {
|
||||
filter,
|
||||
firstValueFrom,
|
||||
lastValueFrom,
|
||||
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>) {
|
||||
|
||||
/**
|
||||
* Creates a new ObservableTracker and instantly subscribes to the given observable.
|
||||
* @param observable The observable to track
|
||||
* @param clone Whether to clone tracked emissions or not, defaults to true.
|
||||
* Cloning can be necessary if the observable emits objects that are mutated after emission. Cloning makes it
|
||||
* harder to compare the original and the tracked emission using reference equality (e.g. `expect().toBe()`).
|
||||
*/
|
||||
constructor(
|
||||
observable: Observable<T>,
|
||||
private clone = true,
|
||||
) {
|
||||
this.emissions = this.trackEmissions(observable);
|
||||
}
|
||||
|
||||
@@ -33,6 +53,19 @@ export class ObservableTracker<T> {
|
||||
);
|
||||
}
|
||||
|
||||
async expectCompletion(msTimeout = 50): Promise<void> {
|
||||
return await lastValueFrom(
|
||||
this.emissionReceived.pipe(
|
||||
filter(() => false),
|
||||
timeout({
|
||||
first: msTimeout,
|
||||
with: () => throwError(() => new Error("Timeout exceeded waiting for completion.")),
|
||||
}),
|
||||
),
|
||||
{ defaultValue: undefined },
|
||||
);
|
||||
}
|
||||
|
||||
/** 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
|
||||
*/
|
||||
@@ -48,26 +81,31 @@ export class ObservableTracker<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));
|
||||
this.subscription = observable.subscribe({
|
||||
next: (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(this.clone ? clone(value) : value);
|
||||
}
|
||||
}
|
||||
},
|
||||
complete: () => {
|
||||
this.emissionReceived.complete();
|
||||
},
|
||||
});
|
||||
|
||||
return emissions;
|
||||
|
||||
Reference in New Issue
Block a user