microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
bedf110fbb4cf82fb995f4cf2770e8339d5adbea

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/debuggerWorker.ts

87lines · modeblame

3fb37ad5unknown10 years ago1/// <reference path="../typings/vscode-react-native/vscode-react-native" />
2
3let PACKAGER = "localhost:8081";
4
5import * as websocket from "websocket";
6import {ScriptImporter} from "./scriptImporter";
bedf110funknown10 years ago7import {Log} from "../utils/commands/log";
3fb37ad5unknown10 years ago8
9let DebuggerWebSocket = (<any>websocket).w3cwebsocket;
10
11/* global __fbBatchedBridge */
12/* eslint no-unused-vars: 0 */
13
14export class DebuggerWorker {
15private ws: any;
16
17private projectRootPath: string;
18
19constructor(projectRootPath: string) {
20this.projectRootPath = projectRootPath;
21}
22
23private messageHandlers: any = {
24"prepareJSRuntime": function (message: any, cb: any) {
bedf110funknown10 years ago25Log.logMessage("React Native worker got prepareJSRuntime");
3fb37ad5unknown10 years ago26cb();
27},
28"executeApplicationScript": (message: any, cb: any) => {
bedf110funknown10 years ago29Log.logMessage("React Native worker got executeApplicationScript");
3fb37ad5unknown10 years ago30/* tslint:disable:forin */
31for (let key in message.inject) {
32/* tslint:enable:forin */
33(<any>global)[key] = JSON.parse(message.inject[key]);
34}
35// importScripts(message.url, cb);
36new ScriptImporter(this.projectRootPath).import(message.url).done(() => cb());
37},
38"executeBridgeJSCall": function (object: any, cb: any) {
39// Other methods get called on the bridge
40let returnValue: any[][] = [[], [], [], [], []];
41try {
42if (typeof __fbBatchedBridge === "object") {
43returnValue = __fbBatchedBridge[object.method].apply(null, object.arguments);
44}
45} finally {
46cb(JSON.stringify(returnValue));
47}
48}
49};
50
51private createSocket() {
52this.ws = new DebuggerWebSocket("ws://" + PACKAGER + "/debugger-proxy");
53
54this.ws.onopen = () => {
bedf110funknown10 years ago55Log.logMessage("WebSocket connection opened");
3fb37ad5unknown10 years ago56};
57this.ws.onclose = () => {
bedf110funknown10 years ago58Log.logMessage("WebSocket connection closed");
3fb37ad5unknown10 years ago59setTimeout(() => this.ws = this.createSocket(), 1000);
60};
61
62this.ws.onmessage = (message: any) => {
63let object = JSON.parse(message.data);
64if (!object.method) {
65return;
66}
67
68let handler = this.messageHandlers[object.method];
69if (!handler) {
70handler = this.messageHandlers.executeBridgeJSCall;
71}
72handler(object, (result: any) => {
73let response = JSON.stringify({
74replyID: object.id,
75result: result
76});
77this.ws.send(response);
78});
79};
80
81return this.ws;
82}
83
84public start() {
85this.ws = this.createSocket();
86}
87}