Add wait util

This commit is contained in:
Samuel Elliott 2018-04-04 21:53:02 +01:00
parent d4962bb2ab
commit 4d0631ba38
No known key found for this signature in database
GPG Key ID: 8420C7CDE43DC4D6
3 changed files with 9 additions and 4 deletions

View File

@ -34,7 +34,7 @@ export default new class EmoteModule {
const dataPath = Globals.getPath('data');
try {
await this.load(path.join(dataPath));
await this.load(path.join(dataPath, 'emotes.json'));
} catch (err) {
Logger.err('EmoteModule', [`Failed to load emote data. Make sure you've downloaded the emote data and placed it in ${dataPath}:`, err]);
return;
@ -48,11 +48,11 @@ export default new class EmoteModule {
}
async load(dataPath) {
const emotes = await FileUtils.readJsonFromFile(path.join(dataPath, 'emotes.json'));
const emotes = await FileUtils.readJsonFromFile(dataPath);
for (let [index, emote] of emotes.entries()) {
// Pause every 10000 emotes so the window doesn't freeze
if ((index % 10000) === 0)
await new Promise(r => setTimeout(r, 0));
await Utils.wait();
const uri = emote.type === 2 ? 'https://cdn.betterttv.net/emote/:id/1x' : emote.type === 1 ? 'https://cdn.frankerfacez.com/emoticon/:id/1' : 'https://static-cdn.jtvnw.net/emoticons/v1/:id/1.0';
emote.name = emote.id;

View File

@ -88,6 +88,7 @@ export default class PluginApi {
deepfreeze: (...args) => Utils.deepfreeze.apply(Utils, args),
removeFromArray: (...args) => Utils.removeFromArray.apply(Utils, args),
defineSoftGetter: (...args) => Utils.defineSoftGetter.apply(Utils, args),
wait: (...args) => Utils.wait.apply(Utils, args),
until: (...args) => Utils.until.apply(Utils, args)
};
}

View File

@ -32,7 +32,7 @@ export class Utils {
return JSON.parse(jsonString);
} catch (err) {
throw ({
'message': 'Failed to parse json',
message: 'Failed to parse json',
err
});
}
@ -160,6 +160,10 @@ export class Utils {
});
}
static wait(time = 0) {
return new Promise(resolve => setTimeout(resolve, time));
}
static async until(check, time = 0) {
let value, i;
do {