microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
34ec037534c478dfcbd6b40c4585561565cfc0d0

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/debuggerWorker.ts

83lines · modecode

1import * as websocket from "websocket";
2import {ScriptImporter} from "./scriptImporter";
3import {Log} from "../utils/commands/log";
4import {Packager} from "./packager";
5
6declare var __fbBatchedBridge: any;
7
8let DebuggerWebSocket = (<any>websocket).w3cwebsocket;
9
10export class DebuggerWorker {
11 private ws: any;
12
13 private projectRootPath: string;
14
15 constructor(projectRootPath: string) {
16 this.projectRootPath = projectRootPath;
17 }
18
19 private messageHandlers: any = {
20 "prepareJSRuntime": function(message: any, cb: any) {
21 Log.logMessage("React Native worker got prepareJSRuntime");
22 cb();
23 },
24 "executeApplicationScript": (message: any, cb: any) => {
25 Log.logMessage("React Native worker got executeApplicationScript");
26 /* tslint:disable:forin */
27 for (let key in message.inject) {
28 /* tslint:enable:forin */
29 (<any>global)[key] = JSON.parse(message.inject[key]);
30 }
31 // importScripts(message.url, cb);
32 new ScriptImporter(this.projectRootPath).import(message.url).done(() => cb());
33 },
34 "executeBridgeJSCall": function(object: any, cb: any) {
35 // Other methods get called on the bridge
36 let returnValue: any[][] = [[], [], [], [], []];
37 try {
38 if (typeof __fbBatchedBridge === "object") {
39 returnValue = __fbBatchedBridge[object.method].apply(null, object.arguments);
40 }
41 } finally {
42 cb(JSON.stringify(returnValue));
43 }
44 }
45 };
46
47 private createSocket() {
48 this.ws = new DebuggerWebSocket(`ws://${Packager.HOST}/debugger-proxy`);
49
50 this.ws.onopen = () => {
51 Log.logMessage("WebSocket connection opened");
52 };
53 this.ws.onclose = () => {
54 Log.logMessage("WebSocket connection closed");
55 setTimeout(() => this.ws = this.createSocket(), 1000);
56 };
57
58 this.ws.onmessage = (message: any) => {
59 let object = JSON.parse(message.data);
60 if (!object.method) {
61 return;
62 }
63
64 let handler = this.messageHandlers[object.method];
65 if (!handler) {
66 handler = this.messageHandlers.executeBridgeJSCall;
67 }
68 handler(object, (result: any) => {
69 let response = JSON.stringify({
70 replyID: object.id,
71 result: result
72 });
73 this.ws.send(response);
74 });
75 };
76
77 return this.ws;
78 }
79
80 public start() {
81 this.ws = this.createSocket();
82 }
83}