From ed3bab0f8e3ddbda736761d50b2ec8b521f894e9 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Thu, 16 Sep 2021 17:08:10 -0400 Subject: [PATCH] Better forge --- utils/GunSmith/GunSmith.js | 41 +++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/utils/GunSmith/GunSmith.js b/utils/GunSmith/GunSmith.js index 81570ec4..11186ce5 100644 --- a/utils/GunSmith/GunSmith.js +++ b/utils/GunSmith/GunSmith.js @@ -240,7 +240,7 @@ const isReady = () => let procID = 0 -const forge = () => { +const forge = async () => { logger.info(`Forging Gun # ${++procID}`) if (isForging) { throw new Error('Double forge?') @@ -274,8 +274,18 @@ const forge = () => { opts: lastOpts, type: 'init' } - currentGun.send(initMsg) - logger.info('Sent init msg') + await new Promise(res => { + currentGun.on('message', msg => { + if (msg.type === 'init') { + // @ts-ignore + res() + } + }) + currentGun.send(initMsg) + logger.info('Sent init msg') + }) + + logger.info('Received init reply') const lastGunListeners = Object.keys(pathToListeners).map(path => { /** @type {Smith.SmithMsgOn} */ @@ -309,17 +319,20 @@ const forge = () => { ) } - if (isReforge) { - logger.info('Finished reforging, will now auto-auth') - autoAuth().then(() => { - isForging = false - flushPendingPuts() - }) - } else { - logger.info('Finished forging, will now auto-auth') - isForging = false - flushPendingPuts() - } + logger.info( + isReforge + ? 'Finished reforging, will now auto-auth' + : 'Finished forging, will now auto-auth' + ) + + await autoAuth() + + // Eslint disable: This should be caught by a if (isForging) {throw} at the + // beginning of this function + + // eslint-disable-next-line require-atomic-updates + isForging = false + flushPendingPuts() } /**