microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
3a0bfdb2c230b6319fe09bfd70e3bae29d0fe088

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/extensionMessaging.ts

66lines · 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}
21
22export interface MessageWithArguments {
23 message: ExtensionMessage;
24 args: any[];
25}
26
27/**
28 * Sends messages to the extension.
29 */
30export class ExtensionMessageSender {
31
32 public sendMessage(message: ExtensionMessage, args?: any[]): Q.Promise<any> {
33 let deferred = Q.defer<any>();
34 let messageWithArguments: MessageWithArguments = { message: message, args: args };
35 let body = "";
36
37 let pipePath = HostPlatform.getExtensionPipePath();
38 let socket = net.connect(pipePath, function() {
39 let messageJson = JSON.stringify(messageWithArguments);
40 socket.write(messageJson);
41 });
42
43 socket.on("data", function(data: any) {
44 body += data;
45 });
46
47 socket.on("error", function(data: any) {
48 deferred.reject(new Error("An error ocurred while handling message: " + ExtensionMessage[message]));
49 });
50
51 socket.on("end", function() {
52 try {
53 if (body === ErrorMarker) {
54 deferred.reject(new Error("An error ocurred while handling message: " + ExtensionMessage[message]));
55 } else {
56 let responseBody: any = body ? JSON.parse(body) : null;
57 deferred.resolve(responseBody);
58 }
59 } catch (e) {
60 deferred.reject(e);
61 }
62 });
63
64 return deferred.promise;
65 }
66}