microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/cdp-proxy/CDPMessageHandlers/rnCDPMessageHandler.ts
60lines · modeblame
a6562589RedMickey6 years ago | 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. | |
| 3 | | |
b7451aefRedMickey6 years ago | 4 | import { IProtocolCommand } from "vscode-cdp-proxy"; |
259c018fYuri Skorokhodov5 years ago | 5 | import { ProcessedCDPMessage } from "./ICDPMessageHandler"; |
b7451aefRedMickey6 years ago | 6 | import { CDP_API_NAMES } from "./CDPAPINames"; |
259c018fYuri Skorokhodov5 years ago | 7 | import { BaseCDPMessageHandler } from "./baseCDPMessageHandler"; |
a6562589RedMickey6 years ago | 8 | |
259c018fYuri Skorokhodov5 years ago | 9 | export class RnCDPMessageHandler extends BaseCDPMessageHandler { |
a6562589RedMickey6 years ago | 10 | private firstStop: boolean; |
| 11 | | |
| 12 | constructor() { | |
259c018fYuri Skorokhodov5 years ago | 13 | super(); |
a6562589RedMickey6 years ago | 14 | this.firstStop = true; |
| 15 | } | |
| 16 | | |
b7451aefRedMickey6 years ago | 17 | public processDebuggerCDPMessage(event: any): ProcessedCDPMessage { |
09f6024fHeniker4 years ago | 18 | const sendBack = false; |
b7451aefRedMickey6 years ago | 19 | if (event.method === CDP_API_NAMES.CLOSE) { |
a6562589RedMickey6 years ago | 20 | this.handleDebuggerDisconnect(); |
| 21 | } | |
| 22 | | |
b7451aefRedMickey6 years ago | 23 | return { |
| 24 | event, | |
| 25 | sendBack, | |
| 26 | }; | |
| 27 | } | |
| 28 | | |
| 29 | public processApplicationCDPMessage(event: any): ProcessedCDPMessage { | |
09f6024fHeniker4 years ago | 30 | const sendBack = false; |
b7451aefRedMickey6 years ago | 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 | }; | |
a6562589RedMickey6 years ago | 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 | */ | |
b7451aefRedMickey6 years ago | 48 | private handleAppBundleFirstPauseEvent(event: IProtocolCommand): any { |
09f6024fHeniker4 years ago | 49 | const params: any = event.params; |
a6562589RedMickey6 years ago | 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 | } |