Created
June 16, 2020 16:46
-
-
Save Streeterxs/807ddc3cad0d21435cc4ba014402d558 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { asyncStorageModule } from "../asyncStorage"; | |
import { getUniqueId } from "react-native-device-info"; | |
class Device { | |
private _device: string = ''; | |
private _isFetching = false; | |
private static _instance: Device; | |
private _deviceAsyncStorageModule = asyncStorageModule<string>('device'); | |
private constructor() {} | |
static get instance() { | |
if(!this._instance) { | |
this._instance = new Device(); | |
} | |
return this._instance; | |
} | |
async getDevice() { | |
if (!this._device && !this._isFetching) { | |
this._isFetching = true; | |
let deviceReturn = await this._deviceAsyncStorageModule.getValue(); | |
if (!deviceReturn) { | |
deviceReturn = getUniqueId(); | |
await this._deviceAsyncStorageModule.setValue(deviceReturn); | |
} | |
this._device = deviceReturn; | |
} | |
return this._device | |
} | |
}; | |
export default Device.instance; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment