|
1 | | -import { ChromeDebugAdapter, IRestartRequestArgs, logger } from 'vscode-chrome-debug-core'; |
| 1 | +import { ChromeDebugAdapter, IRestartRequestArgs } from 'vscode-chrome-debug-core'; |
2 | 2 | import { Event, TerminatedEvent } from 'vscode-debugadapter'; |
3 | 3 | import { DebugProtocol } from 'vscode-debugprotocol'; |
4 | 4 | import * as extProtocol from '../common/extensionProtocol'; |
5 | | -import { AndroidProject } from '../project/androidProject'; |
6 | | -import { IosProject } from '../project/iosProject'; |
7 | | -import { NativeScriptCli } from '../project/nativeScriptCli'; |
8 | 5 |
|
9 | 6 | const reconnectAfterLiveSyncTimeout = 10 * 1000; |
10 | 7 |
|
11 | | -export function nativeScriptDebugAdapterGenerator(iosProject: typeof IosProject, |
12 | | - androidProject: typeof AndroidProject, |
13 | | - nativeScriptCli: typeof NativeScriptCli) { |
14 | | - return class NativeScriptDebugAdapter extends ChromeDebugAdapter { |
15 | | - private _idCounter = 0; |
16 | | - private _pendingRequests: object = {}; |
17 | | - private isLiveSync: boolean = false; |
18 | | - private portWaitingResolve: any; |
19 | | - private isDisconnecting: boolean = false; |
20 | | - |
21 | | - public attach(args: any): Promise<void> { |
22 | | - return this.processRequestAndAttach(args); |
| 8 | +export class NativeScriptDebugAdapter extends ChromeDebugAdapter { |
| 9 | + private _idCounter = 0; |
| 10 | + private _pendingRequests: object = {}; |
| 11 | + private isLiveSync: boolean = false; |
| 12 | + private portWaitingResolve: any; |
| 13 | + private isDisconnecting: boolean = false; |
| 14 | + |
| 15 | + public attach(args: any): Promise<void> { |
| 16 | + return this.processRequestAndAttach(args); |
| 17 | + } |
| 18 | + |
| 19 | + public launch(args: any): Promise<void> { |
| 20 | + return this.processRequestAndAttach(args); |
| 21 | + } |
| 22 | + |
| 23 | + public onPortReceived(port) { |
| 24 | + this.portWaitingResolve && this.portWaitingResolve(port); |
| 25 | + } |
| 26 | + |
| 27 | + public onExtensionResponse(response) { |
| 28 | + this._pendingRequests[response.requestId](response.result); |
| 29 | + delete this._pendingRequests[response.requestId]; |
| 30 | + } |
| 31 | + |
| 32 | + public disconnect(args: any): void { |
| 33 | + this.isDisconnecting = true; |
| 34 | + if (!args.restart) { |
| 35 | + this.callRemoteMethod('buildService', 'disconnect'); |
23 | 36 | } |
24 | 37 |
|
25 | | - public launch(args: any): Promise<void> { |
26 | | - return this.processRequestAndAttach(args); |
27 | | - } |
| 38 | + super.disconnect(args); |
| 39 | + } |
28 | 40 |
|
29 | | - public onPortReceived(port) { |
30 | | - this.portWaitingResolve && this.portWaitingResolve(port); |
31 | | - } |
| 41 | + protected async terminateSession(reason: string, disconnectArgs?: DebugProtocol.DisconnectArguments, restart?: IRestartRequestArgs): Promise<void> { |
| 42 | + let restartRequestArgs; |
| 43 | + let timeoutId; |
32 | 44 |
|
33 | | - public onExtensionResponse(response) { |
34 | | - this._pendingRequests[response.requestId](response.result); |
35 | | - delete this._pendingRequests[response.requestId]; |
36 | | - } |
| 45 | + if (!this.isDisconnecting && this.isLiveSync) { |
| 46 | + const portProm = new Promise<any>((res, rej) => { |
| 47 | + this.portWaitingResolve = res; |
37 | 48 |
|
38 | | - public disconnect(args: any): void { |
39 | | - this.isDisconnecting = true; |
40 | | - if (!args.restart) { |
41 | | - this.callRemoteMethod('buildService', 'disconnect'); |
42 | | - } |
| 49 | + timeoutId = setTimeout(() => { |
| 50 | + res(); |
| 51 | + }, reconnectAfterLiveSyncTimeout); |
| 52 | + }); |
43 | 53 |
|
44 | | - super.disconnect(args); |
| 54 | + restartRequestArgs = await portProm; |
| 55 | + clearTimeout(timeoutId); |
45 | 56 | } |
46 | 57 |
|
47 | | - protected async terminateSession(reason: string, disconnectArgs?: DebugProtocol.DisconnectArguments, restart?: IRestartRequestArgs): Promise<void> { |
48 | | - let restartRequestArgs; |
49 | | - let timeoutId; |
| 58 | + await super.terminateSession(reason, disconnectArgs, restartRequestArgs); |
| 59 | + } |
| 60 | + |
| 61 | + protected hookConnectionEvents(): void { |
| 62 | + super.hookConnectionEvents(); |
50 | 63 |
|
51 | | - if (!this.isDisconnecting && this.isLiveSync) { |
52 | | - const portProm = new Promise<any>((res, rej) => { |
53 | | - this.portWaitingResolve = res; |
| 64 | + this.chrome.Log.onEntryAdded((params) => this.onEntryAdded(params)); |
| 65 | + } |
54 | 66 |
|
55 | | - timeoutId = setTimeout(() => { |
56 | | - res(); |
57 | | - }, reconnectAfterLiveSyncTimeout); |
58 | | - }); |
| 67 | + protected onEntryAdded(params: any): void { |
| 68 | + if (params && params.entry && params.entry.level) { |
| 69 | + const message = params.entry; |
59 | 70 |
|
60 | | - restartRequestArgs = await portProm; |
61 | | - clearTimeout(timeoutId); |
| 71 | + message.type = message.level; |
| 72 | + |
| 73 | + const that = this as any; |
| 74 | + const script = that.getScriptByUrl && that.getScriptByUrl(params.entry.url); |
| 75 | + |
| 76 | + if (script) { |
| 77 | + message.stack = { |
| 78 | + callFrames: [ |
| 79 | + { |
| 80 | + columnNumber: 0, |
| 81 | + lineNumber: params.entry.lineNumber, |
| 82 | + scriptId: script.scriptId, |
| 83 | + url: params.entry.url, |
| 84 | + }, |
| 85 | + ], |
| 86 | + }; |
62 | 87 | } |
63 | 88 |
|
64 | | - await super.terminateSession(reason, disconnectArgs, restartRequestArgs); |
| 89 | + this.onMessageAdded({ |
| 90 | + message, |
| 91 | + }); |
65 | 92 | } |
| 93 | + } |
66 | 94 |
|
67 | | - protected hookConnectionEvents(): void { |
68 | | - super.hookConnectionEvents(); |
| 95 | + private async processRequestAndAttach(args: any) { |
| 96 | + const transformedArgs = this.translateArgs(args); |
69 | 97 |
|
70 | | - this.chrome.Log.onEntryAdded((params) => this.onEntryAdded(params)); |
| 98 | + if (args.__restart) { |
| 99 | + transformedArgs.port = args.__restart.port; |
| 100 | + } else { |
| 101 | + this._session.sendEvent(new Event(extProtocol.BEFORE_DEBUG_START)); |
| 102 | + transformedArgs.port = await this.callRemoteMethod('buildService', 'processRequest', transformedArgs); |
71 | 103 | } |
72 | 104 |
|
73 | | - protected onEntryAdded(params: any): void { |
74 | | - if (params && params.entry && params.entry.level) { |
75 | | - const message = params.entry; |
76 | | - |
77 | | - message.type = message.level; |
78 | | - |
79 | | - const that = this as any; |
80 | | - const script = that.getScriptByUrl && that.getScriptByUrl(params.entry.url); |
81 | | - |
82 | | - if (script) { |
83 | | - message.stack = { |
84 | | - callFrames: [ |
85 | | - { |
86 | | - columnNumber: 0, |
87 | | - lineNumber: params.entry.lineNumber, |
88 | | - scriptId: script.scriptId, |
89 | | - url: params.entry.url, |
90 | | - }, |
91 | | - ], |
92 | | - }; |
93 | | - } |
94 | | - |
95 | | - this.onMessageAdded({ |
96 | | - message, |
97 | | - }); |
98 | | - } |
| 105 | + if (!transformedArgs.port) { |
| 106 | + this._session.sendEvent(new TerminatedEvent()); |
99 | 107 | } |
100 | 108 |
|
101 | | - private async processRequestAndAttach(args: any) { |
102 | | - const transformedArgs = this.translateArgs(args); |
| 109 | + (this.pathTransformer as any).setTargetPlatform(args.platform); |
| 110 | + (ChromeDebugAdapter as any).SET_BREAKPOINTS_TIMEOUT = 20000; |
103 | 111 |
|
104 | | - if (args.__restart) { |
105 | | - transformedArgs.port = args.__restart.port; |
106 | | - } else { |
107 | | - this._session.sendEvent(new Event(extProtocol.BEFORE_DEBUG_START)); |
108 | | - transformedArgs.port = await this.callRemoteMethod('buildService', 'processRequest', transformedArgs); |
109 | | - } |
| 112 | + this.isLiveSync = args.watch; |
110 | 113 |
|
111 | | - if (!transformedArgs.port) { |
112 | | - this._session.sendEvent(new TerminatedEvent()); |
113 | | - } |
114 | | - |
115 | | - (this.pathTransformer as any).setTargetPlatform(args.platform); |
116 | | - (ChromeDebugAdapter as any).SET_BREAKPOINTS_TIMEOUT = 20000; |
117 | | - |
118 | | - this.isLiveSync = args.watch; |
| 114 | + return super.attach(transformedArgs); |
| 115 | + } |
119 | 116 |
|
120 | | - return super.attach(transformedArgs); |
| 117 | + private translateArgs(args): any { |
| 118 | + if (args.diagnosticLogging) { |
| 119 | + args.trace = args.diagnosticLogging; |
121 | 120 | } |
122 | 121 |
|
123 | | - private translateArgs(args): any { |
124 | | - if (args.diagnosticLogging) { |
125 | | - args.trace = args.diagnosticLogging; |
126 | | - } |
127 | | - |
128 | | - if (args.appRoot) { |
129 | | - args.webRoot = args.appRoot; |
130 | | - } |
131 | | - |
132 | | - return args; |
| 122 | + if (args.appRoot) { |
| 123 | + args.webRoot = args.appRoot; |
133 | 124 | } |
134 | 125 |
|
135 | | - private callRemoteMethod<T>(service: string, method: string, ...args: any[]): Promise<T> { |
136 | | - const request: extProtocol.IRequest = { id: `req${++this._idCounter}`, service, method, args }; |
| 126 | + return args; |
| 127 | + } |
137 | 128 |
|
138 | | - return new Promise<T>((res, rej) => { |
139 | | - this._pendingRequests[request.id] = res; |
| 129 | + private callRemoteMethod<T>(service: string, method: string, ...args: any[]): Promise<T> { |
| 130 | + const request: extProtocol.IRequest = { id: `req${++this._idCounter}`, service, method, args }; |
140 | 131 |
|
141 | | - this._session.sendEvent(new Event(extProtocol.NS_DEBUG_ADAPTER_MESSAGE, request)); |
142 | | - }); |
143 | | - } |
144 | | - }; |
145 | | -} |
| 132 | + return new Promise<T>((res, rej) => { |
| 133 | + this._pendingRequests[request.id] = res; |
| 134 | + |
| 135 | + this._session.sendEvent(new Event(extProtocol.NS_DEBUG_ADAPTER_MESSAGE, request)); |
| 136 | + }); |
| 137 | + } |
| 138 | +}; |
0 commit comments