microsoft/vscode-react-native

Public

mirrored from https://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
a875cef475b9a3dbed5f54480837fd4956013416

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/extensionMessaging.ts

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