microsoft/vscode-react-native

Public

mirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.1.3

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/common/extensionMessaging.ts

71lines · 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
4import * as Q from "q";
5import * as net from "net";
6import {HostPlatform} from "./hostPlatform";
7
8export let ErrorMarker = "vscodereactnative-error-marker";
9
10/**
11 * Defines the messages sent to the extension.
12 * Add new messages to this enum.
13 */
14export enum ExtensionMessage {
15 START_PACKAGER,
16 STOP_PACKAGER,
17 PREWARM_BUNDLE_CACHE,
18 START_MONITORING_LOGCAT,
19 STOP_MONITORING_LOGCAT,
20 SEND_TELEMETRY
21}
22
23export interface MessageWithArguments {
24 message: ExtensionMessage;
25 args: any[];
26}
27
28export interface IExtensionMessageSender {
29 sendMessage(message: ExtensionMessage, args?: any[]): Q.Promise<any>;
30}
31
32/**
33 * Sends messages to the extension.
34 */
35export class ExtensionMessageSender implements IExtensionMessageSender {
36
37 public sendMessage(message: ExtensionMessage, args?: any[]): Q.Promise<any> {
38 let deferred = Q.defer<any>();
39 let messageWithArguments: MessageWithArguments = { message: message, args: args };
40 let body = "";
41
42 let pipePath = HostPlatform.getExtensionPipePath();
43 let socket = net.connect(pipePath, function() {
44 let messageJson = JSON.stringify(messageWithArguments);
45 socket.write(messageJson);
46 });
47
48 socket.on("data", function(data: any) {
49 body += data;
50 });
51
52 socket.on("error", function(data: any) {
53 deferred.reject(new Error("An error ocurred while handling message: " + ExtensionMessage[message]));
54 });
55
56 socket.on("end", function() {
57 try {
58 if (body === ErrorMarker) {
59 deferred.reject(new Error("An error ocurred while handling message: " + ExtensionMessage[message]));
60 } else {
61 let responseBody: any = body ? JSON.parse(body) : null;
62 deferred.resolve(responseBody);
63 }
64 } catch (e) {
65 deferred.reject(e);
66 }
67 });
68
69 return deferred.promise;
70 }
71}