bitwarden-estensione-browser/libs/common/src/platform/services/app-id.service.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

47 lines
1.5 KiB
TypeScript
Raw Normal View History

import { Observable, filter, firstValueFrom, tap } from "rxjs";
import { AppIdService as AppIdServiceAbstraction } from "../abstractions/app-id.service";
2022-02-22 15:39:11 +01:00
import { Utils } from "../misc/utils";
import { APPLICATION_ID_DISK, GlobalStateProvider, KeyDefinition } from "../state";
export const APP_ID_KEY = new KeyDefinition(APPLICATION_ID_DISK, "appId", {
deserializer: (value: string) => value,
});
export const ANONYMOUS_APP_ID_KEY = new KeyDefinition(APPLICATION_ID_DISK, "anonymousAppId", {
deserializer: (value: string) => value,
});
2018-01-09 22:19:55 +01:00
2018-04-04 14:22:18 +02:00
export class AppIdService implements AppIdServiceAbstraction {
appId$: Observable<string>;
anonymousAppId$: Observable<string>;
2018-01-09 22:19:55 +01:00
constructor(globalStateProvider: GlobalStateProvider) {
const appIdState = globalStateProvider.get(APP_ID_KEY);
const anonymousAppIdState = globalStateProvider.get(ANONYMOUS_APP_ID_KEY);
this.appId$ = appIdState.state$.pipe(
tap(async (appId) => {
if (!appId) {
await appIdState.update(() => Utils.newGuid());
}
}),
filter((appId) => !!appId),
);
this.anonymousAppId$ = anonymousAppIdState.state$.pipe(
tap(async (appId) => {
if (!appId) {
await anonymousAppIdState.update(() => Utils.newGuid());
}
}),
filter((appId) => !!appId),
);
2018-01-09 22:19:55 +01:00
}
async getAppId(): Promise<string> {
return await firstValueFrom(this.appId$);
2018-01-09 22:19:55 +01:00
}
async getAnonymousAppId(): Promise<string> {
return await firstValueFrom(this.anonymousAppId$);
2021-12-16 13:36:21 +01:00
}
2018-01-09 22:19:55 +01:00
}