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