Skip to content

Instantly share code, notes, and snippets.

@Streeterxs
Created June 16, 2020 16:46
Show Gist options
  • Save Streeterxs/807ddc3cad0d21435cc4ba014402d558 to your computer and use it in GitHub Desktop.
Save Streeterxs/807ddc3cad0d21435cc4ba014402d558 to your computer and use it in GitHub Desktop.
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