microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/debugger/debuggerWorker.ts
83lines · modeblame
3fb37ad5unknown10 years ago | 1 | import * as websocket from "websocket"; |
| 2 | import {ScriptImporter} from "./scriptImporter"; | |
bedf110funknown10 years ago | 3 | import {Log} from "../utils/commands/log"; |
f5ea0577unknown10 years ago | 4 | import {Packager} from "./packager"; |
3fb37ad5unknown10 years ago | 5 | |
65ee84c9unknown10 years ago | 6 | declare var __fbBatchedBridge: any; |
3fb37ad5unknown10 years ago | 7 | |
65ee84c9unknown10 years ago | 8 | let DebuggerWebSocket = (<any>websocket).w3cwebsocket; |
3fb37ad5unknown10 years ago | 9 | |
| 10 | export 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 = { | |
f5ea0577unknown10 years ago | 20 | "prepareJSRuntime": function(message: any, cb: any) { |
bedf110funknown10 years ago | 21 | Log.logMessage("React Native worker got prepareJSRuntime"); |
3fb37ad5unknown10 years ago | 22 | cb(); |
| 23 | }, | |
| 24 | "executeApplicationScript": (message: any, cb: any) => { | |
bedf110funknown10 years ago | 25 | Log.logMessage("React Native worker got executeApplicationScript"); |
3fb37ad5unknown10 years ago | 26 | /* tslint:disable:forin */ |
| 27 | for (let key in message.inject) { | |
f5ea0577unknown10 years ago | 28 | /* tslint:enable:forin */ |
3fb37ad5unknown10 years ago | 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 | }, | |
f5ea0577unknown10 years ago | 34 | "executeBridgeJSCall": function(object: any, cb: any) { |
3fb37ad5unknown10 years ago | 35 | // Other methods get called on the bridge |
| 36 | let returnValue: any[][] = [[], [], [], [], []]; | |
| 37 | try { | |
f5ea0577unknown10 years ago | 38 | if (typeof __fbBatchedBridge === "object") { |
| 39 | returnValue = __fbBatchedBridge[object.method].apply(null, object.arguments); | |
| 40 | } | |
3fb37ad5unknown10 years ago | 41 | } finally { |
| 42 | cb(JSON.stringify(returnValue)); | |
| 43 | } | |
| 44 | } | |
| 45 | }; | |
| 46 | | |
| 47 | private createSocket() { | |
3af9a124unknown10 years ago | 48 | this.ws = new DebuggerWebSocket(`ws://${Packager.HOST}/debugger-proxy`); |
3fb37ad5unknown10 years ago | 49 | |
| 50 | this.ws.onopen = () => { | |
bedf110funknown10 years ago | 51 | Log.logMessage("WebSocket connection opened"); |
3fb37ad5unknown10 years ago | 52 | }; |
| 53 | this.ws.onclose = () => { | |
bedf110funknown10 years ago | 54 | Log.logMessage("WebSocket connection closed"); |
3fb37ad5unknown10 years ago | 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 | } |