Skip to content

Commit

Permalink
refactor: More logging in the ApiClient to try and solve the initial lag
Browse files Browse the repository at this point in the history
  • Loading branch information
itssimple committed Oct 16, 2021
1 parent 168504a commit cbbb424
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/scripts/destiny2/apiClient.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
function DestinyApiClient(d2ApiClient) {
log("D2API", "Initializing Destiny Api Client");
const apiToken = "c32cd3cb4eb94a84acc468a1cf333dac";

const pluginClient = d2ApiClient;
Expand Down Expand Up @@ -67,6 +68,7 @@ function DestinyApiClient(d2ApiClient) {
this.trackedGoals = [];

this.checkManifestVersion = async function () {
log("D2API", "Checking manifest version");
return new Promise(async function (resolve, reject) {
let manifest = await self.getManifest();
let lastVersion = (await db.getItem("manifestVersion")) ?? "null";
Expand All @@ -89,23 +91,27 @@ function DestinyApiClient(d2ApiClient) {
await self.loadDestinyContentData();

resolve({ updatedManifest: true, version: self.lastVersion });
log("D2API", "Manifest updated");
return;
}

await checkStoredDefinitions();

resolve({ updatedManifest: false, version: self.lastVersion });
log("D2API", "Manifest version is up to date");
});
};

this.getManifest = async function () {
let lastManifestUpdate = await db.getItem("lastManifestUpdate");
log("D2API", "Checking if manifest is cached");

if (
lastManifestUpdate !== null &&
Date.now() - lastManifestUpdate < 60000 * 60
) {
if ((await db.getItem("manifest")) !== null) {
log("D2API", "Manifest is cached");
return { Response: JSON.parse(await db.getItem("manifest")) };
}
}
Expand All @@ -122,6 +128,7 @@ function DestinyApiClient(d2ApiClient) {
if (manifest.ErrorStatus == "Success") {
db.setItem("lastManifestUpdate", Date.now());
db.setItem("manifest", JSON.stringify(manifest.Response));
log("D2API", "Manifest updated");
resolve(manifest);
} else {
reject(manifest);
Expand All @@ -140,6 +147,7 @@ function DestinyApiClient(d2ApiClient) {
};

async function loadDataFromStorage() {
log("D2API", "Loading data from storage");
let _cachedManifest = await db.getItem("manifest");
if (_cachedManifest !== null) {
self.cachedManifest = JSON.parse(_cachedManifest);
Expand Down Expand Up @@ -168,6 +176,7 @@ function DestinyApiClient(d2ApiClient) {
);
}

log("D2API", "Data loaded from storage");
eventEmitter.emit("destiny-data-loaded");
}

Expand Down Expand Up @@ -246,12 +255,6 @@ function DestinyApiClient(d2ApiClient) {
});
}

function convertObjectToArray(object) {
return Object.keys(object).map((key) => {
return object[key];
});
}

/**
* @description Gets a primed XHR client, with the correct headers.
*/
Expand Down Expand Up @@ -946,5 +949,7 @@ function DestinyApiClient(d2ApiClient) {

this.goalApi = new Destiny2Goals();

log("D2API", "Destiny Api Client initialized");

return this;
}

0 comments on commit cbbb424

Please sign in to comment.