From cdb4c72864178eade545c6597a28762df2071f6f Mon Sep 17 00:00:00 2001 From: Vincent Taverna Date: Wed, 24 Aug 2016 23:33:12 -0400 Subject: [PATCH] finish #128 es7 async and await --- .eslintrc | 4 +- main.development.js | 135 +++++++++++++++++++++++++------------------- package.js | 55 +++++++++--------- package.json | 2 +- 4 files changed, 109 insertions(+), 87 deletions(-) diff --git a/.eslintrc b/.eslintrc index c60bd6a..8c58244 100644 --- a/.eslintrc +++ b/.eslintrc @@ -28,7 +28,9 @@ 'everything-else' ] }], - "no-loops/no-loops": 2 + "no-loops/no-loops": 2, + # https://github.com/eslint/eslint/issues/6274 + "generator-star-spacing": 0 }, "plugins": [ "import", diff --git a/main.development.js b/main.development.js index 36d33a1..5b6d338 100644 --- a/main.development.js +++ b/main.development.js @@ -29,6 +29,10 @@ if (process.env.NODE_ENV === 'development') { require('electron-debug')() // eslint-disable-line global-require } +function sleep(time) { + return new Promise(r => setTimeout(r, time)) +} + function createWindow() { mainWindow = new BrowserWindow({ width: 800, @@ -215,7 +219,7 @@ ipcMain.on('check-and-delete-credentials', () => { } }) -ipcMain.on('pokemon-login', (event, method, username, password) => { +ipcMain.on('pokemon-login', async (event, method, username, password) => { console.log('[+] Attempting to login') let login if (method === 'google') { @@ -224,15 +228,17 @@ ipcMain.on('pokemon-login', (event, method, username, password) => { login = new pogobuf.PTCLogin() } - login.login(username, password).then(token => { + try { + const token = await login.login(username, password) + client.setAuthInfo(method, token) client.init() event.sender.send('pokemon-logged-in') - }).catch(error => { + } catch (error) { console.error(error) showErrorMessage(error.message) - }) + } }) // END OF LOGIN @@ -241,9 +247,12 @@ ipcMain.on('table-did-mount', () => { }) // POKEMON -ipcMain.on('get-player-info', (event) => { +ipcMain.on('get-player-info', async (event) => { console.log('[+] Retrieving player info') - client.getPlayer().then(response => { + + try { + const response = await client.getPlayer() + if (!response.success) { event.returnValue = { success: false @@ -255,7 +264,9 @@ ipcMain.on('get-player-info', (event) => { success: 'true', player_data: response.player_data } - }) + } catch (error) { + console.error(error) + } }) function generateEmptySpecies(candies) { @@ -374,8 +385,10 @@ function parseInventory(inventory) { } } -function getPlayersPokemons(event, sync = 'sync') { - client.getInventory(0).then(inventory => { +async function getPlayersPokemons(event, sync = 'sync') { + try { + const inventory = await client.getInventory(0) + if (!inventory.success) { const payload = { success: false } if (sync !== 'sync') { @@ -393,7 +406,9 @@ function getPlayersPokemons(event, sync = 'sync') { } else { event.sender.send('receive-players-pokemons', payload) } - }) + } catch (error) { + console.error(error) + } } ipcMain.on('get-players-pokemons', (event, sync) => { @@ -401,62 +416,66 @@ ipcMain.on('get-players-pokemons', (event, sync) => { getPlayersPokemons(event, sync) }) -ipcMain.on('power-up-pokemon', (event, id, nickname) => { - client.upgradePokemon(id) - .then(() => { - console.log(`[+] Upgraded Pokemon with id: ${id}`) - const message = `Upgraded ${nickname} succesfully!` - const title = `Power Up ${nickname}` - // TODO parse the response instead of retrieving all the new pokemon - // Requires replacing the main parsing with more functional code - getPlayersPokemons(event, 'async') - showInformationMessage(message, title) - }) - .catch(console.error) +ipcMain.on('power-up-pokemon', async (event, id, nickname) => { + try { + await client.upgradePokemon(id) + + console.log(`[+] Upgraded Pokemon with id: ${id}`) + const message = `Upgraded ${nickname} succesfully!` + const title = `Power Up ${nickname}` + // TODO parse the response instead of retrieving all the new pokemon + // Requires replacing the main parsing with more functional code + getPlayersPokemons(event, 'async') + showInformationMessage(message, title) + } catch (error) { + console.error(error) + } }) -ipcMain.on('transfer-pokemon', (event, pokemon, delay) => { - setTimeout(() => { - // The line below is useful right now for testing - // it'd be even more useful with mock data responses! - // (new Promise((resolve, reject) => { resolve() })) - client.releasePokemon(pokemon.id) - .then(() => { - console.log(`[+] Released Pokemon with id: ${pokemon.id}`) - - event.sender.send('transfer-pokemon-complete', pokemon) - }).catch(console.error) - }, delay) +ipcMain.on('transfer-pokemon', async (event, pokemon, delay) => { + try { + await sleep(delay) + await client.releasePokemon(pokemon.id) + console.log(`[+] Released Pokemon with id: ${pokemon.id}`) + + event.sender.send('transfer-pokemon-complete', pokemon) + } catch (error) { + console.error(error) + } }) -ipcMain.on('evolve-pokemon', (event, pokemon, delay) => { - setTimeout(() => { - // The line below is useful right now for testing - // it'd be even more useful with mock data responses! - // (new Promise((resolve, reject) => { resolve() })) - client.evolvePokemon(pokemon.id) - .then(() => { - console.log(`[+] Evolved Pokemon with id: ${pokemon.id}`) - - event.sender.send('evolve-pokemon-complete', pokemon) - }).catch(console.error) - }, delay) +ipcMain.on('evolve-pokemon', async (event, pokemon, delay) => { + try { + await sleep(delay) + await client.evolvePokemon(pokemon.id) + console.log(`[+] Evolved Pokemon with id: ${pokemon.id}`) + + event.sender.send('evolve-pokemon-complete', pokemon) + } catch (error) { + console.error(error) + } }) -ipcMain.on('favorite-pokemon', (event, id, isFavorite) => { - client.setFavoritePokemon(id, isFavorite) - .then(() => { - getPlayersPokemons(event, 'async') - }).catch(console.error) - console.log(`[+] Pokemon favorite status set to ${isFavorite}`) +ipcMain.on('favorite-pokemon', async (event, id, isFavorite) => { + try { + await client.setFavoritePokemon(id, isFavorite) + + console.log(`[+] Pokemon favorite status set to ${isFavorite}`) + getPlayersPokemons(event, 'async') + } catch (error) { + console.error(error) + } }) -ipcMain.on('rename-pokemon', (event, id, nickname) => { - client.nicknamePokemon(id, nickname) - .then(() => { - console.log(`[+] Pokemon ${id} nicknamed ${nickname}`) +ipcMain.on('rename-pokemon', async (event, id, nickname) => { + try { + await client.nicknamePokemon(id, nickname) - event.sender.send('rename-pokemon-complete', id, nickname) - }).catch(console.error) + console.log(`[+] Pokemon ${id} nicknamed ${nickname}`) + + event.sender.send('rename-pokemon-complete', id, nickname) + } catch (error) { + console.error(error) + } }) // END OF POKEMON diff --git a/package.js b/package.js index 14ed01d..dba6408 100644 --- a/package.js +++ b/package.js @@ -68,35 +68,36 @@ function build(cfg) { }) } -function startPack() { +async function startPack() { console.log('start pack...') - build(electronCfg) - .then(() => build(cfg)) - .then(() => del('release')) - .then(paths => { - if (shouldBuildAll) { - // build for all platforms - const archs = ['ia32', 'x64'] - const platforms = ['linux', 'win32', 'darwin'] - - platforms.forEach(plat => { - archs.forEach(arch => { - pack(plat, arch, log(plat, arch)) - }) - }) - } else if (argv.platform || argv.arch) { - const arch = argv.arch || os.arch() - const platform = argv.platform || os.platform() - - pack(platform, arch, log(platform, arch)) - } else { - // build for current platform only - pack(os.platform(), os.arch(), log(os.platform(), os.arch())) + + try { + await build(electronCfg) + await build(cfg) + const paths = await del('release') + + if (shouldBuildAll) { + // build for all platforms + const archs = ['ia32', 'x64'] + const platforms = ['linux', 'win32', 'darwin'] + + for (const plat of platforms) { // eslint-disable-line + for (const arch of archs) { // eslint-disable-line + await pack(plat, arch, log(plat, arch)) + } } - }) - .catch(err => { - console.error(err) - }) + } else if (argv.platform || argv.arch) { + const arch = argv.arch || os.arch() + const platform = argv.platform || os.platform() + + await pack(platform, arch, log(platform, arch)) + } else { + // build for current platform only + await pack(os.platform(), os.arch(), log(os.platform(), os.arch())) + } + } catch (error) { + console.error(error) + } } function pack(plat, arch, cb) { diff --git a/package.json b/package.json index 4eb3660..7e3500d 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "build-renderer": "cross-env NODE_ENV=production node -r babel-register ./node_modules/webpack/bin/webpack --config webpack.config.production.js --progress --profile --colors", "build": "npm run build-main && npm run build-renderer", "start-hot": "cross-env HOT=1 NODE_ENV=development electron -r babel-register -r babel-polyfill ./main.development.js", - "package": "cross-env NODE_ENV=production node -r babel-register package.js", + "package": "cross-env NODE_ENV=production node -r babel-register -r babel-polyfill package.js", "package-all": "npm run package -- --all", "postinstall": "node node_modules/fbjs-scripts/node/check-dev-engines.js package.json", "dev": "concurrently --kill-others \"npm run hot-server\" \"npm run start-hot\""