Skip to content

Commit

Permalink
🚀 Improved performance of location acquisition
Browse files Browse the repository at this point in the history
  • Loading branch information
jolzee committed Mar 27, 2020
1 parent c530028 commit 460dffc
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 32 deletions.
8 changes: 8 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
"smoothscroll-polyfill": "^0.4.4",
"striptags": "^3.1.1",
"superagent": "^5.2.2",
"ttl-localstorage": "^1.5.2",
"url-parse": "^1.4.7",
"url-regex": "^5.0.0",
"uuid": "^7.0.2",
Expand Down
92 changes: 60 additions & 32 deletions src/utils/setup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const logger = require("@/utils/logging").getLogger("setup.js");
import "@mdi/font/css/materialdesignicons.css";
import { LocalStorage } from "ttl-localstorage";

// import "typeface-roboto";
import {
Expand Down Expand Up @@ -250,38 +251,26 @@ export default class Setup {
}
}, 2000);
if (window.leopardConfig.mustSendLocationAtLogin) {
superagent
.get("https://api.ipify.org")
.then(res => {
return superagent
.get(
`https://cors-anywhere.herokuapp.com/http://www.geoplugin.net/json.gp?ip=${res.text}`
)
.accept("application/json");
})
.then(res => {
const loc = JSON.parse(res.text);
logger.debug(`Location Information`, loc);
this.LOCATION = {
city: loc.geoplugin_city,
continentCode: loc.geoplugin_continentCode,
continentName: loc.geoplugin_continentName,
countryCode: loc.geoplugin_countryCode,
countryName: loc.geoplugin_countryName,
currencySymbol: loc.geoplugin_currencySymbol,
currencyCode: loc.geoplugin_currencyCode,
latitude: loc.geoplugin_latitude,
longitude: loc.geoplugin_longitude,
regionCode: loc.geoplugin_regionCode,
regionName: loc.geoplugin_regionName
};
resolve(vuetify);
})
.catch(err => {
this.LOCATION = "";
logger.error(`Unable to obtain location info`, err);
resolve(vuetify);
});
LocalStorage.keyExists(STORAGE_KEY + "loc").then(exists => {
if (exists) {
LocalStorage.get(STORAGE_KEY + "loc").then(data => {
superagent.get("https://api.ipify.org").then(res => {
if (res.text === data.ip) {
logger.debug(
`📍 Found Location Info in LocalStorage. IP hasn't changed`,
data
);
this.LOCATION = data;
resolve(vuetify);
} else {
this.obtainLocation(resolve, vuetify);
}
});
});
} else {
this.obtainLocation(resolve, vuetify);
}
});
} else {
resolve(vuetify);
}
Expand All @@ -293,6 +282,45 @@ export default class Setup {
});
}

obtainLocation(resolve, vuetify) {
let browserIp;
superagent
.get("https://api.ipify.org")
.then(res => {
browserIp = res.text;
return superagent
.get(
`https://cors-anywhere.herokuapp.com/http://www.geoplugin.net/json.gp?ip=${res.text}`
)
.accept("application/json");
})
.then(res => {
const loc = JSON.parse(res.text);
logger.debug(`📍 Obtained New Location Information`, loc);
this.LOCATION = {
ip: browserIp,
city: loc.geoplugin_city,
continentCode: loc.geoplugin_continentCode,
continentName: loc.geoplugin_continentName,
countryCode: loc.geoplugin_countryCode,
countryName: loc.geoplugin_countryName,
currencySymbol: loc.geoplugin_currencySymbol,
currencyCode: loc.geoplugin_currencyCode,
latitude: loc.geoplugin_latitude,
longitude: loc.geoplugin_longitude,
regionCode: loc.geoplugin_regionCode,
regionName: loc.geoplugin_regionName
};
LocalStorage.put(STORAGE_KEY + "loc", this.LOCATION); // cache so we don't do this too often
resolve(vuetify);
})
.catch(err => {
this.LOCATION = "";
logger.error(`📍 Unable to obtain location info`, err);
resolve(vuetify);
});
}

getSolutionConfig() {
logger.debug("Begin looking for Solution Config ");
return new Promise((resolve, reject) => {
Expand Down

0 comments on commit 460dffc

Please sign in to comment.