1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-22 03:03:43 +00:00

convert appid service to ts

This commit is contained in:
Kyle Spearrin
2017-11-02 15:51:05 -04:00
parent 8eeb5821f0
commit 5fd850929d
4 changed files with 43 additions and 3 deletions

View File

@@ -0,0 +1,31 @@
import UtilsService from './utils.service';
export default class AppIdService {
static getAppId(): Promise<string> {
return AppIdService.makeAndGetAppId('appId');
}
static getAnonymousAppId(): Promise<string> {
return AppIdService.makeAndGetAppId('anonymousAppId');
}
private static async makeAndGetAppId(key: string) {
const existingId = await UtilsService.getObjFromStorage<string>(key);
if(existingId != null) {
return existingId;
}
const guid = UtilsService.newGuid();
await UtilsService.saveObjToStorage(key, guid);
return guid;
}
// TODO: remove these in favor of static methods
getAppId(): Promise<string> {
return AppIdService.getAppId();
}
getAnonymousAppId(): Promise<string> {
return AppIdService.getAnonymousAppId();
}
}

View File

@@ -8,6 +8,15 @@ const AnalyticsIds = {
};
export default class UtilsService {
// ref: http://stackoverflow.com/a/2117523/1090359
static newGuid(): string {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
const r = Math.random() * 16 | 0;
const v = c === 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
static extendObject(...objects: any[]): any {
for (let i = 1; i < objects.length; i++) {
for (const key in objects[i]) {