mirror of
https://github.com/bitwarden/browser
synced 2026-02-10 21:50:15 +00:00
Prefer return value to parameter mutation
This also solves issues where JS deals with ref updates weird due to cryptic variable capture rules. Fixes: replace, clear methods
This commit is contained in:
@@ -6,7 +6,12 @@ import { DerivedStateDefinition } from "../types/derived-state-definition";
|
||||
export interface ActiveUserState<T> {
|
||||
readonly state$: Observable<T>;
|
||||
readonly getFromState: () => Promise<T>;
|
||||
readonly update: (configureState: (state: T) => void) => Promise<void>;
|
||||
/**
|
||||
*
|
||||
* @param configureState function that takes the current state and returns the new state
|
||||
* @returns The new state
|
||||
*/
|
||||
readonly update: (configureState: (state: T) => T) => Promise<T>;
|
||||
createDerived: <TTo>(
|
||||
derivedStateDefinition: DerivedStateDefinition<T, TTo>
|
||||
) => DerivedActiveUserState<T, TTo>;
|
||||
|
||||
@@ -90,7 +90,6 @@ class DefaultActiveUserState<T> implements ActiveUserState<T> {
|
||||
);
|
||||
// startWith?
|
||||
this.formattedKey$ = this.accountService.activeAccount$.pipe(
|
||||
tap((user) => console.log("user", user)), // Temp
|
||||
map((account) =>
|
||||
account != null && account.id != null
|
||||
? userKeyBuilder(account.id, this.keyDefinition)
|
||||
@@ -100,7 +99,6 @@ class DefaultActiveUserState<T> implements ActiveUserState<T> {
|
||||
|
||||
const activeAccountData$ = this.formattedKey$.pipe(
|
||||
switchMap(async (key) => {
|
||||
console.log("user emitted: ", key); // temp
|
||||
if (key == null) {
|
||||
return null;
|
||||
}
|
||||
@@ -128,7 +126,7 @@ class DefaultActiveUserState<T> implements ActiveUserState<T> {
|
||||
});
|
||||
}
|
||||
|
||||
async update(configureState: (state: T) => void): Promise<void> {
|
||||
async update(configureState: (state: T) => T): Promise<T> {
|
||||
const key = await this.createKey();
|
||||
if (key == null) {
|
||||
throw new Error("Attempting to active user state, when no user is active.");
|
||||
@@ -137,10 +135,11 @@ class DefaultActiveUserState<T> implements ActiveUserState<T> {
|
||||
? this.stateSubject.getValue()
|
||||
: await this.seedInitial(key);
|
||||
|
||||
configureState(currentState);
|
||||
const newState = configureState(currentState);
|
||||
|
||||
await this.chosenStorageLocation.save(await this.createKey(), currentState);
|
||||
this.stateSubject.next(currentState);
|
||||
await this.chosenStorageLocation.save(await this.createKey(), newState);
|
||||
this.stateSubject.next(newState);
|
||||
return newState;
|
||||
}
|
||||
|
||||
async getFromState(): Promise<T> {
|
||||
|
||||
@@ -106,15 +106,16 @@ export class FolderService implements InternalFolderServiceAbstraction {
|
||||
folders[f.id] = f;
|
||||
});
|
||||
}
|
||||
return folders;
|
||||
});
|
||||
}
|
||||
|
||||
async replace(folders: { [id: string]: FolderData }): Promise<void> {
|
||||
await this.folderState.update((f) => (f = folders));
|
||||
await this.folderState.update((_) => folders);
|
||||
}
|
||||
|
||||
async clear(userId?: string): Promise<any> {
|
||||
await this.folderState.update((f) => (f = null));
|
||||
await this.folderState.update((_) => null);
|
||||
}
|
||||
|
||||
async delete(id: string | string[]): Promise<void> {
|
||||
@@ -123,6 +124,7 @@ export class FolderService implements InternalFolderServiceAbstraction {
|
||||
folderIds.forEach((folderId) => {
|
||||
delete folders[folderId];
|
||||
});
|
||||
return folders;
|
||||
});
|
||||
|
||||
// Items in a deleted folder are re-assigned to "No Folder"
|
||||
|
||||
Reference in New Issue
Block a user