microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/cdp-proxy/CDPMessageHandlers/rnCDPMessageHandler.ts
60lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. |
| 3 | |
| 4 | import { IProtocolCommand } from "vscode-cdp-proxy"; |
| 5 | import { ProcessedCDPMessage } from "./ICDPMessageHandler"; |
| 6 | import { CDP_API_NAMES } from "./CDPAPINames"; |
| 7 | import { BaseCDPMessageHandler } from "./baseCDPMessageHandler"; |
| 8 | |
| 9 | export class RnCDPMessageHandler extends BaseCDPMessageHandler { |
| 10 | private firstStop: boolean; |
| 11 | |
| 12 | constructor() { |
| 13 | super(); |
| 14 | this.firstStop = true; |
| 15 | } |
| 16 | |
| 17 | public processDebuggerCDPMessage(event: any): ProcessedCDPMessage { |
| 18 | let sendBack = false; |
| 19 | if (event.method === CDP_API_NAMES.CLOSE) { |
| 20 | this.handleDebuggerDisconnect(); |
| 21 | } |
| 22 | |
| 23 | return { |
| 24 | event, |
| 25 | sendBack, |
| 26 | }; |
| 27 | } |
| 28 | |
| 29 | public processApplicationCDPMessage(event: any): ProcessedCDPMessage { |
| 30 | let sendBack = false; |
| 31 | if (event.method === CDP_API_NAMES.DEBUGGER_PAUSED && this.firstStop) { |
| 32 | event.params = this.handleAppBundleFirstPauseEvent(event); |
| 33 | } |
| 34 | |
| 35 | return { |
| 36 | event, |
| 37 | sendBack, |
| 38 | }; |
| 39 | } |
| 40 | |
| 41 | /** Since the bundle runs inside the Node.js VM in `debuggerWorker.js` in runtime |
| 42 | * Node debug adapter need time to parse new added code source maps |
| 43 | * So we added `debugger;` statement at the start of the bundle code |
| 44 | * and wait for the adapter to receive a signal to stop on that statement |
| 45 | * and then change pause reason to `Break on start` so js-debug can process all breakpoints in the bundle and |
| 46 | * continue the code execution using `continueOnAttach` flag |
| 47 | */ |
| 48 | private handleAppBundleFirstPauseEvent(event: IProtocolCommand): any { |
| 49 | let params: any = event.params; |
| 50 | if (params.reason && params.reason === "other") { |
| 51 | this.firstStop = false; |
| 52 | params.reason = "Break on start"; |
| 53 | } |
| 54 | return params; |
| 55 | } |
| 56 | |
| 57 | private handleDebuggerDisconnect() { |
| 58 | this.firstStop = true; |
| 59 | } |
| 60 | } |
| 61 | |