Skip to content

Commit

Permalink
simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
juan-fernandez committed Oct 25, 2024
1 parent 1ce0673 commit 6a6c9e6
Showing 1 changed file with 23 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { parentPort } = require('worker_threads')
// TODO: move session to common place?
// TODO: move session to common place
const session = require('../../../debugger/devtools_client/session')
// TODO: move getLocalStateForCallFrame to common place?
// TODO: move getLocalStateForCallFrame to common place
const { getLocalStateForCallFrame } = require('../../../debugger/devtools_client/snapshot')
const log = require('../../log')

Expand All @@ -10,8 +10,8 @@ let sessionStarted = false
const scriptIds = []
const scriptUrls = new Map()

const probes = new Map()
const breakpoints = new Map()
const breakpointIdToSnapshotId = new Map()
const breakpointIdToProbe = new Map()

function findScriptFromPartialPath (path) {
return scriptIds
Expand All @@ -26,34 +26,31 @@ session.on('Debugger.scriptParsed', ({ params }) => {
}
})

session.on('Debugger.paused', async ({ params }) => {
const { hitBreakpoints: [hitBreakpoint], callFrames } = params

const probe = breakpoints.get(hitBreakpoint)
const probeId = probes.get(hitBreakpoint)
session.on('Debugger.paused', async ({ params: { hitBreakpoints: [hitBreakpoint], callFrames } }) => {
const probe = breakpointIdToProbe.get(hitBreakpoint)
if (!probe) {
return session.post('Debugger.resume')
}

const stack = callFrames.map(({ functionName, location: { scriptId, lineNumber, columnNumber } }) => {
let fileName = scriptUrls.get(scriptId)
const stack = callFrames.map((frame) => {
let fileName = scriptUrls.get(frame.location.scriptId)
if (fileName.startsWith('file://')) fileName = fileName.substr(7) // TODO: This might not be required

return {
fileName,
function: functionName,
lineNumber: lineNumber + 1,
columnNumber: columnNumber + 1
function: frame.functionName,
lineNumber: frame.location.lineNumber + 1, // Beware! lineNumber is zero-indexed
columnNumber: frame.location.columnNumber + 1 // Beware! columnNumber is zero-indexed
}
})

const getLocalState = await getLocalStateForCallFrame(callFrames[0])

await session.post('Debugger.resume')

if (!probe) {
return
}
const snapshotId = breakpointIdToSnapshotId.get(hitBreakpoint)

const snapshot = {
id: probe.snapshotId,
id: snapshotId,
timestamp: Date.now(),
probe: {
id: probe.probeId,
Expand All @@ -71,18 +68,17 @@ session.on('Debugger.paused', async ({ params }) => {
}
}

parentPort.postMessage({ probe, snapshot, id: probeId })
parentPort.postMessage({ snapshot })
})

// TODO: add option to remove breakpoint
parentPort.on('message', async ({ snapshotId, probe: { id: probeId, file, line } }) => {
// only message at the moment is a line probe
// TODO: add option to remove breakpoint
await addBreakpoint({ probeId, file, line, snapshotId })
await addBreakpoint(snapshotId, { probeId, file, line })
})

async function addBreakpoint (probe) {
async function addBreakpoint (snapshotId, probe) {
if (!sessionStarted) await start()
const { file, line, probeId } = probe
const { file, line } = probe

probe.location = { file, lines: [String(line)] }

Expand All @@ -100,9 +96,8 @@ async function addBreakpoint (probe) {
}
})

probes.set(breakpointId, probeId)

breakpoints.set(breakpointId, probe)
breakpointIdToProbe.set(breakpointId, probe)
breakpointIdToSnapshotId.set(breakpointId, snapshotId)
}

function start () {
Expand Down

0 comments on commit 6a6c9e6

Please sign in to comment.