Skip to content

Commit

Permalink
rewrite+move getToolboxDevs
Browse files Browse the repository at this point in the history
Since TBApi is imported now, we don't have to setTimeout when calling this function anymore, yay
  • Loading branch information
eritbh committed Apr 24, 2021
1 parent 30f85ab commit fcf1322
Showing 1 changed file with 35 additions and 50 deletions.
85 changes: 35 additions & 50 deletions extension/data/tbcore.js
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,39 @@ async function fetchUserDetails (tries = 3) {
}
}

/**
* Fetches the list of Toolbox developers from the /r/toolbox mod list.
* @returns {Promise<void>}
*/
async function getToolboxDevs () {
let devs;
try {
// Fetch the /r/toolbox mod list
const resp = await TBApi.getJSON('/r/toolbox/about/moderators.json');
TBStorage.purifyObject(resp);
devs = resp.data.children.map(child => child.name).filter(dev => dev !== 'AutoModerator');
} catch (_) {
// Something went wrong, use a hardcoded fallback list
devs = [
'agentlame',
'creesch',
'LowSociety ',
'TheEnigmaBlade',
'dakta',
'largenocream',
'psdtwk',
'amici_ursi',
'noeatnosleep',
'Garethp',
'WorseThanHipster',
'eritbh',
];
}

window.TBCore.tbDevs = devs;
await TBStorage.setSettingAsync('Utils', 'tbDevs', devs);
}

let userDetails;
let cacheDetails;
let newModSubs;
Expand Down Expand Up @@ -792,11 +825,7 @@ let newModSubs;
});

if (!toolboxDevs || toolboxDevs.length < 1) {
// TODO: getToolboxDevs relies on TBApi.getJSON, which is only set
// after this code gets called. So, we use setTimeout to queue
// the call and execute it after the methods we need are all
// defined.
setTimeout(getToolboxDevs, 0);
getToolboxDevs();
}

// Extra checks on old faults
Expand Down Expand Up @@ -824,7 +853,7 @@ let newModSubs;
// These need to happen for every version change
TBCore.firstRun = true; // for use by other modules.
TBStorage.setSetting(SETTINGS_NAME, 'lastVersion', shortVersion); // set last version to this version.
setTimeout(getToolboxDevs, 0); // always repopulate tb devs for each version change
getToolboxDevs(); // always repopulate tb devs for each version change

//* * This should be a per-release section of stuff we want to change in each update. Like setting/converting data/etc. It should always be removed before the next release. **//

Expand Down Expand Up @@ -1011,15 +1040,6 @@ let newModSubs;
return debugObject;
};

/**
* Fetches the toolbox dev from /r/toolbox or falls back to a predefined list.
* @function
* @returns {array} List of toolbox devs
*/
TBCore.getToolboxDevs = function getToolboxDevs () {
getToolboxDevs();
};

TBCore.sendEvent = function (tbuEvent) {
logger.log('Sending event:', tbuEvent);
window.dispatchEvent(new CustomEvent(tbuEvent));
Expand Down Expand Up @@ -1580,41 +1600,6 @@ let newModSubs;
});
}

function getToolboxDevs () {
TBApi.getJSON('/r/toolbox/about/moderators.json').then(resp => {
TBStorage.purifyObject(resp);
const children = resp.data.children,
devs = [];

children.forEach(child => {
// AutoModerator seems to add itself to /r/toolbox sometimes, but it's definitely not a developer
if (child.name === 'AutoModerator') {
return;
}
devs.push(child.name);
});
TBCore.tbDevs = devs;
TBStorage.setSetting(SETTINGS_NAME, 'tbDevs', devs);
}).catch(() => {
const devs = [
'agentlame',
'creesch',
'LowSociety ',
'TheEnigmaBlade',
'dakta',
'largenocream',
'psdtwk',
'amici_ursi',
'noeatnosleep',
'Garethp',
'WorseThanHipster',
'eritbh',
];
TBCore.tbDevs = devs;
TBStorage.setSetting(SETTINGS_NAME, 'tbDevs', devs);
});
}

// Watch for locationHref changes and sent an event with details
let locationHref;
let locationHash;
Expand Down

0 comments on commit fcf1322

Please sign in to comment.