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