mirror of
https://github.com/bitwarden/browser
synced 2025-12-16 08:13:42 +00:00
Add UserKeyDefinition to union (#8298)
This commit is contained in:
@@ -25,6 +25,7 @@ import {
|
|||||||
FakeGlobalState,
|
FakeGlobalState,
|
||||||
FakeSingleUserState,
|
FakeSingleUserState,
|
||||||
} from "./fake-state";
|
} from "./fake-state";
|
||||||
|
import { isUserKeyDefinition } from "../src/platform/state/user-key-definition";
|
||||||
|
|
||||||
export class FakeGlobalStateProvider implements GlobalStateProvider {
|
export class FakeGlobalStateProvider implements GlobalStateProvider {
|
||||||
mock = mock<GlobalStateProvider>();
|
mock = mock<GlobalStateProvider>();
|
||||||
@@ -95,7 +96,10 @@ export class FakeSingleUserStateProvider implements SingleUserStateProvider {
|
|||||||
return result as SingleUserState<T>;
|
return result as SingleUserState<T>;
|
||||||
}
|
}
|
||||||
|
|
||||||
getFake<T>(userId: UserId, keyDefinition: KeyDefinition<T>): FakeSingleUserState<T> {
|
getFake<T>(
|
||||||
|
userId: UserId,
|
||||||
|
keyDefinition: KeyDefinition<T> | UserKeyDefinition<T>,
|
||||||
|
): FakeSingleUserState<T> {
|
||||||
return this.get(userId, keyDefinition) as FakeSingleUserState<T>;
|
return this.get(userId, keyDefinition) as FakeSingleUserState<T>;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -137,7 +141,7 @@ export class FakeActiveUserStateProvider implements ActiveUserStateProvider {
|
|||||||
return result as ActiveUserState<T>;
|
return result as ActiveUserState<T>;
|
||||||
}
|
}
|
||||||
|
|
||||||
getFake<T>(keyDefinition: KeyDefinition<T>): FakeActiveUserState<T> {
|
getFake<T>(keyDefinition: KeyDefinition<T> | UserKeyDefinition<T>): FakeActiveUserState<T> {
|
||||||
return this.get(keyDefinition) as FakeActiveUserState<T>;
|
return this.get(keyDefinition) as FakeActiveUserState<T>;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -154,8 +158,15 @@ export class FakeActiveUserStateProvider implements ActiveUserStateProvider {
|
|||||||
|
|
||||||
export class FakeStateProvider implements StateProvider {
|
export class FakeStateProvider implements StateProvider {
|
||||||
mock = mock<StateProvider>();
|
mock = mock<StateProvider>();
|
||||||
getUserState$<T>(keyDefinition: KeyDefinition<T>, userId?: UserId): Observable<T> {
|
getUserState$<T>(
|
||||||
|
keyDefinition: KeyDefinition<T> | UserKeyDefinition<T>,
|
||||||
|
userId?: UserId,
|
||||||
|
): Observable<T> {
|
||||||
|
if (isUserKeyDefinition(keyDefinition)) {
|
||||||
this.mock.getUserState$(keyDefinition, userId);
|
this.mock.getUserState$(keyDefinition, userId);
|
||||||
|
} else {
|
||||||
|
this.mock.getUserState$(keyDefinition, userId);
|
||||||
|
}
|
||||||
if (userId) {
|
if (userId) {
|
||||||
return this.getUser<T>(userId, keyDefinition).state$;
|
return this.getUser<T>(userId, keyDefinition).state$;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,8 +18,22 @@ import { ActiveUserStateProvider, SingleUserStateProvider } from "./user-state.p
|
|||||||
* and {@link GlobalStateProvider}.
|
* and {@link GlobalStateProvider}.
|
||||||
*/
|
*/
|
||||||
export abstract class StateProvider {
|
export abstract class StateProvider {
|
||||||
/** @see{@link ActiveUserState.activeUserId$} */
|
/** @see{@link ActiveUserStateProvider.activeUserId$} */
|
||||||
activeUserId$: Observable<UserId | undefined>;
|
activeUserId$: Observable<UserId | undefined>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets a state observable for a given key and userId.
|
||||||
|
*
|
||||||
|
* @remarks If userId is falsy the observable returned will point to the currently active user _and not update if the active user changes_.
|
||||||
|
* This is different to how `getActive` works and more similar to `getUser` for whatever user happens to be active at the time of the call.
|
||||||
|
*
|
||||||
|
* @note consider converting your {@link KeyDefinition} to a {@link UserKeyDefinition} for additional features.
|
||||||
|
*
|
||||||
|
* @param keyDefinition - The key definition for the state you want to get.
|
||||||
|
* @param userId - The userId for which you want the state for. If not provided, the state for the currently active user will be returned.
|
||||||
|
*/
|
||||||
|
abstract getUserState$<T>(keyDefinition: KeyDefinition<T>, userId?: UserId): Observable<T>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets a state observable for a given key and userId.
|
* Gets a state observable for a given key and userId.
|
||||||
*
|
*
|
||||||
@@ -29,7 +43,7 @@ export abstract class StateProvider {
|
|||||||
* @param keyDefinition - The key definition for the state you want to get.
|
* @param keyDefinition - The key definition for the state you want to get.
|
||||||
* @param userId - The userId for which you want the state for. If not provided, the state for the currently active user will be returned.
|
* @param userId - The userId for which you want the state for. If not provided, the state for the currently active user will be returned.
|
||||||
*/
|
*/
|
||||||
getUserState$: <T>(keyDefinition: KeyDefinition<T>, userId?: UserId) => Observable<T>;
|
abstract getUserState$<T>(keyDefinition: UserKeyDefinition<T>, userId?: UserId): Observable<T>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the state for a given key and userId.
|
* Sets the state for a given key and userId.
|
||||||
|
|||||||
Reference in New Issue
Block a user