Persist device ID

Summary: Linked to https://github.com/facebook/flipper/issues/3319

Reviewed By: passy

Differential Revision: D36786952

fbshipit-source-id: f3214f35039845a8e35fa14e63f509a6fbdddb1f
This commit is contained in:
Andrey Goncharov
2022-06-01 02:49:25 -07:00
committed by Facebook GitHub Bot
parent ee64216725
commit 055b14c6dd
5 changed files with 40 additions and 6 deletions

View File

@@ -7,6 +7,8 @@
* @format
*/
import {DEVICE_ID_STORAGE_KEY} from './consts';
// https://github.com/microsoft/TypeScript/issues/36931#issuecomment-846131999
type Assert = (condition: unknown) => asserts condition;
export const assert: Assert = (condition) => {
@@ -66,3 +68,22 @@ export const detectDevice = (): string => {
}
return require('os').release();
};
export const getDeviceId = () => {
// localStorage is not defined in Node.js env
const persistedId =
typeof localStorage === 'object'
? localStorage?.getItem(DEVICE_ID_STORAGE_KEY)
: undefined;
if (persistedId) {
return persistedId;
}
const newId = `${Date.now()}.${Math.random()}`;
if (typeof localStorage === 'object') {
localStorage?.setItem(DEVICE_ID_STORAGE_KEY, newId);
}
return newId;
};