1+ import * as path from 'path' ;
2+ import * as extProtocol from './ExtensionProtocol' ;
3+ let ipc = require ( 'node-ipc' ) ;
4+
5+ export class ExtensionClient {
6+ private static _appRoot : string ;
7+ private static _instance : ExtensionClient ;
8+
9+ private _idCounter = 0 ;
10+ private _pendingRequests : Object ;
11+
12+ private _ipcClientInitialized : Promise < any > ;
13+
14+ public static getInstance ( ) {
15+ if ( ! this . _instance ) {
16+ this . _instance = new ExtensionClient ( ) ;
17+ }
18+ return this . _instance ;
19+ }
20+
21+ public static getAppRoot ( ) {
22+ return this . _appRoot ;
23+ }
24+
25+ public static setAppRoot ( appRoot : string ) {
26+ this . _appRoot = appRoot ;
27+ }
28+
29+ constructor ( ) {
30+ if ( ! ExtensionClient . getAppRoot ( ) ) {
31+ throw new Error ( `Unable to connect to extension host. App root is '${ ExtensionClient . getAppRoot ( ) } '` ) ;
32+ }
33+
34+ this . _idCounter = 0 ;
35+ this . _pendingRequests = { } ;
36+
37+ ipc . config . id = 'debug-adpater-' + process . pid ;
38+ ipc . config . retry = 1500 ;
39+
40+ this . _ipcClientInitialized = new Promise ( ( res , rej ) => {
41+ ipc . connectTo (
42+ 'extHost' ,
43+ path . join ( ExtensionClient . getAppRoot ( ) , 'temp-nativescript-vscode-extension-pipe-handle' ) ,
44+ ( ) => {
45+ ipc . of . extHost . on ( 'connect' , ( ) => {
46+ res ( ) ;
47+ } ) ;
48+ ipc . of . extHost . on ( 'extension-protocol-message' , ( response : extProtocol . Response ) => {
49+ ( < ( result : Object ) => void > this . _pendingRequests [ response . requestId ] ) ( response . result ) ;
50+ } ) ;
51+ }
52+ ) ;
53+ } ) ;
54+ }
55+
56+ private callRemoteMethod ( method : string , args : Object ) : Promise < Object > {
57+ let request : extProtocol . Request = { id : 'req' + ( ++ this . _idCounter ) , method : method , args : args } ;
58+ return new Promise < Object > ( ( res , rej ) => {
59+ this . _pendingRequests [ request . id ] = res ;
60+ ipc . of . extHost . emit ( 'extension-protocol-message' , request ) ;
61+ } ) ;
62+ }
63+
64+ public analyticsLaunchDebugger ( args : extProtocol . AnalyticsLaunchDebuggerArgs ) : Promise < any > {
65+ return this . callRemoteMethod ( 'analyticsLaunchDebugger' , args ) ;
66+ }
67+
68+ public runRunCommand ( args : extProtocol . AnalyticsRunRunCommandArgs ) : Promise < any > {
69+ return this . callRemoteMethod ( 'runRunCommand' , args ) ;
70+ }
71+ }
0 commit comments