forge() should not be awaited

This commit is contained in:
Daniel Lugo 2021-09-23 10:09:44 -04:00
parent 2acb7bc379
commit 06d20f9c16

View file

@ -241,102 +241,104 @@ let procCounter = 0
let killed = false let killed = false
const forge = async () => { const forge = () => {
if (killed) { ;(async () => {
throw new Error('Tried to forge after killing GunSmith') if (killed) {
} throw new Error('Tried to forge after killing GunSmith')
logger.info(`Forging Gun # ${++procCounter}`) }
if (isForging) { logger.info(`Forging Gun # ${++procCounter}`)
throw new Error('Double forge?') if (isForging) {
} throw new Error('Double forge?')
}
/** Used only for logs. */ /** Used only for logs. */
const isReforge = !!currentGun const isReforge = !!currentGun
logger.info(isReforge ? 'Will reforge' : 'Will forge') logger.info(isReforge ? 'Will reforge' : 'Will forge')
isForging = true isForging = true
if (currentGun) { if (currentGun) {
currentGun.off('message', handleMsg) currentGun.off('message', handleMsg)
currentGun.disconnect() currentGun.disconnect()
currentGun.kill() currentGun.kill()
logger.info('Destroyed current gun') logger.info('Destroyed current gun')
} }
const newGun = fork('utils/GunSmith/gun.js') const newGun = fork('utils/GunSmith/gun.js')
currentGun = newGun currentGun = newGun
logger.info('Forged new gun') logger.info('Forged new gun')
// currentGun.on('', e => { // currentGun.on('', e => {
// logger.info('event from subprocess') // logger.info('event from subprocess')
// logger.info(e) // logger.info(e)
// }) // })
currentGun.on('message', handleMsg) currentGun.on('message', handleMsg)
/** @type {Smith.SmithMsgInit} */ /** @type {Smith.SmithMsgInit} */
const initMsg = { const initMsg = {
opts: lastOpts, opts: lastOpts,
type: 'init' type: 'init'
} }
await new Promise(res => { await new Promise(res => {
currentGun.on('message', msg => { currentGun.on('message', msg => {
if (msg.type === 'init') { if (msg.type === 'init') {
// @ts-ignore // @ts-ignore
res() res()
} }
})
currentGun.send(initMsg)
logger.info('Sent init msg')
}) })
currentGun.send(initMsg)
logger.info('Sent init msg')
})
logger.info('Received init reply') logger.info('Received init reply')
const lastGunListeners = Object.keys(pathToListeners).map(path => { const lastGunListeners = Object.keys(pathToListeners).map(path => {
/** @type {Smith.SmithMsgOn} */ /** @type {Smith.SmithMsgOn} */
const msg = { const msg = {
path, path,
type: 'on' type: 'on'
}
return msg
})
if (lastGunListeners.length) {
currentGun.send(lastGunListeners)
logger.info(`Sent ${lastGunListeners.length} pending on() listeners`)
} }
return msg
})
if (lastGunListeners.length) { const lastGunMapListeners = Object.keys(pathToMapListeners).map(path => {
currentGun.send(lastGunListeners) /** @type {Smith.SmithMsgMapOn} */
const msg = {
path,
type: 'map.on'
}
return msg
})
logger.info(`Sent ${lastGunListeners.length} pending on() listeners`) if (lastGunMapListeners.length) {
} currentGun.send(lastGunMapListeners)
const lastGunMapListeners = Object.keys(pathToMapListeners).map(path => { logger.info(
/** @type {Smith.SmithMsgMapOn} */ `Sent ${lastGunMapListeners.length} pending map().on() listeners`
const msg = { )
path,
type: 'map.on'
} }
return msg
})
if (lastGunMapListeners.length) {
currentGun.send(lastGunMapListeners)
logger.info( logger.info(
`Sent ${lastGunMapListeners.length} pending map().on() listeners` isReforge
? 'Finished reforging, will now auto-auth'
: 'Finished forging, will now auto-auth'
) )
}
logger.info( await autoAuth()
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: This should be caught by a if (isForging) {throw} at the // eslint-disable-next-line require-atomic-updates
// beginning of this function isForging = false
flushPendingPuts()
// eslint-disable-next-line require-atomic-updates })()
isForging = false
flushPendingPuts()
} }
/** /**
@ -575,8 +577,7 @@ function createReplica(path, afterMap = false) {
setTimeout(() => { setTimeout(() => {
if (!canaryPeep) { if (!canaryPeep) {
forge() forge()
.then(isReady) isReady().then(checkCanary)
.then(checkCanary)
} }
}, 30000) }, 30000)