From 4944b9b1d37523cc72d57387184bc1710ba589f5 Mon Sep 17 00:00:00 2001 From: Timothy Gu Date: Sun, 26 Nov 2017 21:22:06 -0800 Subject: [PATCH] Fix event timing --- bin/c8.js | 45 +++++++++++++++++++++++++++++++-------------- 1 file changed, 31 insertions(+), 14 deletions(-) diff --git a/bin/c8.js b/bin/c8.js index 91fd8b06a..e50101f86 100755 --- a/bin/c8.js +++ b/bin/c8.js @@ -8,31 +8,48 @@ const waitTillPortOpen = require('wait-till-port-open') getPort().then(async port => { foreground( - ['node', `--inspect-brk=${port}`].concat(process.argv.slice(2)), + [process.execPath, `--inspect-brk=${port}`].concat(process.argv.slice(2)), (done) => { console.info('actually got here') + done() } ) try { await waitTillPortOpen(port) const client = await CRI({port: port}) + const initialPause = new Promise((resolve) => { + client.once('Debugger.paused', resolve) + }) + + const mainContextInfo = new Promise((resolve) => { + client.once('Runtime.executionContextCreated', (message) => { + resolve(message.context) + }) + }) + + const executionComplete = new Promise((resolve) => { + client.on('Runtime.executionContextDestroyed', async (message) => { + if (message.executionContextId === (await mainContextInfo).id) { + resolve(message) + } + }) + }) + const {Debugger, Runtime, Profiler} = client - await Runtime.runIfWaitingForDebugger() - await Runtime.enable() - await Profiler.enable() - await Profiler.startPreciseCoverage({callCount: true, detailed: true}) - await Debugger.enable() - await Debugger.paused() + await Promise.all([ + Profiler.enable(), + Runtime.enable(), + Debugger.enable(), + Profiler.startPreciseCoverage({callCount: true, detailed: true}), + Runtime.runIfWaitingForDebugger(), + initialPause + ]) await Debugger.resume() - client.on('event', async (message) => { - // console.info(message) - if (message.method === 'Runtime.executionContextDestroyed') { - await outputCoverage(Profiler) - client.close() - } - }) + await executionComplete + await outputCoverage(Profiler) + client.close() } catch (err) { console.error(err)