Skip to content

Commit

Permalink
Fix event timing
Browse files Browse the repository at this point in the history
  • Loading branch information
TimothyGu committed Dec 1, 2017
1 parent bb117b7 commit bbdaaab
Showing 1 changed file with 32 additions and 15 deletions.
47 changes: 32 additions & 15 deletions bin/c8.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,48 @@
#!/usr/bin/env node
'use strict'

const argv = require('yargs').parse()
const CRI = require('chrome-remote-interface')
const spawn = require('../lib/spawn')

;(async () => {
try {
info = await spawn(process.execPath,
[`--inspect-brk=0`].concat(process.argv.slice(2)))
const info = await spawn(process.execPath,
[`--inspect-brk=0`].concat(process.argv.slice(2)))
const client = await CRI({port: info.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)
Expand Down

0 comments on commit bbdaaab

Please sign in to comment.