mirror of
https://github.com/bitwarden/browser
synced 2025-12-14 15:23:33 +00:00
* Rename service-factory folder * Move cryptographic service factories * Move crypto models * Move crypto services * Move domain base class * Platform code owners * Move desktop log services * Move log files * Establish component library ownership * Move background listeners * Move background background * Move localization to Platform * Move browser alarms to Platform * Move browser state to Platform * Move CLI state to Platform * Move Desktop native concerns to Platform * Move flag and misc to Platform * Lint fixes * Move electron state to platform * Move web state to Platform * Move lib state to Platform * Fix broken tests * Rename interface to idiomatic TS * `npm run prettier` 🤖 * Resolve review feedback * Set platform as owners of web core and shared * Expand moved services * Fix test types --------- Co-authored-by: Hinton <hinton@users.noreply.github.com>
48 lines
1.4 KiB
TypeScript
48 lines
1.4 KiB
TypeScript
import { Jsonify } from "type-fest";
|
|
|
|
import { Account } from "./account";
|
|
import { GlobalState } from "./global-state";
|
|
|
|
export class State<
|
|
TGlobalState extends GlobalState = GlobalState,
|
|
TAccount extends Account = Account
|
|
> {
|
|
accounts: { [userId: string]: TAccount } = {};
|
|
globals: TGlobalState;
|
|
activeUserId: string;
|
|
authenticatedAccounts: string[] = [];
|
|
accountActivity: { [userId: string]: number } = {};
|
|
|
|
constructor(globals: TGlobalState) {
|
|
this.globals = globals;
|
|
}
|
|
|
|
// TODO, make Jsonify<State,TGlobalState,TAccount> work. It currently doesn't because Globals doesn't implement Jsonify.
|
|
static fromJSON<TGlobalState extends GlobalState, TAccount extends Account>(
|
|
obj: any,
|
|
accountDeserializer: (json: Jsonify<TAccount>) => TAccount
|
|
): State<TGlobalState, TAccount> {
|
|
if (obj == null) {
|
|
return null;
|
|
}
|
|
|
|
return Object.assign(new State(null), obj, {
|
|
accounts: State.buildAccountMapFromJSON(obj?.accounts, accountDeserializer),
|
|
});
|
|
}
|
|
|
|
private static buildAccountMapFromJSON<TAccount extends Account>(
|
|
jsonAccounts: { [userId: string]: Jsonify<TAccount> },
|
|
accountDeserializer: (json: Jsonify<TAccount>) => TAccount
|
|
) {
|
|
if (!jsonAccounts) {
|
|
return {};
|
|
}
|
|
const accounts: { [userId: string]: TAccount } = {};
|
|
for (const userId in jsonAccounts) {
|
|
accounts[userId] = accountDeserializer(jsonAccounts[userId]);
|
|
}
|
|
return accounts;
|
|
}
|
|
}
|